CZendDataCache.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * CZendDataCache 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. * CZendDataCache implements a cache application module based on the Zend Data Cache
  12. * delivered with {@link http://www.zend.com/en/products/server/ ZendServer}.
  13. *
  14. * To use this application component, the Zend Data Cache PHP extension must be loaded.
  15. *
  16. * See {@link CCache} manual for common cache operations that are supported by CZendDataCache.
  17. *
  18. * @author Steffen Dietz <steffo.dietz[at]googlemail[dot]com>
  19. * @package system.caching
  20. */
  21. class CZendDataCache extends CCache
  22. {
  23. /**
  24. * Initializes this application component.
  25. * This method is required by the {@link IApplicationComponent} interface.
  26. * It checks the availability of Zend Data Cache.
  27. * @throws CException if Zend Data Cache extension is not loaded.
  28. */
  29. public function init()
  30. {
  31. parent::init();
  32. if(!function_exists('zend_shm_cache_store'))
  33. throw new CException(Yii::t('yii','CZendDataCache requires PHP Zend Data Cache extension to be loaded.'));
  34. }
  35. /**
  36. * Retrieves a value from cache with a specified key.
  37. * This is the implementation of the method declared in the parent class.
  38. * @param string $key a unique key identifying the cached value
  39. * @return string|boolean the value stored in cache, false if the value is not in the cache or expired.
  40. */
  41. protected function getValue($key)
  42. {
  43. $result = zend_shm_cache_fetch($key);
  44. return $result !== NULL ? $result : false;
  45. }
  46. /**
  47. * Stores a value identified by a key in cache.
  48. * This is the implementation of the method declared in the parent class.
  49. *
  50. * @param string $key the key identifying the value to be cached
  51. * @param string $value the value to be cached
  52. * @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
  53. * @return boolean true if the value is successfully stored into cache, false otherwise
  54. */
  55. protected function setValue($key,$value,$expire)
  56. {
  57. return zend_shm_cache_store($key,$value,$expire);
  58. }
  59. /**
  60. * Stores a value identified by a key into cache if the cache does not contain this key.
  61. * This is the implementation of the method declared in the parent class.
  62. *
  63. * @param string $key the key identifying the value to be cached
  64. * @param string $value the value to be cached
  65. * @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
  66. * @return boolean true if the value is successfully stored into cache, false otherwise
  67. */
  68. protected function addValue($key,$value,$expire)
  69. {
  70. return (NULL === zend_shm_cache_fetch($key)) ? $this->setValue($key,$value,$expire) : false;
  71. }
  72. /**
  73. * Deletes a value with the specified key from cache
  74. * This is the implementation of the method declared in the parent class.
  75. * @param string $key the key of the value to be deleted
  76. * @return boolean if no error happens during deletion
  77. */
  78. protected function deleteValue($key)
  79. {
  80. return zend_shm_cache_delete($key);
  81. }
  82. /**
  83. * Deletes all values from cache.
  84. * This is the implementation of the method declared in the parent class.
  85. * @return boolean whether the flush operation was successful.
  86. * @since 1.1.5
  87. */
  88. protected function flushValues()
  89. {
  90. return zend_shm_cache_clear();
  91. }
  92. }