PageRenderTime 60ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/cake/cake/libs/cache.php

https://github.com/geeknbar/projet-tut-site-web
PHP | 509 lines | 247 code | 32 blank | 230 comment | 64 complexity | 1c62a4fd56fb93dd53401a6e73b04f0b MD5 | raw file
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * Caching for CakePHP.
  5. *
  6. *
  7. * PHP versions 4 and 5
  8. *
  9. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  10. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * Redistributions of files must retain the above copyright notice.
  14. *
  15. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  16. * @link http://cakephp.org CakePHP(tm) Project
  17. * @package cake
  18. * @subpackage cake.cake.libs
  19. * @since CakePHP(tm) v 1.2.0.4933
  20. * @version $Revision$
  21. * @modifiedby $LastChangedBy$
  22. * @lastmodified $Date$
  23. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  24. */
  25. /**
  26. * Caching for CakePHP.
  27. *
  28. * @package cake
  29. * @subpackage cake.cake.libs
  30. */
  31. class Cache extends Object {
  32. /**
  33. * Cache engine to use
  34. *
  35. * @var CacheEngine
  36. * @access protected
  37. */
  38. var $_Engine = null;
  39. /**
  40. * Cache configuration stack
  41. *
  42. * @var array
  43. * @access private
  44. */
  45. var $__config = array();
  46. /**
  47. * Holds name of the current configuration being used
  48. *
  49. * @var array
  50. * @access private
  51. */
  52. var $__name = 'default';
  53. /**
  54. * whether to reset the settings with the next call to self::set();
  55. *
  56. * @var array
  57. * @access private
  58. */
  59. var $__reset = false;
  60. /**
  61. * Returns a singleton instance
  62. *
  63. * @return object
  64. * @access public
  65. * @static
  66. */
  67. function &getInstance() {
  68. static $instance = array();
  69. if (!$instance) {
  70. $instance[0] =& new Cache();
  71. }
  72. return $instance[0];
  73. }
  74. /**
  75. * Tries to find and include a file for a cache engine and returns object instance
  76. *
  77. * @param $name Name of the engine (without 'Engine')
  78. * @return mixed $engine object or null
  79. * @access private
  80. */
  81. function __loadEngine($name) {
  82. if (!class_exists($name . 'Engine')) {
  83. require LIBS . 'cache' . DS . strtolower($name) . '.php';
  84. }
  85. return true;
  86. }
  87. /**
  88. * Set the cache configuration to use
  89. *
  90. * @see app/config/core.php for configuration settings
  91. * @param string $name Name of the configuration
  92. * @param array $settings Optional associative array of settings passed to the engine
  93. * @return array(engine, settings) on success, false on failure
  94. * @access public
  95. * @static
  96. */
  97. function config($name = null, $settings = array()) {
  98. $_this =& Cache::getInstance();
  99. if (is_array($name)) {
  100. $settings = $name;
  101. }
  102. if ($name === null || !is_string($name)) {
  103. $name = $_this->__name;
  104. }
  105. $current = array();
  106. if (isset($_this->__config[$name])) {
  107. $current = $_this->__config[$name];
  108. }
  109. if (!empty($settings)) {
  110. $_this->__name = null;
  111. $_this->__config[$name] = array_merge($current, $settings);
  112. }
  113. if (empty($_this->__config[$name]['engine'])) {
  114. return false;
  115. }
  116. $_this->__name = $name;
  117. $engine = $_this->__config[$name]['engine'];
  118. if (!$_this->isInitialized($engine)) {
  119. if ($_this->engine($engine, $_this->__config[$name]) === false) {
  120. return false;
  121. }
  122. $settings = $_this->__config[$name] = $_this->settings($engine);
  123. } else {
  124. $settings = $_this->__config[$name] = $_this->set($_this->__config[$name]);
  125. }
  126. return compact('engine', 'settings');
  127. }
  128. /**
  129. * Set the cache engine to use or modify settings for one instance
  130. *
  131. * @param string $name Name of the engine (without 'Engine')
  132. * @param array $settings Optional associative array of settings passed to the engine
  133. * @return boolean True on success, false on failure
  134. * @access public
  135. * @static
  136. */
  137. function engine($name = 'File', $settings = array()) {
  138. $cacheClass = $name . 'Engine';
  139. $_this =& Cache::getInstance();
  140. if (!isset($_this->_Engine[$name])) {
  141. if ($_this->__loadEngine($name) === false) {
  142. return false;
  143. }
  144. $_this->_Engine[$name] =& new $cacheClass();
  145. }
  146. if ($_this->_Engine[$name]->init($settings)) {
  147. if (time() % $_this->_Engine[$name]->settings['probability'] === 0) {
  148. $_this->_Engine[$name]->gc();
  149. }
  150. return true;
  151. }
  152. $_this->_Engine[$name] = null;
  153. return false;
  154. }
  155. /**
  156. * Temporarily change settings to current config options. if no params are passed, resets settings if needed
  157. * Cache::write() will reset the configuration changes made
  158. *
  159. * @param mixed $settings Optional string for simple name-value pair or array
  160. * @param string $value Optional for a simple name-value pair
  161. * @return array of settings
  162. * @access public
  163. * @static
  164. */
  165. function set($settings = array(), $value = null) {
  166. $_this =& Cache::getInstance();
  167. if (!isset($_this->__config[$_this->__name])) {
  168. return false;
  169. }
  170. $engine = $_this->__config[$_this->__name]['engine'];
  171. if (isset($settings['engine'])) {
  172. $engine = $settings['engine'];
  173. }
  174. if (!empty($settings)) {
  175. $_this->__reset = true;
  176. }
  177. if ($_this->__reset === true) {
  178. if (empty($settings)) {
  179. $_this->__reset = false;
  180. $settings = $_this->__config[$_this->__name];
  181. } else {
  182. if (is_string($settings) && $value !== null) {
  183. $settings = array($settings => $value);
  184. }
  185. $settings = array_merge($_this->__config[$_this->__name], $settings);
  186. }
  187. $_this->_Engine[$engine]->init($settings);
  188. }
  189. return $_this->settings($engine);
  190. }
  191. /**
  192. * Garbage collection
  193. *
  194. * Permanently remove all expired and deleted data
  195. *
  196. * @return void
  197. * @access public
  198. * @static
  199. */
  200. function gc() {
  201. $_this =& Cache::getInstance();
  202. $config = $_this->config();
  203. $_this->_Engine[$config['engine']]->gc();
  204. }
  205. /**
  206. * Write data for key into cache
  207. *
  208. * @param string $key Identifier for the data
  209. * @param mixed $value Data to be cached - anything except a resource
  210. * @param string $config Optional - string configuration name
  211. * @return boolean True if the data was successfully cached, false on failure
  212. * @access public
  213. * @static
  214. */
  215. function write($key, $value, $config = null) {
  216. $_this =& Cache::getInstance();
  217. if (is_array($config)) {
  218. extract($config);
  219. } else if ($config && (is_numeric($config) || is_numeric($config[0]) || (isset($config[1]) && is_numeric($config[1])))) {
  220. $config = null;
  221. }
  222. if ($config && isset($_this->__config[$config])) {
  223. $settings = $_this->set($_this->__config[$config]);
  224. } else {
  225. $settings = $_this->settings();
  226. }
  227. if (empty($settings)) {
  228. return null;
  229. }
  230. extract($settings);
  231. if (!$_this->isInitialized($engine)) {
  232. return false;
  233. }
  234. if (!$key = $_this->_Engine[$engine]->key($key)) {
  235. return false;
  236. }
  237. if (is_resource($value)) {
  238. return false;
  239. }
  240. if ($duration < 1) {
  241. return false;
  242. }
  243. $success = $_this->_Engine[$engine]->write($settings['prefix'] . $key, $value, $duration);
  244. $settings = $_this->set();
  245. return $success;
  246. }
  247. /**
  248. * Read a key from the cache
  249. *
  250. * @param string $key Identifier for the data
  251. * @param string $config name of the configuration to use
  252. * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
  253. * @access public
  254. * @static
  255. */
  256. function read($key, $config = null) {
  257. $_this =& Cache::getInstance();
  258. if (isset($_this->__config[$config])) {
  259. $settings = $_this->set($_this->__config[$config]);
  260. } else {
  261. $settings = $_this->settings();
  262. }
  263. if (empty($settings)) {
  264. return null;
  265. }
  266. extract($settings);
  267. if (!$_this->isInitialized($engine)) {
  268. return false;
  269. }
  270. if (!$key = $_this->_Engine[$engine]->key($key)) {
  271. return false;
  272. }
  273. $success = $_this->_Engine[$engine]->read($settings['prefix'] . $key);
  274. if ($config !== null && $config !== $_this->__name) {
  275. $settings = $_this->set();
  276. }
  277. return $success;
  278. }
  279. /**
  280. * Delete a key from the cache
  281. *
  282. * @param string $key Identifier for the data
  283. * @param string $config name of the configuration to use
  284. * @return boolean True if the value was succesfully deleted, false if it didn't exist or couldn't be removed
  285. * @access public
  286. * @static
  287. */
  288. function delete($key, $config = null) {
  289. $_this =& Cache::getInstance();
  290. if (isset($_this->__config[$config])) {
  291. $settings = $_this->set($_this->__config[$config]);
  292. } else {
  293. $settings = $_this->settings();
  294. }
  295. if (empty($settings)) {
  296. return null;
  297. }
  298. extract($settings);
  299. if (!$_this->isInitialized($engine)) {
  300. return false;
  301. }
  302. if (!$key = $_this->_Engine[$engine]->key($key)) {
  303. return false;
  304. }
  305. $success = $_this->_Engine[$engine]->delete($settings['prefix'] . $key);
  306. $settings = $_this->set();
  307. return $success;
  308. }
  309. /**
  310. * Delete all keys from the cache
  311. *
  312. * @param boolean $check if true will check expiration, otherwise delete all
  313. * @param string $config name of the configuration to use
  314. * @return boolean True if the cache was succesfully cleared, false otherwise
  315. * @access public
  316. * @static
  317. */
  318. function clear($check = false, $config = null) {
  319. $_this =& Cache::getInstance();
  320. if (isset($_this->__config[$config])) {
  321. $settings = $_this->set($_this->__config[$config]);
  322. } else {
  323. $settings = $_this->settings();
  324. }
  325. if (empty($settings)) {
  326. return null;
  327. }
  328. extract($settings);
  329. if (isset($engine) && !$_this->isInitialized($engine)) {
  330. return false;
  331. }
  332. $success = $_this->_Engine[$engine]->clear($check);
  333. $settings = $_this->set();
  334. return $success;
  335. }
  336. /**
  337. * Check if Cache has initialized a working storage engine
  338. *
  339. * @param string $engine Name of the engine
  340. * @param string $config Name of the configuration setting
  341. * @return bool
  342. * @access public
  343. * @static
  344. */
  345. function isInitialized($engine = null) {
  346. if (Configure::read('Cache.disable')) {
  347. return false;
  348. }
  349. $_this =& Cache::getInstance();
  350. if (!$engine && isset($_this->__config[$_this->__name]['engine'])) {
  351. $engine = $_this->__config[$_this->__name]['engine'];
  352. }
  353. return isset($_this->_Engine[$engine]);
  354. }
  355. /**
  356. * Return the settings for current cache engine
  357. *
  358. * @param string $engine Name of the engine
  359. * @return array list of settings for this engine
  360. * @access public
  361. * @static
  362. */
  363. function settings($engine = null) {
  364. $_this =& Cache::getInstance();
  365. if (!$engine && isset($_this->__config[$_this->__name]['engine'])) {
  366. $engine = $_this->__config[$_this->__name]['engine'];
  367. }
  368. if (isset($_this->_Engine[$engine]) && !is_null($_this->_Engine[$engine])) {
  369. return $_this->_Engine[$engine]->settings();
  370. }
  371. return array();
  372. }
  373. }
  374. /**
  375. * Storage engine for CakePHP caching
  376. *
  377. * @package cake
  378. * @subpackage cake.cake.libs
  379. */
  380. class CacheEngine extends Object {
  381. /**
  382. * settings of current engine instance
  383. *
  384. * @var int
  385. * @access public
  386. */
  387. var $settings = array();
  388. /**
  389. * Iitialize the cache engine
  390. *
  391. * Called automatically by the cache frontend
  392. *
  393. * @param array $params Associative array of parameters for the engine
  394. * @return boolean True if the engine has been succesfully initialized, false if not
  395. * @access public
  396. */
  397. function init($settings = array()) {
  398. $this->settings = array_merge(array('prefix' => 'cake_', 'duration'=> 3600, 'probability'=> 100), $this->settings, $settings);
  399. if (!is_numeric($this->settings['duration'])) {
  400. $this->settings['duration'] = strtotime($this->settings['duration']) - time();
  401. }
  402. return true;
  403. }
  404. /**
  405. * Garbage collection
  406. *
  407. * Permanently remove all expired and deleted data
  408. *
  409. * @access public
  410. */
  411. function gc() {
  412. }
  413. /**
  414. * Write value for a key into cache
  415. *
  416. * @param string $key Identifier for the data
  417. * @param mixed $value Data to be cached
  418. * @param mixed $duration How long to cache the data, in seconds
  419. * @return boolean True if the data was succesfully cached, false on failure
  420. * @access public
  421. */
  422. function write($key, &$value, $duration) {
  423. trigger_error(sprintf(__('Method write() not implemented in %s', true), get_class($this)), E_USER_ERROR);
  424. }
  425. /**
  426. * Read a key from the cache
  427. *
  428. * @param string $key Identifier for the data
  429. * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
  430. * @access public
  431. */
  432. function read($key) {
  433. trigger_error(sprintf(__('Method read() not implemented in %s', true), get_class($this)), E_USER_ERROR);
  434. }
  435. /**
  436. * Delete a key from the cache
  437. *
  438. * @param string $key Identifier for the data
  439. * @return boolean True if the value was succesfully deleted, false if it didn't exist or couldn't be removed
  440. * @access public
  441. */
  442. function delete($key) {
  443. }
  444. /**
  445. * Delete all keys from the cache
  446. *
  447. * @param boolean $check if true will check expiration, otherwise delete all
  448. * @return boolean True if the cache was succesfully cleared, false otherwise
  449. * @access public
  450. */
  451. function clear($check) {
  452. }
  453. /**
  454. * Cache Engine settings
  455. *
  456. * @return array settings
  457. * @access public
  458. */
  459. function settings() {
  460. return $this->settings;
  461. }
  462. /**
  463. * generates a safe key
  464. *
  465. * @param string $key the key passed over
  466. * @return mixed string $key or false
  467. * @access public
  468. */
  469. function key($key) {
  470. if (empty($key)) {
  471. return false;
  472. }
  473. $key = Inflector::underscore(str_replace(array(DS, '/', '.'), '_', strval($key)));
  474. return $key;
  475. }
  476. }
  477. ?>