params["user_login_cache"]["state"] == "on") throw new Exception("Redis extension is not loaded"); $this->expireTime = isset(Yii::app()->params["user_login_cache"]["expire_time"]) ? Yii::app()->params["user_login_cache"]["expire_time"] : 3600; $server = Yii::app()->params["user_login_cache"]["servers"]; $this->_redis = new Redis(); $this->_redis->connect($server["host"], $server["port"]); if(isset($server['password'])){ $this->_redis->auth($server['password']); } $this->_redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP); } protected static function instance(){ if(!is_object(self::$_instance)){ self::$_instance = new self(); } return self::$_instance; } public static function select($dbNum){ return self::instance()->_redis->select($dbNum); } public static function keys($keys){ return self::instance()->_redis->keys($keys); } public static function set($key, $val){ $params = func_get_args(); if(isset($params[2])) $expireTime = $params[2]; else $expireTime = self::instance()->expireTime; return $expireTime === -1 ? self::instance()->_redis->set($key, $val) : self::instance()->_redis->setex($key, $expireTime, $val); } public static function add($key, $val){ $params = func_get_args(); if(isset($params[2])) $expireTime = $params[2]; else $expireTime = self::instance()->expireTime; try{ self::instance()->_redis->multi(); self::instance()->_redis->setnx($key, $val); self::instance()->_redis->expire($key, $expireTime); self::instance()->_redis->exec(); return true; } catch(RedisException $e){ return false; } } public static function exists($key){ return self::instance()->_redis->exists($key); } public static function get($key){ return self::instance()->_redis->get($key); } public static function del($key){ $keys = func_get_args(); return self::instance()->_redis->del($keys); } public static function expire($key, $expireTime){ return self::instance()->_redis->expire($key, $expireTime); } }