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);
    }

}

 

转载于:https://www.cnblogs.com/yszr/p/10621598.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值