CRedisCache.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. protected $_cache=null;
  19. /**
  20. * @var array list of servers
  21. */
  22. protected $_servers=array();
  23. /**
  24. * @var string location of the predis client
  25. */
  26. public $predisPath="ext.redis.Predis";
  27. /**
  28. * @var string list of servers
  29. */
  30. public $servers=array('host'=>'127.0.0.1','port'=>6379);
  31. /**
  32. * Initializes this application component.
  33. * This method is required by the {@link IApplicationComponent} interface.
  34. * It creates the redis instance and adds redis servers.
  35. * @throws CException if redis extension is not loaded
  36. */
  37. public function init()
  38. {
  39. parent::init();
  40. $this->getRedis();
  41. }
  42. /**
  43. * @return mixed the redis instance (or redisd if {@link useRedisd} is true) used by this component.
  44. */
  45. public function getRedis()
  46. {
  47. if($this->_cache!==null)
  48. return $this->_cache;
  49. else{
  50. require_once Yii::getPathOfAlias($this->predisPath).".php";
  51. Yii::log('Opening Redis connection',CLogger::LEVEL_TRACE);
  52. return $this->_cache=new Predis_Client($this->servers);
  53. }
  54. }
  55. /**
  56. * Retrieves a value from cache with a specified key.
  57. * This is the implementation of the method declared in the parent class.
  58. * @param string $key a unique key identifying the cached value
  59. * @return string the value stored in cache, false if the value is not in the cache or expired.
  60. */
  61. public function getValue($key)
  62. {
  63. $result = $this->_cache->get($key);
  64. // $this->closeRedis();
  65. return $result;
  66. }
  67. /**
  68. * Retrieves multiple values from cache with the specified keys.
  69. * @param array $keys a list of keys identifying the cached values
  70. * @return array a list of cached values indexed by the keys
  71. * @since 1.0.8
  72. */
  73. public function getValues($keys)
  74. {
  75. $result = $this->_cache->mget($keys);
  76. // $this->closeRedis();
  77. return $result;
  78. }
  79. /**
  80. * @param $key
  81. * @param $array
  82. *
  83. * @return bool
  84. */
  85. public function hmset($key,$array)
  86. {
  87. $result = $this->_cache->hmset($key,$array);
  88. // $this->closeRedis();
  89. return $result;
  90. }
  91. public function hmget($key,$array)
  92. {
  93. $result = $this->_cache->hmget($key,$array);
  94. // $this->closeRedis();
  95. return $result;
  96. }
  97. /**
  98. * @param $value
  99. * @return array
  100. */
  101. public function hvals($key)
  102. {
  103. $result = $this->_cache->hvals($key);
  104. // $this->closeRedis();
  105. return $result;
  106. }
  107. /**
  108. * delete hash opeation
  109. */
  110. public function hdel($name, $key)
  111. {
  112. $result = $this->_cache->hdel($name, $key);
  113. // $this->closeRedis();
  114. return $result;
  115. }
  116. /**
  117. * Stores a value identified by a key in cache.
  118. * This is the implementation of the method declared in the parent class.
  119. *
  120. * @param string $key the key identifying the value to be cached
  121. * @param string $value the value to be cached
  122. * @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
  123. * @return boolean true if the value is successfully stored into cache, false otherwise
  124. */
  125. public function setValue($key,$value,$expire=0)
  126. {
  127. if($expire>0){
  128. $result = $this->_cache->setex($key,$expire,$value);
  129. // $this->closeRedis();
  130. return $result;
  131. }else{
  132. $result = $this->_cache->set($key,$value);
  133. // $this->closeRedis();
  134. return $result;
  135. }
  136. }
  137. /**
  138. * Stores a value identified by a key into cache if the cache does not contain this key.
  139. * This is the implementation of the method declared in the parent class.
  140. *
  141. * @param string $key the key identifying the value to be cached
  142. * @param string $value the value to be cached
  143. * @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
  144. * @return boolean true if the value is successfully stored into cache, false otherwise
  145. */
  146. public function addValue($key,$value,$expire)
  147. {
  148. if($expire>0){
  149. if($this->_cache->setnx($key,$expire,$value))
  150. return $this->_cache->expire($key,$expire);
  151. return false;
  152. }else
  153. return $this->_cache->setnx($key,$value);
  154. }
  155. /**
  156. * Deletes a value with the specified key from cache
  157. * This is the implementation of the method declared in the parent class.
  158. * @param string $key the key of the value to be deleted
  159. * @return boolean if no error happens during deletion
  160. */
  161. public function deleteValue($key)
  162. {
  163. $result = $this->_cache->del($key);
  164. // $this->closeRedis();
  165. return $result;
  166. }
  167. /**
  168. * @param $key
  169. * @param $name
  170. * @param $value
  171. * @return int
  172. */
  173. public function hset($key,$name,$value){
  174. $result = $this->_cache->hSet($key,$name,$value);
  175. // $this->closeRedis();
  176. return $result;
  177. }
  178. /**
  179. * @param $key
  180. * @param $expire
  181. *
  182. * @return bool
  183. */
  184. public function expire ($key,$expire){
  185. $result = $this->_cache->expire($key,$expire);
  186. // $this->closeRedis();
  187. return $result;
  188. }
  189. /**
  190. * @param $key
  191. * @param $name
  192. * @return string
  193. */
  194. public function hget($key,$name){
  195. $result = $this->_cache->hGet($key,$name);
  196. // $this->closeRedis();
  197. return $result;
  198. }
  199. /**
  200. * @param $key
  201. */
  202. public function hgetAll($key){
  203. $result = $this->_cache->hgetall($key);
  204. // $this->closeRedis();
  205. return $result;
  206. }
  207. /**
  208. * @param $key
  209. * @param $name
  210. * @return bool
  211. */
  212. public function hexists($key,$name){
  213. $result = $this->_cache->hExists($key,$name);
  214. // $this->closeRedis();
  215. return $result;
  216. }
  217. /**
  218. * @param $eky
  219. * @param $num
  220. * @param $value
  221. * @return int
  222. */
  223. public function zadd($key,$num,$value){
  224. $result = $this->_cache->zAdd($key,$num,$value);
  225. // $this->closeRedis();
  226. return $result;
  227. }
  228. /**
  229. * @param $eky
  230. * @param $start
  231. * @param $end
  232. * @return int
  233. */
  234. public function zcount($key,$start,$end){
  235. $result = $this->_cache->zCount($key,$start,$end);
  236. // $this->closeRedis();
  237. return $result;
  238. }
  239. /**
  240. * @param $key
  241. * @param $start
  242. * @param $end
  243. * @return array
  244. */
  245. public function zrange($key,$start,$end){
  246. $result = $this->_cache->zRange($key,$start,$end);
  247. // $this->closeRedis();
  248. return $result;
  249. }
  250. /**
  251. * @param $eky
  252. * @param $start
  253. * @param $end
  254. * @return array
  255. */
  256. public function zrangebyscore($key,$start,$end){
  257. $result = $this->_cache->zRangeByScore($key,$start,$end);
  258. // $this->closeRedis();
  259. return $result;
  260. }
  261. /**
  262. * @param $key
  263. * @param $start
  264. * @param $end
  265. * @param array $options
  266. * @return array
  267. */
  268. public function zRevRangeByScore($key,$start,$end){
  269. $result = $this->_cache->zRevRangeByScore($key,$start,$end);
  270. // $this->closeRedis();
  271. return $result;
  272. }
  273. /**
  274. * Deletes all values from cache.
  275. * This is the implementation of the method declared in the parent class.
  276. * @return boolean whether the flush operation was successful.
  277. * @since 1.1.5
  278. */
  279. public function flushValues()
  280. {
  281. $result = $this->_cache->flush();
  282. // $this->closeRedis();
  283. return $result;
  284. }
  285. /**
  286. * call unusual method
  287. * */
  288. public function __call($method,$args){
  289. return call_user_func_array(array($this->_cache,$method),$args);
  290. }
  291. /**
  292. * Returns whether there is a cache entry with a specified key.
  293. * This method is required by the interface ArrayAccess.
  294. * @param string $id a key identifying the cached value
  295. * @return boolean
  296. */
  297. public function offsetExists($id)
  298. {
  299. $result = $this->_cache->exists($id);
  300. // $this->closeRedis();
  301. return $result;
  302. }
  303. public function hkeys($key){
  304. $result = $this->_cache->hkeys($key);
  305. // $this->closeRedis();
  306. return $result;
  307. }
  308. public function closeRedis(){
  309. $this->_cache->disconnect();
  310. }
  311. }