CRedisCache_old.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * CRedisCache class file
  4. *
  5. * use and modify it as you wish
  6. *
  7. * @author Gustavo Salomé <gustavonips@gmail.com>
  8. * @license http://www.opensource.org/licenses/gpl-3.0.html
  9. */
  10. /**
  11. * CRedisCache uses Predis client as redis php client{@link https://github.com/nrk/predis predis}.
  12. */
  13. class CRedisCache extends CCache
  14. {
  15. /**
  16. * @var Redis the Redis instance
  17. */
  18. private $_cache=null;
  19. /**
  20. * @var array list of redis server configurations
  21. */
  22. private $_servers=array();
  23. /**
  24. * Initializes this application component.
  25. * This method is required by the {@link IApplicationComponent} interface.
  26. * It creates the redis instance and adds redis servers.
  27. * @throws CException if redis extension is not loaded
  28. */
  29. public function init()
  30. {
  31. parent::init();
  32. $servers=$this->getServers();
  33. if(!count($servers))
  34. {
  35. $this->_servers[]=$servers=array('host'=>'127.0.0.1','port'=>6379);
  36. }
  37. $this->getRedis();
  38. }
  39. /**
  40. * @return mixed the redis instance (or redisd if {@link useRedisd} is true) used by this component.
  41. */
  42. public function getRedis()
  43. {
  44. if($this->_cache!==null)
  45. return $this->_cache;
  46. else{
  47. include Yii::getPathOfAlias("ext.redis.Predis").'.php';
  48. Yii::log('Opening Redis connection',CLogger::LEVEL_TRACE);
  49. return $this->_cache=new Predis_Client($this->getServers());
  50. }
  51. }
  52. /**
  53. * @return array list of redis server configurations.
  54. */
  55. public function getServers()
  56. {
  57. return $this->_servers;
  58. }
  59. /**
  60. * Retrieves a value from cache with a specified key.
  61. * This is the implementation of the method declared in the parent class.
  62. * @param string $key a unique key identifying the cached value
  63. * @return string the value stored in cache, false if the value is not in the cache or expired.
  64. */
  65. protected function getValue($key)
  66. {
  67. return $this->_cache->get($key);
  68. }
  69. /**
  70. * Retrieves multiple values from cache with the specified keys.
  71. * @param array $keys a list of keys identifying the cached values
  72. * @return array a list of cached values indexed by the keys
  73. * @since 1.0.8
  74. */
  75. protected function getValues($keys)
  76. {
  77. return $this->_cache->mget($keys);
  78. }
  79. /**
  80. * Stores a value identified by a key in cache.
  81. * This is the implementation of the method declared in the parent class.
  82. *
  83. * @param string $key the key identifying the value to be cached
  84. * @param string $value the value to be cached
  85. * @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
  86. * @return boolean true if the value is successfully stored into cache, false otherwise
  87. */
  88. protected function setValue($key,$value,$expire)
  89. {
  90. if($expire>0)
  91. return $this->_cache->setex($key,$expire,$value);
  92. else
  93. return $this->_cache->set($key,$value);
  94. }
  95. /**
  96. * Stores a value identified by a key into cache if the cache does not contain this key.
  97. * This is the implementation of the method declared in the parent class.
  98. *
  99. * @param string $key the key identifying the value to be cached
  100. * @param string $value the value to be cached
  101. * @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
  102. * @return boolean true if the value is successfully stored into cache, false otherwise
  103. */
  104. protected function addValue($key,$value,$expire)
  105. {
  106. if($expire>0){
  107. if($this->_cache->setnx($key,$time,$value))
  108. return $this->_cache->expire($key,$time);
  109. return false;
  110. }else
  111. return $this->_cache->setnx($key,$value);
  112. }
  113. /**
  114. * Deletes a value with the specified key from cache
  115. * This is the implementation of the method declared in the parent class.
  116. * @param string $key the key of the value to be deleted
  117. * @return boolean if no error happens during deletion
  118. */
  119. protected function deleteValue($key)
  120. {
  121. return $this->_cache->del($key);
  122. }
  123. /**
  124. * Deletes all values from cache.
  125. * This is the implementation of the method declared in the parent class.
  126. * @return boolean whether the flush operation was successful.
  127. * @since 1.1.5
  128. */
  129. protected function flushValues()
  130. {
  131. return $this->_cache->flush();
  132. }
  133. /**
  134. * call unusual method
  135. * */
  136. public function __call($method,$args){
  137. return call_user_func_array(array($this->_cache,$method),$args);
  138. }
  139. /**
  140. * Returns whether there is a cache entry with a specified key.
  141. * This method is required by the interface ArrayAccess.
  142. * @param string $id a key identifying the cached value
  143. * @return boolean
  144. */
  145. public function offsetExists($id)
  146. {
  147. return $this->_cache->exists($id);
  148. }
  149. }