CacheEngine.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. $settings += $this->settings + array(
  44. 'prefix' => 'cake_',
  45. 'duration' => 3600,
  46. 'probability' => 100,
  47. 'groups' => array()
  48. );
  49. $this->settings = $settings;
  50. if (!empty($this->settings['groups'])) {
  51. sort($this->settings['groups']);
  52. $this->_groupPrefix = str_repeat('%s_', count($this->settings['groups']));
  53. }
  54. if (!is_numeric($this->settings['duration'])) {
  55. $this->settings['duration'] = strtotime($this->settings['duration']) - time();
  56. }
  57. return true;
  58. }
  59. /**
  60. * Garbage collection
  61. *
  62. * Permanently remove all expired and deleted data
  63. *
  64. * @param integer $expires [optional] An expires timestamp, invalidataing all data before.
  65. * @return void
  66. */
  67. public function gc($expires = null) {
  68. }
  69. /**
  70. * Write value for a key into cache
  71. *
  72. * @param string $key Identifier for the data
  73. * @param mixed $value Data to be cached
  74. * @param integer $duration How long to cache for.
  75. * @return boolean True if the data was successfully cached, false on failure
  76. */
  77. abstract public function write($key, $value, $duration);
  78. /**
  79. * Read a key from the cache
  80. *
  81. * @param string $key Identifier for the data
  82. * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
  83. */
  84. abstract public function read($key);
  85. /**
  86. * Increment a number under the key and return incremented value
  87. *
  88. * @param string $key Identifier for the data
  89. * @param integer $offset How much to add
  90. * @return New incremented value, false otherwise
  91. */
  92. abstract public function increment($key, $offset = 1);
  93. /**
  94. * Decrement a number under the key and return decremented value
  95. *
  96. * @param string $key Identifier for the data
  97. * @param integer $offset How much to subtract
  98. * @return New incremented value, false otherwise
  99. */
  100. abstract public function decrement($key, $offset = 1);
  101. /**
  102. * Delete a key from the cache
  103. *
  104. * @param string $key Identifier for the data
  105. * @return boolean True if the value was successfully deleted, false if it didn't exist or couldn't be removed
  106. */
  107. abstract public function delete($key);
  108. /**
  109. * Delete all keys from the cache
  110. *
  111. * @param boolean $check if true will check expiration, otherwise delete all
  112. * @return boolean True if the cache was successfully cleared, false otherwise
  113. */
  114. abstract public function clear($check);
  115. /**
  116. * Clears all values belonging to a group. Is upt to the implementing engine
  117. * to decide whether actually deete the keys or just simulate it to acheive
  118. * the same result.
  119. *
  120. * @param string $groups name of the group to be cleared
  121. * @return boolean
  122. */
  123. public function clearGroup($group) {
  124. return false;
  125. }
  126. /**
  127. * Does whatever initialization for each group is required
  128. * and returns the `group value` for each of them, this is
  129. * the token representing each group in the cache key
  130. *
  131. * @return array
  132. */
  133. public function groups() {
  134. return $this->settings['groups'];
  135. }
  136. /**
  137. * Cache Engine settings
  138. *
  139. * @return array settings
  140. */
  141. public function settings() {
  142. return $this->settings;
  143. }
  144. /**
  145. * Generates a safe key for use with cache engine storage engines.
  146. *
  147. * @param string $key the key passed over
  148. * @return mixed string $key or false
  149. */
  150. public function key($key) {
  151. if (empty($key)) {
  152. return false;
  153. }
  154. $prefix = '';
  155. if (!empty($this->_groupPrefix)) {
  156. $prefix = vsprintf($this->_groupPrefix, $this->groups());
  157. }
  158. $key = preg_replace('/[\s]+/', '_', strtolower(trim(str_replace(array(DS, '/', '.'), '_', strval($key)))));
  159. return $prefix . $key;
  160. }
  161. }