ApcEngine.php 4.8 KB

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