CEAcceleratorCache.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * CEAcceleratorCache class file
  4. *
  5. * @author Steffen Dietz <steffo.dietz[at]googlemail[dot]com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CEAcceleratorCache implements a cache application module based on {@link http://eaccelerator.net/ eaccelerator}.
  12. *
  13. * To use this application component, the eAccelerator PHP extension must be loaded.
  14. *
  15. * See {@link CCache} manual for common cache operations that are supported by CEAccelerator.
  16. *
  17. * Please note that as of v0.9.6, eAccelerator no longer supports data caching.
  18. * This means if you still want to use this component, your eAccelerator should be of 0.9.5.x or lower version.
  19. *
  20. * @author Steffen Dietz <steffo.dietz[at]googlemail[dot]com>
  21. * @package system.caching
  22. */
  23. class CEAcceleratorCache extends CCache
  24. {
  25. /**
  26. * Initializes this application component.
  27. * This method is required by the {@link IApplicationComponent} interface.
  28. * It checks the availability of eAccelerator.
  29. * @throws CException if eAccelerator extension is not loaded, is disabled or the cache functions are not compiled in.
  30. */
  31. public function init()
  32. {
  33. parent::init();
  34. if(!function_exists('eaccelerator_get'))
  35. throw new CException(Yii::t('yii','CEAcceleratorCache requires PHP eAccelerator extension to be loaded, enabled or compiled with the "--with-eaccelerator-shared-memory" option.'));
  36. }
  37. /**
  38. * Retrieves a value from cache with a specified key.
  39. * This is the implementation of the method declared in the parent class.
  40. * @param string $key a unique key identifying the cached value
  41. * @return string|boolean the value stored in cache, false if the value is not in the cache or expired.
  42. */
  43. protected function getValue($key)
  44. {
  45. $result = eaccelerator_get($key);
  46. return $result !== NULL ? $result : false;
  47. }
  48. /**
  49. * Stores a value identified by a key in cache.
  50. * This is the implementation of the method declared in the parent class.
  51. *
  52. * @param string $key the key identifying the value to be cached
  53. * @param string $value the value to be cached
  54. * @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
  55. * @return boolean true if the value is successfully stored into cache, false otherwise
  56. */
  57. protected function setValue($key,$value,$expire)
  58. {
  59. return eaccelerator_put($key,$value,$expire);
  60. }
  61. /**
  62. * Stores a value identified by a key into cache if the cache does not contain this key.
  63. * This is the implementation of the method declared in the parent class.
  64. *
  65. * @param string $key the key identifying the value to be cached
  66. * @param string $value the value to be cached
  67. * @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
  68. * @return boolean true if the value is successfully stored into cache, false otherwise
  69. */
  70. protected function addValue($key,$value,$expire)
  71. {
  72. return (NULL === eaccelerator_get($key)) ? $this->setValue($key,$value,$expire) : false;
  73. }
  74. /**
  75. * Deletes a value with the specified key from cache
  76. * This is the implementation of the method declared in the parent class.
  77. * @param string $key the key of the value to be deleted
  78. * @return boolean if no error happens during deletion
  79. */
  80. protected function deleteValue($key)
  81. {
  82. return eaccelerator_rm($key);
  83. }
  84. /**
  85. * Deletes all values from cache.
  86. * This is the implementation of the method declared in the parent class.
  87. * @return boolean whether the flush operation was successful.
  88. * @since 1.1.5
  89. */
  90. protected function flushValues()
  91. {
  92. // first, remove expired content from cache
  93. eaccelerator_gc();
  94. // now, remove leftover cache-keys
  95. $keys = eaccelerator_list_keys();
  96. foreach($keys as $key)
  97. $this->deleteValue(substr($key['name'], 1));
  98. return true;
  99. }
  100. }