ZendDataCache.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\caching;
  8. /**
  9. * ZendDataCache provides Zend data caching in terms of an application component.
  10. *
  11. * To use this application component, the [Zend Data Cache PHP extension](http://www.zend.com/en/products/server/)
  12. * must be loaded.
  13. *
  14. * See [[Cache]] for common cache operations that ZendDataCache supports.
  15. *
  16. * For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview).
  17. *
  18. * @author Qiang Xue <qiang.xue@gmail.com>
  19. * @since 2.0
  20. */
  21. class ZendDataCache extends Cache
  22. {
  23. /**
  24. * Retrieves a value from cache with a specified key.
  25. * This is the implementation of the method declared in the parent class.
  26. * @param string $key a unique key identifying the cached value
  27. * @return mixed|false the value stored in cache, false if the value is not in the cache or expired.
  28. */
  29. protected function getValue($key)
  30. {
  31. $result = zend_shm_cache_fetch($key);
  32. return $result === null ? false : $result;
  33. }
  34. /**
  35. * Stores a value identified by a key in cache.
  36. * This is the implementation of the method declared in the parent class.
  37. *
  38. * @param string $key the key identifying the value to be cached
  39. * @param mixed $value the value to be cached. Most often it's a string. If you have disabled [[serializer]],
  40. * it could be something else.
  41. * @param int $duration the number of seconds in which the cached value will expire. 0 means never expire.
  42. * @return bool true if the value is successfully stored into cache, false otherwise
  43. */
  44. protected function setValue($key, $value, $duration)
  45. {
  46. return zend_shm_cache_store($key, $value, $duration);
  47. }
  48. /**
  49. * Stores a value identified by a key into cache if the cache does not contain this key.
  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 mixed $value the value to be cached. Most often it's a string. If you have disabled [[serializer]],
  54. * it could be something else.
  55. * @param int $duration the number of seconds in which the cached value will expire. 0 means never expire.
  56. * @return bool true if the value is successfully stored into cache, false otherwise
  57. */
  58. protected function addValue($key, $value, $duration)
  59. {
  60. return zend_shm_cache_fetch($key) === null ? $this->setValue($key, $value, $duration) : false;
  61. }
  62. /**
  63. * Deletes a value with the specified key from cache
  64. * This is the implementation of the method declared in the parent class.
  65. * @param string $key the key of the value to be deleted
  66. * @return bool if no error happens during deletion
  67. */
  68. protected function deleteValue($key)
  69. {
  70. return zend_shm_cache_delete($key);
  71. }
  72. /**
  73. * Deletes all values from cache.
  74. * This is the implementation of the method declared in the parent class.
  75. * @return bool whether the flush operation was successful.
  76. */
  77. protected function flushValues()
  78. {
  79. return zend_shm_cache_clear();
  80. }
  81. }