CacheEngine.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since CakePHP(tm) v 1.2.0.4933
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Cache;
  16. use Cake\Utility\Inflector;
  17. /**
  18. * Storage engine for CakePHP caching
  19. *
  20. */
  21. abstract class CacheEngine {
  22. /**
  23. * Runtime config
  24. *
  25. * This is the config of a particular instance
  26. *
  27. * @var array
  28. */
  29. protected $_config = [];
  30. /**
  31. * The default cache configuration is overriden in most cache adapters. These are
  32. * the keys that are common to all adapters. If overriden, this property is not used.
  33. *
  34. * - `duration` Specify how long items in this cache configuration last.
  35. * - `groups` List of groups or 'tags' associated to every key stored in this config.
  36. * handy for deleting a complete group from cache.
  37. * - `prefix` Prefix appended to all entries. Good for when you need to share a keyspace
  38. * with either another cache config or another application.
  39. * - `probability` Probability of hitting a cache gc cleanup. Setting to 0 will disable
  40. * cache::gc from ever being called automatically.
  41. *
  42. * @var array
  43. */
  44. protected $_defaultConfig = [
  45. 'duration' => 3600,
  46. 'groups' => [],
  47. 'prefix' => 'cake_',
  48. 'probability' => 100
  49. ];
  50. /**
  51. * Contains the compiled string with all groups
  52. * prefixes to be prepended to every key in this cache engine
  53. *
  54. * @var string
  55. */
  56. protected $_groupPrefix = null;
  57. /**
  58. * Initialize the cache engine
  59. *
  60. * Called automatically by the cache frontend. Merge the runtime config with the defaults
  61. * before use.
  62. *
  63. * @param array $config Associative array of parameters for the engine
  64. * @return boolean True if the engine has been successfully initialized, false if not
  65. */
  66. public function init($config = []) {
  67. $this->_config = $config + $this->_defaultConfig;
  68. if (!empty($this->_config['groups'])) {
  69. sort($this->_config['groups']);
  70. $this->_groupPrefix = str_repeat('%s_', count($this->_config['groups']));
  71. }
  72. if (!is_numeric($this->_config['duration'])) {
  73. $this->_config['duration'] = strtotime($this->_config['duration']) - time();
  74. }
  75. return true;
  76. }
  77. /**
  78. * Garbage collection
  79. *
  80. * Permanently remove all expired and deleted data
  81. *
  82. * @param integer $expires [optional] An expires timestamp, invalidating all data before.
  83. * @return void
  84. */
  85. public function gc($expires = null) {
  86. }
  87. /**
  88. * Write value for a key into cache
  89. *
  90. * @param string $key Identifier for the data
  91. * @param mixed $value Data to be cached
  92. * @return boolean True if the data was successfully cached, false on failure
  93. */
  94. abstract public function write($key, $value);
  95. /**
  96. * Read a key from the cache
  97. *
  98. * @param string $key Identifier for the data
  99. * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
  100. */
  101. abstract public function read($key);
  102. /**
  103. * Increment a number under the key and return incremented value
  104. *
  105. * @param string $key Identifier for the data
  106. * @param integer $offset How much to add
  107. * @return New incremented value, false otherwise
  108. */
  109. abstract public function increment($key, $offset = 1);
  110. /**
  111. * Decrement a number under the key and return decremented value
  112. *
  113. * @param string $key Identifier for the data
  114. * @param integer $offset How much to subtract
  115. * @return New incremented value, false otherwise
  116. */
  117. abstract public function decrement($key, $offset = 1);
  118. /**
  119. * Delete a key from the cache
  120. *
  121. * @param string $key Identifier for the data
  122. * @return boolean True if the value was successfully deleted, false if it didn't exist or couldn't be removed
  123. */
  124. abstract public function delete($key);
  125. /**
  126. * Delete all keys from the cache
  127. *
  128. * @param boolean $check if true will check expiration, otherwise delete all
  129. * @return boolean True if the cache was successfully cleared, false otherwise
  130. */
  131. abstract public function clear($check);
  132. /**
  133. * Clears all values belonging to a group. Is up to the implementing engine
  134. * to decide whether actually delete the keys or just simulate it to achieve
  135. * the same result.
  136. *
  137. * @param string $groups name of the group to be cleared
  138. * @return boolean
  139. */
  140. public function clearGroup($group) {
  141. return false;
  142. }
  143. /**
  144. * Does whatever initialization for each group is required
  145. * and returns the `group value` for each of them, this is
  146. * the token representing each group in the cache key
  147. *
  148. * @return array
  149. */
  150. public function groups() {
  151. return $this->_config['groups'];
  152. }
  153. /**
  154. * Cache Engine config
  155. *
  156. * @return array config
  157. */
  158. public function config() {
  159. return $this->_config;
  160. }
  161. /**
  162. * Generates a safe key for use with cache engine storage engines.
  163. *
  164. * @param string $key the key passed over
  165. * @return mixed string $key or false
  166. */
  167. public function key($key) {
  168. if (empty($key)) {
  169. return false;
  170. }
  171. $prefix = '';
  172. if (!empty($this->_groupPrefix)) {
  173. $prefix = vsprintf($this->_groupPrefix, $this->groups());
  174. }
  175. $key = preg_replace('/[\s]+/', '_', strtolower(trim(str_replace([DS, '/', '.'], '_', strval($key)))));
  176. return $prefix . $key;
  177. }
  178. /**
  179. * Generates a safe key, taking account of the configured key prefix
  180. *
  181. * @param string $key the key passed over
  182. * @return mixed string $key or false
  183. * @throws \InvalidArgumentException If key's value is empty
  184. */
  185. protected function _key($key) {
  186. $key = $this->key($key);
  187. if (!$key) {
  188. throw new \InvalidArgumentException('An empty value is not valid as a cache key');
  189. }
  190. return $this->_config['prefix'] . $key;
  191. }
  192. }