* @license http://www.opensource.org/licenses/gpl-3.0.html */ /** * CRedisCache uses Predis client as redis php client{@link https://github.com/nrk/predis predis}. */ class CRedisCache extends CCache { /** * @var Redis the Redis instance */ protected $_cache=null; /** * @var array list of servers */ protected $_servers=array(); /** * @var string location of the predis client */ public $predisPath="ext.redis.Predis"; /** * @var string list of servers */ public $servers=array('host'=>'127.0.0.1','port'=>6379); /** * Initializes this application component. * This method is required by the {@link IApplicationComponent} interface. * It creates the redis instance and adds redis servers. * @throws CException if redis extension is not loaded */ public function init() { parent::init(); $this->getRedis(); } /** * @return mixed the redis instance (or redisd if {@link useRedisd} is true) used by this component. */ public function getRedis() { if($this->_cache!==null) return $this->_cache; else{ require_once Yii::getPathOfAlias($this->predisPath).".php"; Yii::log('Opening Redis connection',CLogger::LEVEL_TRACE); return $this->_cache=new Predis_Client($this->servers); } } /** * Retrieves a value from cache with a specified key. * This is the implementation of the method declared in the parent class. * @param string $key a unique key identifying the cached value * @return string the value stored in cache, false if the value is not in the cache or expired. */ public function getValue($key) { $result = $this->_cache->get($key); // $this->closeRedis(); return $result; } /** * Retrieves multiple values from cache with the specified keys. * @param array $keys a list of keys identifying the cached values * @return array a list of cached values indexed by the keys * @since 1.0.8 */ public function getValues($keys) { $result = $this->_cache->mget($keys); // $this->closeRedis(); return $result; } /** * @param $key * @param $array * * @return bool */ public function hmset($key,$array) { $result = $this->_cache->hmset($key,$array); // $this->closeRedis(); return $result; } public function hmget($key,$array) { $result = $this->_cache->hmget($key,$array); // $this->closeRedis(); return $result; } /** * @param $value * @return array */ public function hvals($key) { $result = $this->_cache->hvals($key); // $this->closeRedis(); return $result; } /** * delete hash opeation */ public function hdel($name, $key) { $result = $this->_cache->hdel($name, $key); // $this->closeRedis(); return $result; } /** * Stores a value identified by a key in cache. * This is the implementation of the method declared in the parent class. * * @param string $key the key identifying the value to be cached * @param string $value the value to be cached * @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire. * @return boolean true if the value is successfully stored into cache, false otherwise */ public function setValue($key,$value,$expire=0) { if($expire>0){ $result = $this->_cache->setex($key,$expire,$value); // $this->closeRedis(); return $result; }else{ $result = $this->_cache->set($key,$value); // $this->closeRedis(); return $result; } } /** * Stores a value identified by a key into cache if the cache does not contain this key. * This is the implementation of the method declared in the parent class. * * @param string $key the key identifying the value to be cached * @param string $value the value to be cached * @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire. * @return boolean true if the value is successfully stored into cache, false otherwise */ public function addValue($key,$value,$expire) { if($expire>0){ if($this->_cache->setnx($key,$expire,$value)) return $this->_cache->expire($key,$expire); return false; }else return $this->_cache->setnx($key,$value); } /** * Deletes a value with the specified key from cache * This is the implementation of the method declared in the parent class. * @param string $key the key of the value to be deleted * @return boolean if no error happens during deletion */ public function deleteValue($key) { $result = $this->_cache->del($key); // $this->closeRedis(); return $result; } /** * @param $key * @param $name * @param $value * @return int */ public function hset($key,$name,$value){ $result = $this->_cache->hSet($key,$name,$value); // $this->closeRedis(); return $result; } /** * @param $key * @param $expire * * @return bool */ public function expire ($key,$expire){ $result = $this->_cache->expire($key,$expire); // $this->closeRedis(); return $result; } /** * @param $key * @param $name * @return string */ public function hget($key,$name){ $result = $this->_cache->hGet($key,$name); // $this->closeRedis(); return $result; } /** * @param $key */ public function hgetAll($key){ $result = $this->_cache->hgetall($key); // $this->closeRedis(); return $result; } /** * @param $key * @param $name * @return bool */ public function hexists($key,$name){ $result = $this->_cache->hExists($key,$name); // $this->closeRedis(); return $result; } /** * @param $eky * @param $num * @param $value * @return int */ public function zadd($key,$num,$value){ $result = $this->_cache->zAdd($key,$num,$value); // $this->closeRedis(); return $result; } /** * @param $eky * @param $start * @param $end * @return int */ public function zcount($key,$start,$end){ $result = $this->_cache->zCount($key,$start,$end); // $this->closeRedis(); return $result; } /** * @param $key * @param $start * @param $end * @return array */ public function zrange($key,$start,$end){ $result = $this->_cache->zRange($key,$start,$end); // $this->closeRedis(); return $result; } /** * @param $eky * @param $start * @param $end * @return array */ public function zrangebyscore($key,$start,$end){ $result = $this->_cache->zRangeByScore($key,$start,$end); // $this->closeRedis(); return $result; } /** * @param $key * @param $start * @param $end * @param array $options * @return array */ public function zRevRangeByScore($key,$start,$end){ $result = $this->_cache->zRevRangeByScore($key,$start,$end); // $this->closeRedis(); return $result; } /** * Deletes all values from cache. * This is the implementation of the method declared in the parent class. * @return boolean whether the flush operation was successful. * @since 1.1.5 */ public function flushValues() { $result = $this->_cache->flush(); // $this->closeRedis(); return $result; } /** * call unusual method * */ public function __call($method,$args){ return call_user_func_array(array($this->_cache,$method),$args); } /** * Returns whether there is a cache entry with a specified key. * This method is required by the interface ArrayAccess. * @param string $id a key identifying the cached value * @return boolean */ public function offsetExists($id) { $result = $this->_cache->exists($id); // $this->closeRedis(); return $result; } public function hkeys($key){ $result = $this->_cache->hkeys($key); // $this->closeRedis(); return $result; } public function closeRedis(){ $this->_cache->disconnect(); } }