PHP Redis類操作

gxfw 9年前發布 | 6K 次閱讀 PHP Redis

    /*

 * InitPHP 2.0 國產PHP開發框架  Dao-Nosql-Redis  
 *-------------------------------------------------------------------------------  
 * 版權所有: CopyRight By initphp.com  
 * 您可以自由使用該源碼,但是在使用過程中,請保留作者信息。尊重他人勞動成果就是尊重自己  
 *-------------------------------------------------------------------------------  
 * $Author:zhuli  
 * $Dtime:2011-10-09   
***********************************************************************************/    
class redisInit {    

    private $redis; //redis對象    

    /**  
     * 初始化Redis  
     * $config = array(  
     *  'server' => '127.0.0.1' 服務器  
     *  'port'   => '6379' 端口號  
     * )  
     * @param array $config  
     */    
    public function init($config = array()) {    
        if ($config['server'] == '')  $config['server'] = '127.0.0.1';    
        if ($config['port'] == '')  $config['port'] = '6379';    
        $this->redis = new Redis();    
        $this->redis->connect($config['server'], $config['port']);    
        return $this->redis;    
    }    

    /**  
     * 設置值  
     * @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;    
    }    

    /**  
     * 通過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);    
    }    

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

    /**  
     * 返回redis對象  
     * redis有非常多的操作方法,我們只封裝了一部分  
     * 拿著這個對象就可以直接調用redis自身方法  
     */    
    public function redis() {    
        return $this->redis;    
    }    
}  </pre> 


 本文由用戶 gxfw 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!