ARedisCache.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * A cache component that allows items to be cached using redis.
  4. * @author Charles Pick
  5. * @package packages.redis
  6. */
  7. class ARedisCache extends CCache {
  8. /**
  9. * Holds the redis connection
  10. * @var ARedisConnection
  11. */
  12. protected $_connection;
  13. /**
  14. * Sets the redis connection to use for caching
  15. * @param ARedisConnection|string $connection the redis connection, if a string is provided, it is presumed to be a the name of an applciation component
  16. */
  17. public function setConnection($connection)
  18. {
  19. if (is_string($connection)) {
  20. $connection = Yii::app()->{$connection};
  21. }
  22. $this->_connection = $connection;
  23. }
  24. /**
  25. * Gets the redis connection to use for caching
  26. * @return ARedisConnection
  27. */
  28. public function getConnection()
  29. {
  30. if ($this->_connection === null) {
  31. if (!isset(Yii::app()->redis)) {
  32. throw new CException("ARedisCache expects a 'redis' application component");
  33. }
  34. $this->_connection = Yii::app()->redis;
  35. }
  36. return $this->_connection;
  37. }
  38. /**
  39. * Retrieves a value from cache with a specified key.
  40. * This is the implementation of the method declared in the parent class.
  41. * @param string $key a unique key identifying the cached value
  42. * @return string the value stored in cache, false if the value is not in the cache or expired.
  43. */
  44. protected function getValue($key) {
  45. return $this->getConnection()->getClient()->get($key);
  46. }
  47. /**
  48. * Retrieves multiple values from cache with the specified keys.
  49. * @param array $keys a list of keys identifying the cached values
  50. * @return array a list of cached values indexed by the keys
  51. */
  52. protected function getValues($keys) {
  53. return $this->getConnection()->getClient()->mget($keys);
  54. }
  55. /**
  56. * Stores a value identified by a key in cache.
  57. * This is the implementation of the method declared in the parent class.
  58. *
  59. * @param string $key the key identifying the value to be cached
  60. * @param string $value the value to be cached
  61. * @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
  62. * @return boolean true if the value is successfully stored into cache, false otherwise
  63. */
  64. protected function setValue($key,$value,$expire = 0) {
  65. $this->getConnection()->getClient()->set($key,$value);
  66. if ($expire) {
  67. $this->getConnection()->getClient()->expire($key,$expire);
  68. }
  69. }
  70. /**
  71. * Stores a value identified by a key into cache if the cache does not contain this key.
  72. * This is the implementation of the method declared in the parent class.
  73. *
  74. * @param string $key the key identifying the value to be cached
  75. * @param string $value the value to be cached
  76. * @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
  77. * @return boolean true if the value is successfully stored into cache, false otherwise
  78. */
  79. protected function addValue($key,$value,$expire) {
  80. if($expire>0)
  81. $expire+=time();
  82. else
  83. $expire=0;
  84. if (!$this->getConnection()->getClient()->setnx($key,$value)) {
  85. return false;
  86. }
  87. if ($expire) {
  88. $this->getConnection()->getClient()->expire($key,$expire);
  89. }
  90. return true;
  91. }
  92. /**
  93. * Deletes a value with the specified key from cache
  94. * This is the implementation of the method declared in the parent class.
  95. * @param string $key key of the value to be deleted
  96. * @return boolean if no error happens during deletion
  97. */
  98. protected function deleteValue($key) {
  99. return $this->getConnection()->getClient()->delete($key);
  100. }
  101. /**
  102. * Deletes all values from cache.
  103. * Be careful of performing this operation if the cache is shared by multiple applications.
  104. * @return boolean whether flushing was successful or not
  105. */
  106. public function flush() {
  107. return (bool) $this->getConnection()->getClient()->flushDb();
  108. }
  109. }