<?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. ql语句中left join和inner join中的on与where的区别分析

    sql语句中left join和inner join中的on与where的区别分析   关于SQL SERVER的表联接查询INNER JOIN .LEFT JOIN和RIGHT JOIN,经常会用到 ...

  2. python名片管理

    python名片管理是我根据视频自己敲敲的代码,后续学习会持续更新 代码 card_main.py import card_tools # 无限循环,由用户决定什么时候退出 while True: # ...

  3. JS之汉字与Unicode码的相互转化

    有时候,我们在给后端传递变量的的值中有汉字,可能由于编码的原因,传递到后端后变为乱码了.所以有时候为了省事或者其它特殊要求的时候,会把传递的汉字转换成Unicode编码后再进行传递. 当然汉字转换成u ...

  4. java程序存入数据库中文乱码解决方案

    一.问题描述 背景:代码迁移,ssm框架在插入数据到mysql数据库时,中文乱码.代码中的编码配置没有问题,因为该项目代码以前使用过,没有问题.现在换了数据库,数据库配置也做了修改,统一使用utf8, ...

  5. Web前端:博客美化:二、鼠标特效

    1.获取JS权限 因为是js代码所以需要放在 侧边栏公告 里 没开通之前,有一个申请的链接,点击即可,我是第二天才看到过审的 ^-^ 2.Ctrl+C.Ctrl+V 数组里的文字随自己心情啦 另:30 ...

  6. 扫码下单使用FAQ

    1.适用情景:扫码点餐支付宝支付报错 解决方案:1.检查主账号上口碑授权是否失效.(重新授权) 2.检查主账号上的PID是否绑定.(绑定PID) 注意:1.支付宝扫码进行的扫码下单支持直连支付宝和蚂蚁 ...

  7. Wampserver或者帝国CMS安装后, 打开localhost显示IIS欢迎界面图片

    我们在安装集成环境Wampserver或者帝国CMS之后,有时会遇到一个问题, 打开localhost显示一张IIS欢迎界面图片,这个问题该如何解决呢,我在这里简单整理了一下解决方法 电脑win10系 ...

  8. 任意activity作为启动页

    在androidManifest.xml中对应的activity中添加 android:exported=“true”

  9. linux下objdump应用

    <a href="http://www.maomao365.com/?p=952" > linux命令objdump的用法  http://www.maomao365. ...

  10. June 11. 2018 Week 24th, Monday

    Love is the beauty of the soul. 爱是灵魂之美. From Saint Augustine. The complete version of this quote goe ...