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中常见的单例模式写法做了一个总结,如有错漏 ...
随机推荐
- Node.js性能分析神器Easy-Monitor
摘要: 使用Easy-Monitor,可以准确定位Node.js应用的性能瓶颈,帮助我们优化代码性能. 当应用出现性能问题时,最大的问题在于:如何准确定位造成性能瓶颈的代码呢?对于Node.js开发者 ...
- react学习(一)
组件和属性(props) 函数式组件: function Welcome(props) { return <h1>Hello, {props.name}</h1>; } 渲染一 ...
- js正则表达式 URL格式匹配详解
0.URL格式 protocol :// hostname[:port] / path / [;parameters][?query]#fragment [;parameters]没见过 这里就不做相 ...
- qW3xT.2,解决挖矿病毒。
网站在运行期间感觉怪怪的,响应速度慢的不是一丁半点,带宽5M,不该是这样的呀 于是登录Xshell top命令 查看cpu情况如下 PID为3435的进程占用CPU过大,难道被病毒入侵了吗? 查看该进 ...
- gulp插件构建项目 压缩js、css、image、zip、web服务、跨域等插件
推荐一个很好文: https://github.com/lin-xin/blog/issues/2 匹配符 *.**.!.{} gulp.src('./js/*.js') // * 匹配js文件夹下所 ...
- web移动端,需要清楚设备像素比devicePixelRatio的应用
我们这里所说的devicePixelRatio其实指的是window.devicePixelRatio, 被所有WebKit浏览器以及Opera所支持. 概念 devicePixelRatio ,它是 ...
- windows10系统关闭自动更新服务
一.关闭Windows10系统的自动更新服务 1:使用快捷键Win+R,打开运行 2:输入命令:services.msc,打开系统服务界面 找到Windows Update双击 将启动类型改为[禁用] ...
- htnl 定位
相对定位 相对定位:position:relative; 相对定位:相对定位是相对于元素在文档中的初始位置——首先它出现在它所在的位置上(即不设置position时的位置,然后通过设置垂直或水平位置, ...
- ReactNative编写规范
<一> React 代码规范 文件与组件命名 扩展名: 使用.js作为js文件的扩展名.如果同一个文件夹下有同名而不同作用的js文件,则通过中缀(小写)进一步区分,例如:HomePage ...
- C#-非泛型集合的方法
非泛型集合的类和接口位于System.Collections命名空间 如:列表.队列.位数组.哈希表和字典的集合 ArrayList 动态数组 可被单独索引的对象的有序集合可以使用索引在指定的 ...