redis单例模式写法

<?php

/**只看红色重点
 * ===========================================================
 * ZW_Memory_Cache
 * Description
 *             ZW_Memory_Cache
 * @Author     wzhu.email@gmail.com
 * @Version    1.0
 * @Copyright  Zhuweiwei
 *             Copyright © 2008-2012
 *             China. All Rights Reserved.
 * ===========================================================
 */

namespace ZW\Memory;
use \Redis as Redis;
use ZW\Conf\Memory as Conf;

class Handle {

    private $handle = NULL;
    private static $_instance = NULL;        //定义私有的属性变量


    public static function getInstance() {   //定义公用的静态方法
        if (NULL == self::$_instance) {
            self::$_instance = new self;
        }
        return self::$_instance;
    }


    public function __construct() {
        $redis = new Redis();          //实例化redis
        $redis->connect(Conf::HOST, Conf::PORT);
        $redis->auth(Conf::AUTH);
        $this->handle = &$redis;       //将变量与redis通过引用符关联在一起,以后直接使用handle即可,相当于将redis付给一个变量,这是另一种写法
        $this->handle->select(ENVIRONMENT);
    }

    public function __destruct() {
        $this->handle->close();
    }

    public function get($k) {
        return $this->handle->get($k . '');  //获取redis键名
    }


    public function set($k, $v) {
        return $this->handle->set($k . '', $v . '');
    }


    public function setex($k, $v, $ttl = SEC_HOUR) {
        return $this->handle->setex($k, intval($ttl), $v);
    }


    public function del($k) {
        return $this->handle->delete($k);
    }


    public function increment($k, $step = 1, $def = 0) {
        if (!$this->handle->exists($k)) {
            $this->handle->set($k, intval($def));
        }
        return $this->handle->incrBy($k, max(1, $step));
    }


    public function decrement($k, $step = 1, $def = 0) {
        if (!$this->handle->exists($k)) {
            $this->handle->set($k, intval($def));
        }
        return $this->handle->decrBy($k, max(1, $step));
    }


    public function arrGet(array $arrKey) {
        return $this->handle->mGet($arrKey);
    }


    public function arrSet(array $arrKv) {
        return $this->handle->mset($arrKv);
    }


    public function getListAt($k, $index) {
        return $this->handle->lGet($k, $index);
    }

    public function setListAt($k, $index, $v) {
        return $this->handle->lSet($k, $index, $v);
    }

    public function pushListHead($k, $v) {
        return $this->handle->lPush($k, $v);
    }


    public function pushListTail($k, $v) {
        return $this->handle->rPush($k, $v);
    }


    public function popListHead($k) {
        return $this->handle->lPop($k);
    }


    public function popListTail($k) {
        return $this->handle->rPop($k);
    }


    public function getListSize($k) {
        return $this->handle->lSize($k);
    }

    public function ttl($k) {
        return $this->handle->ttl($k);
    }

    public function setnx($k, $v){
        return $this->handle->setnx($k, $v);
    }

    public function exists($k) {
        return $this->handle->exists($k);
    }


    public function expire($k, $ttl) {
        $this->handle->expire($k, intval($ttl));
    }


    public function persist($k) {
        $this->handle->persist($k);
    }


    public function expireAt($k, $timeStamp) {
        $this->handle->expireAt($k, $timeStamp);
    }


    public function append($k, $append) {
        $this->handle->append($k, $append);
    }


    public function keys($regexKey) {
        return $this->handle->keys($regexKey);
    }

}

 

posted @ 2019-03-29 15:29  御世制人  阅读(2875)  评论(0编辑  收藏  举报