ApcEngine.php 5.0 KB

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