CacheEngine.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @package Cake.Cache
  12. * @since CakePHP(tm) v 1.2.0.4933
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. /**
  16. * Storage engine for CakePHP caching
  17. *
  18. * @package Cake.Cache
  19. */
  20. abstract class CacheEngine {
  21. /**
  22. * Settings of current engine instance
  23. *
  24. * @var array
  25. */
  26. public $settings = array();
  27. /**
  28. * Contains the compiled string with all groups
  29. * prefixes to be prepeded to every key in this cache engine
  30. *
  31. * @var string
  32. **/
  33. protected $_groupPrefix = null;
  34. /**
  35. * Initialize the cache engine
  36. *
  37. * Called automatically by the cache frontend
  38. *
  39. * @param array $settings Associative array of parameters for the engine
  40. * @return boolean True if the engine has been successfully initialized, false if not
  41. */
  42. public function init($settings = array()) {
  43. $this->settings = array_merge(
  44. array('prefix' => 'cake_', 'duration' => 3600, 'probability' => 100, 'groups' => array()),
  45. $this->settings,
  46. $settings
  47. );
  48. if (!empty($this->settings['groups'])) {
  49. sort($this->settings['groups']);
  50. $this->_groupPrefix = str_repeat('%s_', count($this->settings['groups']));
  51. }
  52. if (!is_numeric($this->settings['duration'])) {
  53. $this->settings['duration'] = strtotime($this->settings['duration']) - time();
  54. }
  55. return true;
  56. }
  57. /**
  58. * Garbage collection
  59. *
  60. * Permanently remove all expired and deleted data
  61. *
  62. * @param integer $expires [optional] An expires timestamp, invalidataing all data before.
  63. * @return void
  64. */
  65. public function gc($expires = null) {
  66. }
  67. /**
  68. * Write value for a key into cache
  69. *
  70. * @param string $key Identifier for the data
  71. * @param mixed $value Data to be cached
  72. * @param mixed $duration How long to cache for.
  73. * @return boolean True if the data was successfully cached, false on failure
  74. */
  75. abstract public function write($key, $value, $duration);
  76. /**
  77. * Read a key from the cache
  78. *
  79. * @param string $key Identifier for the data
  80. * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
  81. */
  82. abstract public function read($key);
  83. /**
  84. * Increment a number under the key and return incremented value
  85. *
  86. * @param string $key Identifier for the data
  87. * @param integer $offset How much to add
  88. * @return New incremented value, false otherwise
  89. */
  90. abstract public function increment($key, $offset = 1);
  91. /**
  92. * Decrement a number under the key and return decremented value
  93. *
  94. * @param string $key Identifier for the data
  95. * @param integer $offset How much to subtract
  96. * @return New incremented value, false otherwise
  97. */
  98. abstract public function decrement($key, $offset = 1);
  99. /**
  100. * Delete a key from the cache
  101. *
  102. * @param string $key Identifier for the data
  103. * @return boolean True if the value was successfully deleted, false if it didn't exist or couldn't be removed
  104. */
  105. abstract public function delete($key);
  106. /**
  107. * Delete all keys from the cache
  108. *
  109. * @param boolean $check if true will check expiration, otherwise delete all
  110. * @return boolean True if the cache was successfully cleared, false otherwise
  111. */
  112. abstract public function clear($check);
  113. /**
  114. * Clears all values belonging to a group. Is upt to the implementing engine
  115. * to decide whether actually deete the keys or just simulate it to acheive
  116. * the same result.
  117. *
  118. * @param string $groups name of the group to be cleared
  119. * @return boolean
  120. */
  121. public function clearGroup($group) {
  122. return false;
  123. }
  124. /**
  125. * Does whatever initialization for each group is required
  126. * and returns the `group value` for each of them, this is
  127. * the token representing each group in the cache key
  128. *
  129. * @return array
  130. */
  131. public function groups() {
  132. return $this->settings['groups'];
  133. }
  134. /**
  135. * Cache Engine settings
  136. *
  137. * @return array settings
  138. */
  139. public function settings() {
  140. return $this->settings;
  141. }
  142. /**
  143. * Generates a safe key for use with cache engine storage engines.
  144. *
  145. * @param string $key the key passed over
  146. * @return mixed string $key or false
  147. */
  148. public function key($key) {
  149. if (empty($key)) {
  150. return false;
  151. }
  152. $prefix = '';
  153. if (!empty($this->_groupPrefix)) {
  154. $prefix = vsprintf($this->_groupPrefix, $this->groups());
  155. }
  156. $key = Inflector::underscore(str_replace(array(DS, '/', '.'), '_', strval($key)));
  157. return $prefix . $key;
  158. }
  159. }