ApcEngine.php 5.0 KB

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