CacheEngine.php 4.5 KB

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