python 单例模式 redis_redis单例模式写法

本文介绍了使用Python编写的ZW_Memory_Cache类,该类实现了Redis连接的单例模式。通过这个类,可以方便地进行Redis的增删查改等操作,如设置和获取键值、设置过期时间、操作列表等。
摘要由CSDN通过智能技术生成

* ===========================================================

* 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 asRedis;use ZW\Conf\Memory asConf;classHandle {private $handle = NULL;private static $_instance = NULL; //定义私有的属性变量public static functiongetInstance() { //定义公用的静态方法if (NULL == self::$_instance) {

self::$_instance = newself;

}return self::$_instance;

}public function__construct() {$redis = newRedis(); //实例化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);

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值