Base for a static organization website

ApcEngine.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * APC storage engine for cache.
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @package Cake.Cache.Engine
  15. * @since CakePHP(tm) v 1.2.0.4933
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. /**
  19. * APC storage engine for cache
  20. *
  21. * @package Cake.Cache.Engine
  22. */
  23. class ApcEngine extends CacheEngine {
  24. /**
  25. * Contains the compiled group names
  26. * (prefixed with the global configuration prefix)
  27. *
  28. * @var array
  29. */
  30. protected $_compiledGroupNames = array();
  31. /**
  32. * Initialize the Cache Engine
  33. *
  34. * Called automatically by the cache frontend
  35. * To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array());
  36. *
  37. * @param array $settings array of setting for the engine
  38. * @return bool True if the engine has been successfully initialized, false if not
  39. * @see CacheEngine::__defaults
  40. */
  41. public function init($settings = array()) {
  42. if (!isset($settings['prefix'])) {
  43. $settings['prefix'] = Inflector::slug(APP_DIR) . '_';
  44. }
  45. $settings += array('engine' => 'Apc');
  46. parent::init($settings);
  47. return function_exists('apc_dec');
  48. }
  49. /**
  50. * Write data for key into cache
  51. *
  52. * @param string $key Identifier for the data
  53. * @param mixed $value Data to be cached
  54. * @param int $duration How long to cache the data, in seconds
  55. * @return bool True if the data was successfully cached, false on failure
  56. */
  57. public function write($key, $value, $duration) {
  58. $expires = 0;
  59. if ($duration) {
  60. $expires = time() + $duration;
  61. }
  62. apc_store($key . '_expires', $expires, $duration);
  63. return apc_store($key, $value, $duration);
  64. }
  65. /**
  66. * Read a key from the cache
  67. *
  68. * @param string $key Identifier for the data
  69. * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
  70. */
  71. public function read($key) {
  72. $time = time();
  73. $cachetime = (int)apc_fetch($key . '_expires');
  74. if ($cachetime !== 0 && ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime)) {
  75. return false;
  76. }
  77. return apc_fetch($key);
  78. }
  79. /**
  80. * Increments the value of an integer cached key
  81. *
  82. * @param string $key Identifier for the data
  83. * @param int $offset How much to increment
  84. * @return New incremented value, false otherwise
  85. */
  86. public function increment($key, $offset = 1) {
  87. return apc_inc($key, $offset);
  88. }
  89. /**
  90. * Decrements the value of an integer cached key
  91. *
  92. * @param string $key Identifier for the data
  93. * @param int $offset How much to subtract
  94. * @return New decremented value, false otherwise
  95. */
  96. public function decrement($key, $offset = 1) {
  97. return apc_dec($key, $offset);
  98. }
  99. /**
  100. * Delete a key from the cache
  101. *
  102. * @param string $key Identifier for the data
  103. * @return bool True if the value was successfully deleted, false if it didn't exist or couldn't be removed
  104. */
  105. public function delete($key) {
  106. return apc_delete($key);
  107. }
  108. /**
  109. * Delete all keys from the cache. This will clear every cache config using APC.
  110. *
  111. * @param bool $check If true, nothing will be cleared, as entries are removed
  112. * from APC as they expired. This flag is really only used by FileEngine.
  113. * @return bool True Returns true.
  114. */
  115. public function clear($check) {
  116. if ($check) {
  117. return true;
  118. }
  119. if (class_exists('APCIterator', false)) {
  120. $iterator = new APCIterator(
  121. 'user',
  122. '/^' . preg_quote($this->settings['prefix'], '/') . '/',
  123. APC_ITER_NONE
  124. );
  125. apc_delete($iterator);
  126. return true;
  127. }
  128. $cache = apc_cache_info('user');
  129. foreach ($cache['cache_list'] as $key) {
  130. if (strpos($key['info'], $this->settings['prefix']) === 0) {
  131. apc_delete($key['info']);
  132. }
  133. }
  134. return true;
  135. }
  136. /**
  137. * Returns the `group value` for each of the configured groups
  138. * If the group initial value was not found, then it initializes
  139. * the group accordingly.
  140. *
  141. * @return array
  142. */
  143. public function groups() {
  144. if (empty($this->_compiledGroupNames)) {
  145. foreach ($this->settings['groups'] as $group) {
  146. $this->_compiledGroupNames[] = $this->settings['prefix'] . $group;
  147. }
  148. }
  149. $groups = apc_fetch($this->_compiledGroupNames);
  150. if (count($groups) !== count($this->settings['groups'])) {
  151. foreach ($this->_compiledGroupNames as $group) {
  152. if (!isset($groups[$group])) {
  153. apc_store($group, 1);
  154. $groups[$group] = 1;
  155. }
  156. }
  157. ksort($groups);
  158. }
  159. $result = array();
  160. $groups = array_values($groups);
  161. foreach ($this->settings['groups'] as $i => $group) {
  162. $result[] = $group . $groups[$i];
  163. }
  164. return $result;
  165. }
  166. /**
  167. * Increments the group value to simulate deletion of all keys under a group
  168. * old values will remain in storage until they expire.
  169. *
  170. * @param string $group The group to clear.
  171. * @return bool success
  172. */
  173. public function clearGroup($group) {
  174. apc_inc($this->settings['prefix'] . $group, 1, $success);
  175. return $success;
  176. }
  177. }