redis操作封裝類


class Redis {
    // 默認配置名稱(使用load_config加載)
private $_default_config_path = 'package/cache/redis';

    // redis實例對象
    private $_redis;

    // redis服務器地址
    private $_host = '';
    // redis服務器端口
    private $_port = 6379;
    
    /**
     * 構造函數
     *
     * @access public
     * @param array $conf 配置文件集合, 包含參數:
     *              string $host 服務器地址
     *              string $port 服務器端口
     * @return void
     */
    public function __construct(array $conf=array()) {
        $this -> set_conf($conf);
        $this -> reconnect(true);
    }

    /**
     * 設置redis配置
     * 執行前,配置會被重置為[host='', port='6379']
     *
     * @access public
     * @param array $conf 配置文件集合, 包含參數:
     *              string $host 服務器地址
     *              string $port 服務器端口
     * @return void
     */
    public function set_conf(array $conf=array()) {
        if (empty($conf)) {
            $conf = load_config($this -> _default_config_path);
if (!is_array($conf) or empty($conf)) {
to_log(MAIN_LOG_ERROR, '', __CLASS__ . ':' . __FUNCTION__ . ': 默認配置為空');
return;
}
        }

        $this -> _host = '';
        $this -> _port = 6379;

        isset($conf['host']) and $this -> _host = $conf['host'];
        isset($conf['port']) and $this -> _port = intval($conf['port']);
    }
    
    /**
     * 重新連接redis
     *
     * @access public
     * @param boolean $is_new 是否必須重新連接
     * @return boolean
     */
    public function reconnect($is_new=false) {
        $ret = false;
        if ($is_new) {
            $ret = $this -> _connect();
            return $ret;
        }

        $check = $this -> _is_connected();
        if (!$check) {
            $ret = $this -> _connect();
        }

        return $ret;
    }
    
    /**
     * 設置緩存數據, 僅支持字符串
     *
     * @access public
     * @param string $key 緩存變量名
     * @param string $value 緩存變量值
     * @param string $ttl 緩存生存時間,單位:秒
     * @return boolean
     */
    public function set($key, $value, $ttl=3600) {
        $key = strval($key);
        $value = strval($value);
        $ttl = intval($ttl);

        $ttl < 0 and $ttl = 0;

        if ($key === '' or $value === '') {
            return false;
        }
        
        try {
            $result = $this -> _redis -> setex($key, $ttl, $value);
        }catch (\RedisException $e) {
            to_log(MAIN_LOG_WARN, '', __CLASS__ . " -> " . __FUNCTION__ . ": " . $e -> getMessage());
            return false;
        }

        return $result ? true : false;
    }

    /**
     * 重新設置緩存變量的生存時間
     *
     * @access public
     * @param string $key 緩存變量名
     * @param string $ttl 緩存生存時間,單位:秒
     * @return boolean
     */
    public function expire($key, $ttl=3600) {
        $key = strval($key);
        $ttl = intval($ttl);

        $ttl < 0 and $ttl = 0;

        if ($key === '') {
            return false;
        }
        
        try {
            $result = $this -> _redis -> expire($key, $ttl);
        }catch (\RedisException $e) {
            to_log(MAIN_LOG_WARN, '', __CLASS__ . " -> " . __FUNCTION__ . ": " . $e -> getMessage());
            return false;
        }

        return $result ? true : false;
    }

    /**
     * 獲取緩存變量值
     *
     * @access public
     * @param string $key 緩存變量名
     * @return mixed 成功返回變量值,失敗返回false
     */
    public function get($key) {
        $key = strval($key);
        if ($key === '') {
            return false;
        }

        try {
            $result = $this -> _redis -> get($key);
        }catch (\RedisException $e) {
            to_log(MAIN_LOG_WARN, '', __CLASS__ . " -> " . __FUNCTION__ . ": " . $e -> getMessage());
            return false;
        }
        return $result;
    }
    
    /**
     * 批量刪除緩存變量
     *
     * @access public
     * @param mixed $key [string|array] 當為string時,自動轉換為array
     * @return boolean
     */
    public function delete($key) {
        !is_array($key) and $key = array($key);

        $tmp_arr = array();
        foreach ($key as $val) {
            $tmp_str = strval($val);
            $tmp_str !== '' and $tmp_arr[$tmp_str] = 1;
        }
        $key = array_keys($tmp_arr);

        try {
            $ret = true;
            foreach ($key as $val) {
                $result = $this -> _redis -> delete($val);
                !$result and $ret = false;
            }
        }catch(\RedisException $e) {
            to_log(MAIN_LOG_WARN, '', __CLASS__ . " -> " . __FUNCTION__ . ": " . $e -> getMessage());
            return false;
        }
        
        return $ret;
    }
    
    /**
     * 清空redis中的所有數據
     *
     * @access public
     * @return boolean
     */
    public function clear() {
        try {
            $result = $this -> _redis -> flushAll();
        }catch(\RedisException $e){
            to_log(MAIN_LOG_WARN, '', __CLASS__ . " -> " . __FUNCTION__ . ": " . $e -> getMessage());
            return false;
        }

        return $result ? true : false;
    }
    
    /**
     * 將緩存變量放入redis隊列,僅支持字符串及整型
     *
     * @access public
     * @param string $key 緩存變量名
     * @param string $value 緩存變量值
     * @param boolean $to_right 是否從右邊入列
     * @return boolean
     */
    public function push($key, $value, $to_right=true) {
        $key = strval($key);
        $value = strval($value);
        
        if ($key === '' or $value === '') {
            return false;
        }
        
        $func = 'rPush';
        !$to_right and $func = 'lPush';

        try {
            $result = $this -> _redis -> $func($key, $value);
        }catch (\RedisException $e) {
            to_log(MAIN_LOG_WARN, '', __CLASS__ . " -> " . __FUNCTION__ . ": " . $e -> getMessage());
            return false;
        }

        return $result ? true : false;
    }

    /**
     * 緩存變量出列
     *
     * @access public
     * @param string $key 緩存變量名
     * @param boolean $from_left 是否從左邊出列
     * @return boolean 成功返回緩存變量值,失敗返回false
     */
    public function pop($key , $from_left=true) {
        $key = strval($key);
        if ($key === '') {
            return false;
        }

        $func = 'lPop';
        !$from_left and $func = 'rPop';
        
        try {
            $result = $this -> _redis -> $func($key);
        }catch(\RedisException $e){
            to_log(MAIN_LOG_WARN, '', __CLASS__ . " -> " . __FUNCTION__ . ": " . $e -> getMessage());
            return false;
        }

        return $result;
    }
    
    /**
     * 緩存變量自增
     *
     * @access public
     * @param string $key 緩存變量名
     * @return boolean
     */
    public function increase($key) {
        $key = strval($key);
        if ($key === '') {
            return false;
        }

        try {
            $result = $this -> _redis -> incr($key);
        }catch(\RedisException $e){
            to_log(MAIN_LOG_WARN, '', __CLASS__ . " -> " . __FUNCTION__ . ": " . $e -> getMessage());
            return false;
        }

        return $result ? true : false;
    }

    /**
     * 緩存變量自減
     *
     * @access public
     * @param string $key 緩存變量名
     * @return boolean 成功返回TRUE,失敗返回FALSE
     */
    public function decrease($key) {
        $key = strval($key);
        if ($key === '') {
            return false;
        }

        try {
            $result = $this -> _redis -> decr($key);
        }catch(\RedisException $e){
            to_log(MAIN_LOG_WARN, '', __CLASS__ . " -> " . __FUNCTION__ . ": " . $e -> getMessage());
            return false;
        }

        return $result ? true : false;
    }
    
    /**
     * 判斷緩存變量是否已經存在
     *
     * @access public
     * @param string $key 緩存變量名
     * @return boolean 存在返回TRUE,否則返回FALSE
     */
    public function exists($key) {
        $key = strval($key);
        if ($key === '') {
            return false;
        }

        try {
            $result = $this -> _redis -> exists($key);
        }catch (\RedisException $e) {
            to_log(MAIN_LOG_WARN, '', __CLASS__ . " -> " . __FUNCTION__ . ": " . $e -> getMessage());
            return false;
        }

        return $result ? true : false;
    }
    
    /**
     * 返回redis源對象
     *
     * @access public
     * @return object
     */
    public function get_handler() {
        return $this -> _redis;
    }

    // ---------私有實現---------------------------------------------
    /**
     * 檢驗並連接redis服務器
     *
     * @access private
     * @return boolean
     */
    private function _connect() {
        if (!class_exists('\Redis', false)) {
            to_log(MAIN_LOG_ERROR, '', 'Redis類不存在,可能是沒有安裝php_redis擴展');
            return false;
        }

        try {
            $this -> _redis = new \Redis();
            $this -> _redis -> connect($this -> _host, $this -> _port);
        }catch(\RedisException $e){
            to_log(MAIN_LOG_WARN, '', __CLASS__ . " -> " . __FUNCTION__ . ": " . $e -> getMessage());
            return false;
        }

        return true;
    }

    /**
     * 判斷是否已連接到服務器
     *
     * @access public
     * @return boolean
     */ 
    public function _is_connected() {
        if(!is_object($this -> _redis)) return false;

        try {
            $this -> _redis -> ping();
        }catch (\RedisException $e) {
            to_log(MAIN_LOG_WARN, '', __CLASS__ . " -> " . __FUNCTION__ . ": " . $e -> getMessage());
            return false;
        }

        return true;
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM