PageRenderTime 27ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/cakephp/vendors/cake/libs/cache.php

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