ApcEngine.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 1.2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Cache\Engine;
  16. use APCIterator;
  17. use Cake\Cache\CacheEngine;
  18. use Cake\Utility\Inflector;
  19. /**
  20. * APC storage engine for cache
  21. *
  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 = [];
  31. /**
  32. * Initialize the Cache Engine
  33. *
  34. * Called automatically by the cache frontend
  35. *
  36. * @param array $config array of setting for the engine
  37. * @return bool True if the engine has been successfully initialized, false if not
  38. */
  39. public function init(array $config = []) {
  40. if (!isset($config['prefix'])) {
  41. $config['prefix'] = Inflector::slug(APP_DIR) . '_';
  42. }
  43. parent::init($config);
  44. return function_exists('apc_dec');
  45. }
  46. /**
  47. * Write data for key into cache
  48. *
  49. * @param string $key Identifier for the data
  50. * @param mixed $value Data to be cached
  51. * @return bool True if the data was successfully cached, false on failure
  52. */
  53. public function write($key, $value) {
  54. $key = $this->_key($key);
  55. $expires = 0;
  56. $duration = $this->_config['duration'];
  57. if ($duration) {
  58. $expires = time() + $duration;
  59. }
  60. apc_store($key . '_expires', $expires, $duration);
  61. return apc_store($key, $value, $duration);
  62. }
  63. /**
  64. * Read a key from the cache
  65. *
  66. * @param string $key Identifier for the data
  67. * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
  68. */
  69. public function read($key) {
  70. $key = $this->_key($key);
  71. $time = time();
  72. $cachetime = (int)apc_fetch($key . '_expires');
  73. if ($cachetime !== 0 && ($cachetime < $time || ($time + $this->_config['duration']) < $cachetime)) {
  74. return false;
  75. }
  76. return apc_fetch($key);
  77. }
  78. /**
  79. * Increments the value of an integer cached key
  80. *
  81. * @param string $key Identifier for the data
  82. * @param int $offset How much to increment
  83. * @return bool|int New incremented value, false otherwise
  84. */
  85. public function increment($key, $offset = 1) {
  86. $key = $this->_key($key);
  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 bool|int New decremented value, false otherwise
  95. */
  96. public function decrement($key, $offset = 1) {
  97. $key = $this->_key($key);
  98. return apc_dec($key, $offset);
  99. }
  100. /**
  101. * Delete a key from the cache
  102. *
  103. * @param string $key Identifier for the data
  104. * @return bool True if the value was successfully deleted, false if it didn't exist or couldn't be removed
  105. */
  106. public function delete($key) {
  107. $key = $this->_key($key);
  108. return apc_delete($key);
  109. }
  110. /**
  111. * Delete all keys from the cache. This will clear every cache config using APC.
  112. *
  113. * @param bool $check If true, nothing will be cleared, as entries are removed
  114. * from APC as they expired. This flag is really only used by FileEngine.
  115. * @return bool True Returns true.
  116. */
  117. public function clear($check) {
  118. if ($check) {
  119. return true;
  120. }
  121. if (class_exists('APCIterator', false)) {
  122. $iterator = new APCIterator(
  123. 'user',
  124. '/^' . preg_quote($this->_config['prefix'], '/') . '/',
  125. APC_ITER_NONE
  126. );
  127. apc_delete($iterator);
  128. return true;
  129. }
  130. $cache = apc_cache_info('user');
  131. foreach ($cache['cache_list'] as $key) {
  132. if (strpos($key['info'], $this->_config['prefix']) === 0) {
  133. apc_delete($key['info']);
  134. }
  135. }
  136. return true;
  137. }
  138. /**
  139. * Returns the `group value` for each of the configured groups
  140. * If the group initial value was not found, then it initializes
  141. * the group accordingly.
  142. *
  143. * @return array
  144. */
  145. public function groups() {
  146. if (empty($this->_compiledGroupNames)) {
  147. foreach ($this->_config['groups'] as $group) {
  148. $this->_compiledGroupNames[] = $this->_config['prefix'] . $group;
  149. }
  150. }
  151. $groups = apc_fetch($this->_compiledGroupNames);
  152. if (count($groups) !== count($this->_config['groups'])) {
  153. foreach ($this->_compiledGroupNames as $group) {
  154. if (!isset($groups[$group])) {
  155. apc_store($group, 1);
  156. $groups[$group] = 1;
  157. }
  158. }
  159. ksort($groups);
  160. }
  161. $result = [];
  162. $groups = array_values($groups);
  163. foreach ($this->_config['groups'] as $i => $group) {
  164. $result[] = $group . $groups[$i];
  165. }
  166. return $result;
  167. }
  168. /**
  169. * Increments the group value to simulate deletion of all keys under a group
  170. * old values will remain in storage until they expire.
  171. *
  172. * @param string $group The group to clear.
  173. * @return bool success
  174. */
  175. public function clearGroup($group) {
  176. apc_inc($this->_config['prefix'] . $group, 1, $success);
  177. return $success;
  178. }
  179. }