/lib/Cake/Cache/CacheEngine.php

https://gitlab.com/grlopez90/servipro · PHP · 190 lines · 52 code · 18 blank · 120 comment · 4 complexity · e937486276bc362ced43e992ef5447b5 MD5 · raw file

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