Base for a static organization website

CacheEngine.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. * Read a key from the cache
  81. *
  82. * @param string $key Identifier for the data
  83. * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
  84. */
  85. abstract public function read($key);
  86. /**
  87. * Increment a number under the key and return incremented value
  88. *
  89. * @param string $key Identifier for the data
  90. * @param int $offset How much to add
  91. * @return New incremented value, false otherwise
  92. */
  93. abstract public function increment($key, $offset = 1);
  94. /**
  95. * Decrement a number under the key and return decremented value
  96. *
  97. * @param string $key Identifier for the data
  98. * @param int $offset How much to subtract
  99. * @return New incremented value, false otherwise
  100. */
  101. abstract public function decrement($key, $offset = 1);
  102. /**
  103. * Delete a key from the cache
  104. *
  105. * @param string $key Identifier for the data
  106. * @return bool True if the value was successfully deleted, false if it didn't exist or couldn't be removed
  107. */
  108. abstract public function delete($key);
  109. /**
  110. * Delete all keys from the cache
  111. *
  112. * @param bool $check if true will check expiration, otherwise delete all
  113. * @return bool True if the cache was successfully cleared, false otherwise
  114. */
  115. abstract public function clear($check);
  116. /**
  117. * Clears all values belonging to a group. Is up to the implementing engine
  118. * to decide whether actually delete the keys or just simulate it to achieve
  119. * the same result.
  120. *
  121. * @param string $group name of the group to be cleared
  122. * @return bool
  123. */
  124. public function clearGroup($group) {
  125. return false;
  126. }
  127. /**
  128. * Does whatever initialization for each group is required
  129. * and returns the `group value` for each of them, this is
  130. * the token representing each group in the cache key
  131. *
  132. * @return array
  133. */
  134. public function groups() {
  135. return $this->settings['groups'];
  136. }
  137. /**
  138. * Cache Engine settings
  139. *
  140. * @return array settings
  141. */
  142. public function settings() {
  143. return $this->settings;
  144. }
  145. /**
  146. * Generates a safe key for use with cache engine storage engines.
  147. *
  148. * @param string $key the key passed over
  149. * @return mixed string $key or false
  150. */
  151. public function key($key) {
  152. if (empty($key)) {
  153. return false;
  154. }
  155. $prefix = '';
  156. if (!empty($this->_groupPrefix)) {
  157. $prefix = vsprintf($this->_groupPrefix, $this->groups());
  158. }
  159. $key = preg_replace('/[\s]+/', '_', strtolower(trim(str_replace(array(DS, '/', '.'), '_', strval($key)))));
  160. return $prefix . $key;
  161. }
  162. }