ApcEngine.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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.libs.cache
  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.libs.cache
  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_cache_info');
  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. $expires = time() + $duration;
  50. apc_store($key.'_expires', $expires, $duration);
  51. return apc_store($key, $value, $duration);
  52. }
  53. /**
  54. * Read a key from the cache
  55. *
  56. * @param string $key Identifier for the data
  57. * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
  58. */
  59. public function read($key) {
  60. $time = time();
  61. $cachetime = intval(apc_fetch($key.'_expires'));
  62. if ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime) {
  63. return false;
  64. }
  65. return apc_fetch($key);
  66. }
  67. /**
  68. * Increments the value of an integer cached key
  69. *
  70. * @param string $key Identifier for the data
  71. * @param integer $offset How much to increment
  72. * @return New incremented value, false otherwise
  73. */
  74. public function increment($key, $offset = 1) {
  75. return apc_inc($key, $offset);
  76. }
  77. /**
  78. * Decrements the value of an integer cached key
  79. *
  80. * @param string $key Identifier for the data
  81. * @param integer $offset How much to subtract
  82. * @return New decremented value, false otherwise
  83. */
  84. public function decrement($key, $offset = 1) {
  85. return apc_dec($key, $offset);
  86. }
  87. /**
  88. * Delete a key from the cache
  89. *
  90. * @param string $key Identifier for the data
  91. * @return boolean True if the value was successfully deleted, false if it didn't exist or couldn't be removed
  92. */
  93. public function delete($key) {
  94. return apc_delete($key);
  95. }
  96. /**
  97. * Delete all keys from the cache. This will clear every cache config using APC.
  98. *
  99. * @return boolean True if the cache was successfully cleared, false otherwise
  100. */
  101. public function clear($check) {
  102. return apc_clear_cache('user');
  103. }
  104. }