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

redis单例模式写法的更多相关文章

  1. TP5.0 Redis(单例模式)(原)

    看到好多面试都问设计模式,我就简单的了解了一下,顺便把之前封装好的Reis做了一次修改. 单例模式(Singleton Pattern 单件模式或单元素模式) 单例模式确保某个类只有一个实例,而且自行 ...

  2. redis单例模式

    看到好多面试都问设计模式,我就简单的了解了一下,顺便把之前封装好的Reis做了一次修改. 单例模式(Singleton Pattern 单件模式或单元素模式) 单例模式确保某个类只有一个实例,而且自行 ...

  3. tp5 redis 单例模式 转载

    单例模式(Singleton Pattern 单件模式或单元素模式) 单例模式确保某个类只有一个实例,而且自行实例化并向整个系统提供这个实例. 单例模式有以下3个特点: 1 . 它必须有一个构造函数, ...

  4. php redis 单例模式

    单例模式思想其实很简单 首先 有一个实例的静态变量 构造方法和克隆方法设置为私有,防止外部直接new 提供一个获取实例的静态方法 代码如下: class Redis { private static ...

  5. PHP程序中的redis一些写法

    <?php /** * 以下均要先链接好redis */ sdk\libs\RedisHelper::connect("s1")->keys('*'); //这个是获取 ...

  6. 基于AtomicReference的单例模式写法

    AtomicReference类主要属性(来源于jdk1.7中的源码) public class AtomicReference<V> implements java.io.Seriali ...

  7. 23种设计模式--单例模式-Singleton

    一.单例模式的介绍 单例模式简单说就是掌握系统的至高点,在程序中只实例化一次,这样就是单例模式,在系统比如说你是该系统的登录的第多少人,还有数据库的连接池等地方会使用,单例模式是最简单,最常用的模式之 ...

  8. Redis修改数据多线程并发—Redis并发锁

    本文版权归博客园和作者本人吴双共同所有 .转载爬虫请注明地址,博客园蜗牛 http://www.cnblogs.com/tdws/p/5712835.html 蜗牛Redis系列文章目录http:// ...

  9. 你真的会写单例模式吗-------Java实现

    转载: 你真的会写单例模式吗--Java实现 单例模式可能是代码最少的模式了,但是少不一定意味着简单,想要用好.用对单例模式,还真得费一番脑筋.本文对Java中常见的单例模式写法做了一个总结,如有错漏 ...

随机推荐

  1. C#如何通过属性名称反射出属性本身

    A a = new A(); Type t = typeof(A); var v1 = t.GetField("n").GetValue(a);//获取字段值,你的类A.n是字段, ...

  2. IIS服务器被配置为不列出此目录的内容

    使用 IIS 管理器启用目录浏览. 打开 IIS 管理器. 在“功能”视图中,双击“目录浏览”. 在“目录浏览”页上,在“操作”窗格中单击“启用”.

  3. 小tips:JS之浅拷贝与深拷贝

    浅拷贝: function extendCopy(p) { var c = {}; for (var i in p) { c[i] = p[i]; } return c; } 深拷贝: functio ...

  4. 洛谷P4725 【模板】多项式对数函数(多项式ln)

    题意 题目链接 Sol 这个不用背XD 前置知识: \(f(x) = ln(x), f'(x) = \frac{1}{x}\) \(f(g(x)) = f'(g(x)) g'(x)\) 我们要求的是\ ...

  5. 洛谷P4589 [TJOI2018]智力竞赛(二分答案 二分图匹配)

    题意 题目链接 给出一个带权有向图,选出n + 1n+1条链,问能否全部点覆盖,如果不能,问不能覆盖的点权最小值最大是多少 Sol TJOI怎么净出板子题 二分答案之后直接二分图匹配check一下. ...

  6. loadrunner 脚本优化-检查点设置

    脚本优化-检查点设置 by:授客 QQ:1033553122 VuGen判断脚本是否执行成功是根据服务器返回的状态来确定的,如果服务器返回的是HTTP状态为200 OK,那么VuGen就认为脚本正确地 ...

  7. 更新 Anaconda 库文件

    查看库 Anaconda Navigator中 启动Anaconda Prompt(或Anaconda Navigator中Environment->(base)root->Open te ...

  8. Android深入四大组件(九)Content Provider的启动过程

    前言 Content Provider做为四大组件之一,通常情况下并没有其他的组件使用频繁,但这不能作为我们不去深入学习它的理由.关于Content Provider一篇文章是写不完的,这一篇文章先来 ...

  9. Git多人协作常用命令

    Git多人协作工作模式: 首先,可以试图用git push origin branch-name推送自己的修改. 如果推送失败,则因为远程分支比你的本地更新早,需要先用git pull试图合并. 如果 ...

  10. 你的leader还在考核你的千行代码Bug率吗?

    管理学大师德鲁克说:你如果你无法度量它,就无法管理它.要想做有效的管理,就很难绕开度量的问题. 软件开发的过程或者技术团队的管理也存在着如何去合理的度量效率的问题.而度量是把双刃剑,度量具有极强的引导 ...