ApcEngine.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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-2011, 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-2011, 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. * Initialize the Cache Engine
  28. *
  29. * Called automatically by the cache frontend
  30. * To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array());
  31. *
  32. * @param array $settings array of setting for the engine
  33. * @return boolean True if the engine has been successfully initialized, false if not
  34. * @see CacheEngine::__defaults
  35. */
  36. public function init($settings = array()) {
  37. parent::init(array_merge(array('engine' => 'Apc', 'prefix' => Inflector::slug(APP_DIR) . '_'), $settings));
  38. return function_exists('apc_dec');
  39. }
  40. /**
  41. * Write data for key into cache
  42. *
  43. * @param string $key Identifier for the data
  44. * @param mixed $value Data to be cached
  45. * @param integer $duration How long to cache the data, in seconds
  46. * @return boolean True if the data was successfully cached, false on failure
  47. */
  48. public function write($key, $value, $duration) {
  49. if ($duration == 0) {
  50. $expires = 0;
  51. } else {
  52. $expires = time() + $duration;
  53. }
  54. apc_store($key.'_expires', $expires, $duration);
  55. return apc_store($key, $value, $duration);
  56. }
  57. /**
  58. * Read a key from the cache
  59. *
  60. * @param string $key Identifier for the data
  61. * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
  62. */
  63. public function read($key) {
  64. $time = time();
  65. $cachetime = intval(apc_fetch($key.'_expires'));
  66. if ($cachetime !== 0 && ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime)) {
  67. return false;
  68. }
  69. return apc_fetch($key);
  70. }
  71. /**
  72. * Increments the value of an integer cached key
  73. *
  74. * @param string $key Identifier for the data
  75. * @param integer $offset How much to increment
  76. * @return New incremented value, false otherwise
  77. */
  78. public function increment($key, $offset = 1) {
  79. return apc_inc($key, $offset);
  80. }
  81. /**
  82. * Decrements the value of an integer cached key
  83. *
  84. * @param string $key Identifier for the data
  85. * @param integer $offset How much to subtract
  86. * @return New decremented value, false otherwise
  87. */
  88. public function decrement($key, $offset = 1) {
  89. return apc_dec($key, $offset);
  90. }
  91. /**
  92. * Delete a key from the cache
  93. *
  94. * @param string $key Identifier for the data
  95. * @return boolean True if the value was successfully deleted, false if it didn't exist or couldn't be removed
  96. */
  97. public function delete($key) {
  98. return apc_delete($key);
  99. }
  100. /**
  101. * Delete all keys from the cache. This will clear every cache config using APC.
  102. *
  103. * @param boolean $check If true, nothing will be cleared, as entries are removed
  104. * from APC as they expired. This flag is really only used by FileEngine.
  105. * @return boolean True Returns true.
  106. */
  107. public function clear($check) {
  108. if ($check) {
  109. return true;
  110. }
  111. $info = apc_cache_info('user');
  112. $cacheKeys = $info['cache_list'];
  113. unset($info);
  114. foreach ($cacheKeys as $key) {
  115. if (strpos($key['info'], $this->settings['prefix']) === 0) {
  116. apc_delete($key['info']);
  117. }
  118. }
  119. return true;
  120. }
  121. }