一個簡單清晰的Redis操作類

OldBoy~發表於2017-09-07
<?php
/**
 * redis處理的二次封裝
 * 
 */
class Redis{

    private $_redis;
    
    private $_config;
    
    public function __construct() {        
        $this->_config = Yaf_Application::app()->getConfig()->get("Redis");
        
        if(empty($this->_config)){
            throw new Exception("email config can not be empty!");
        }
        if ($this->_config['servers']['host'] == '')  {
            $this->_config['servers']['host'] = '127.0.0.1'; 
        } 
        if ($this->_config['servers']['port'] == ''){
            $this->_config['servers']['port'] = '6379';          
        } 
        $this->_redis = new redis();  
        $this->_redis->connect($this->_config['servers']['host'], $this->_config['servers']['port']);  
        //$this->_redis->pconnect($this->_config['servers']['host'], $this->_config['servers']['port']);
        $this->_redis->auth($this->_config['servers']['password']);
    }

    /** 
     * 設定值 
     * @param string $key KEY名稱 
     * @param string|array $value 獲取得到的資料 
     * @param int $timeOut 時間 
     */  
    public function set($key, $value, $timeOut = 0) {   
        $value = json_encode($value, TRUE);  
        $retRes = $this->_redis->set($key, $value);  
        if ($timeOut > 0) $this->_redis->setTimeout($key, $timeOut);  
        return $retRes;  
    }

    /**
     * 設定db
     * @param int $deIndex db值
     */
    public function select($deIndex) {
        $deIndex = (int)$deIndex;
        $retRes = $this->_redis->select($deIndex);
        return $retRes;
    }

    /**
    * 通過KEY獲取資料 
    * @param string $key KEY名稱 
    */  
    public function get($key) {  
        $result = $this->_redis->get($key);  
        return json_decode($result, TRUE);  
    }  
    
    /** 
     * 刪除一條資料 
     * @param string $key KEY名稱 
     */  
    public function delete($key) {  
        return $this->_redis->delete($key);  
    }  
      
    /** 
     * 清空資料 
     */  
    public function flushAll() {  
        return $this->_redis->flushAll();  
    }  

    /** 
     * 資料入佇列 
     * @param string $key KEY名稱 
     * @param string|array $value 獲取得到的資料 
     * @param bool $right 是否從右邊開始入 
     */  
    public function push($key, $value ,$right = true) {  
        $value = json_encode($value);  
        return $right ? $this->_redis->rPush($key, $value) : $this->redis->lPush($key, $value);  
    }  
      
    /** 
     * 資料出佇列 
     * @param string $key KEY名稱 
     * @param bool $left 是否從左邊開始出資料 
     */  
    public function pop($key , $left = true) {  
        $val = $left ? $this->_redis->lPop($key) : $this->redis->rPop($key);  
        return json_decode($val);  
    }  

    /** 
     * 資料自增 
     * @param string $key KEY名稱 
     */  
    public function increment($key) {  
        return $this->_redis->incr($key);  
    }  
  
    /** 
     * 資料自減 
     * @param string $key KEY名稱 
     */  
    public function decrement($key) {  
        return $this->_redis->decr($key);  
    }  

    /**
     * setTranction   
     * 執行事務新增值
     * @param string $key 
     * @param int $count 
     * @access public
     * @return boolean
     */
    public function setTranction($key, $count){
        $this->_redis->watch($key);
        return $this->_redis->multi()->set($key, $count)->exec();
    }

    /**
     * getTranction   
     * 執行事務獲取
     * @param string $key 
     * @access public
     * @return boolean
     */
    public function getTranction($key){
        $this->_redis->watch($key);
        return $this->_redis->multi()->get($key)->exec();
    }
    
    /**
     * 指定步長增加
     * @param string $key
     * @param int $count
     * @return int
     */
    public function incrBy($key, $count) {
        return $this->_redis->incrBy($key, $count);
    }
    
    /**
     * 指定步長減少
     * @param string $key
     * @param int $count
     * @return int
     */
    public function decrBy($key, $count) {
        return $this->_redis->decrBy($key, $count);
    }

    /**
     * decrByTranction   
     * 執行事務減去某個值
     * @param string $key 
     * @param int $count 
     * @access public
     * @return array
     */
    public function decrByTranction($key, $count){
        $this->_redis->watch($key);
        return $this->_redis->multi()->decrBy($key, $count)->exec();
    }

    /**
     * incrByTranction 
     * 執行事務,增加某個值 
     * @param string $key 
     * @param int $count 
     * @access public
     * @return array
     */
    public function incrByTranction($key, $count){
        $this->_redis->watch($key);
        return $this->_redis->multi()->incrBy($key, $count)->exec();
    }
        
        /**
     * incrByFloat 
     * 執行事務,增加某個值,float型運算
     * @param string $key 
     * @param int $count 
     * @access public
     * @return array
     */
    public function incrByFloat($key, $count){
        $this->_redis->watch($key);
        return $this->_redis->multi()->incrByFloat($key, $count)->exec();
    }

    /** 
     * key是否存在,存在返回ture 
     * @param string $key KEY名稱 
     */  
    public function exists($key) {  
        return $this->_redis->exists($key);  
    }  

    /**
     * setnx 
     * 當沒有值時設定一個值
     * @param string $key 
     * @param mixed $value 
     *
     */
    public function setnx($key, $value){
        return $this->_redis->setnx($key, $value);
    }    
    
    /** 
     * 返回redis物件 
     * redis有非常多的操作方法,我們只封裝了一部分 
     * 拿著這個物件就可以直接呼叫redis自身方法 
     */  
    public function redis() {  
        return $this->_redis;  
    }  

}

 

Module層中的使用

  //*實物商品*-----根據商品ID查詢商品資訊,指定硬條件(是否上架、是否展示、是否刪除)-------使用優先
    public static function getProductInfoById($product_id = 0)  
    {
        $data = [];
        if ( !is_positive_integer( $product_id )) {
            return $data;
        }
        $Cache = new Cache;
        $Cache_key = sprintf(self::$Product_Real_Info_Cache_Key, $product_id);
        $data = unserialize($Cache->get_obj_cache( $Cache_key));
        if ($data === false) {
            $productInfo = self::alias('p')
                        ->field('p.id,p.product_sn,p.product_name,p.product_money,p.product_price,p.score,p.product_stock,
                            p.product_image,p.product_param,p.product_desc,p.product_main,p.category_id,p.merchant_id')
                        ->where(['p.id' => $product_id, 'p.is_delete' => 0,'is_virtual' => 0,'is_shelves' => 1])
                        ->find();
            $data = $productInfo ? $productInfo->toArray() : [];
            if (is_not_empty_array($data)) {
                $Cache->cache_item($Cache_key, serialize($data), self::$Cache_time);
            }
        }
         // echo memory_get_usage();
        return $data;
    }

  

相關文章