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);
} }
redis单例模式写法的更多相关文章
- TP5.0 Redis(单例模式)(原)
看到好多面试都问设计模式,我就简单的了解了一下,顺便把之前封装好的Reis做了一次修改. 单例模式(Singleton Pattern 单件模式或单元素模式) 单例模式确保某个类只有一个实例,而且自行 ...
- redis单例模式
看到好多面试都问设计模式,我就简单的了解了一下,顺便把之前封装好的Reis做了一次修改. 单例模式(Singleton Pattern 单件模式或单元素模式) 单例模式确保某个类只有一个实例,而且自行 ...
- tp5 redis 单例模式 转载
单例模式(Singleton Pattern 单件模式或单元素模式) 单例模式确保某个类只有一个实例,而且自行实例化并向整个系统提供这个实例. 单例模式有以下3个特点: 1 . 它必须有一个构造函数, ...
- php redis 单例模式
单例模式思想其实很简单 首先 有一个实例的静态变量 构造方法和克隆方法设置为私有,防止外部直接new 提供一个获取实例的静态方法 代码如下: class Redis { private static ...
- PHP程序中的redis一些写法
<?php /** * 以下均要先链接好redis */ sdk\libs\RedisHelper::connect("s1")->keys('*'); //这个是获取 ...
- 基于AtomicReference的单例模式写法
AtomicReference类主要属性(来源于jdk1.7中的源码) public class AtomicReference<V> implements java.io.Seriali ...
- 23种设计模式--单例模式-Singleton
一.单例模式的介绍 单例模式简单说就是掌握系统的至高点,在程序中只实例化一次,这样就是单例模式,在系统比如说你是该系统的登录的第多少人,还有数据库的连接池等地方会使用,单例模式是最简单,最常用的模式之 ...
- Redis修改数据多线程并发—Redis并发锁
本文版权归博客园和作者本人吴双共同所有 .转载爬虫请注明地址,博客园蜗牛 http://www.cnblogs.com/tdws/p/5712835.html 蜗牛Redis系列文章目录http:// ...
- 你真的会写单例模式吗-------Java实现
转载: 你真的会写单例模式吗--Java实现 单例模式可能是代码最少的模式了,但是少不一定意味着简单,想要用好.用对单例模式,还真得费一番脑筋.本文对Java中常见的单例模式写法做了一个总结,如有错漏 ...
随机推荐
- Python 常用系统模块整理
Python中的常用的系统模块中部分函数等的整理 random: 随机数 sys: 系统相关 os: 系统相关的 subprocess: 执行新的进程 multiprocessing: 进程相关 th ...
- CSS3的媒体查询(Media Queries)与移动设备显示尺寸大全
媒体查询介绍 我今天就总结一下响应式设计的核心CSS技术Media(媒体查询器)的用法. 先看一个简单的例子: <link rel="stylesheet" media=&q ...
- 如何在数据表当中找出被删掉的数据行ID
这个问题是一年前我刚步入IT行业的一个面试题,当时抓破头皮都想不到的问题,但现在回想过去自身不禁感到可笑,不多扯直接写解决方案.如何在数据表当中找出被删掉的数据行ID,意思是:在一堆的数据当中,让你找 ...
- Android 线程交互
在Android开发过程中,耗时操作是不允许写在主线程(UI线程)中的,以免由于等待时间过长而发生ANR.所以耗时操作需要创建子线程来完成,然而往往这些操作都需要与主线程进行通讯交互(例如更新主线程的 ...
- java传输文件的简单方法
假设现在已经打包了一个文件(1233444333),要将这个文件传输给另一方: package file; import java.io.*; public class F_PasswordUnPas ...
- 使用fiddler对手机上的程序进行抓包
用fiddler对手机上的程序进行抓包,网上有很多的资料,这里写一下来进行备用. 前提: 1.必须确保安装fiddler的电脑和手机在同一个wifi环境下 备注:如果电脑用的是台式机,可以安装一个 ...
- MyBatis笔记----报错:Exception in thread "main" org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)解决方法
报错 Exception in thread "main" org.apache.ibatis.binding.BindingException: Invalid bound st ...
- 完全数java
完全数:小于本身的所有因子的和(包括1) public class test01 { public static void main(String[] args) { Scanner scanner= ...
- [20181108]12c sqlplus rowfetch参数4.txt
[20181108]12c sqlplus rowfetch参数4.txt --//12cR2 可以改变缺省rowfetch参数.11g之前缺省是1.通过一些测试说明问题.--//前几天做的测试有点乱 ...
- MySQl ifnull()和substr()
SUBSTR(str,pos,len) substr用来截取字符串: str 被截取的字符串 pos 开始位置 len 长度 举个例子: substr('abc',1,2)='ab' IFNULL(e ...