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中常见的单例模式写法做了一个总结,如有错漏 ...
随机推荐
- react-router 嵌套路由 内层route找不到
今天在做嵌套路由的时候,没有报错,但是页面显示为空,搜索了一下资料,有两个原因: 1.exact精确匹配 <Route component={xxx} path="/" /& ...
- 前端学习 之 Bootstrap(二)
一.代码 内联代码:用<code>包裹,但是需要用<和>表示尖括号. 键盘输入:用<kbd>包裹表示键盘输入的内容. 多行代码:用<pre>包裹多行代码 ...
- 洛谷P3246 [HNOI2016]序列(离线 差分 树状数组)
题意 题目链接 Sol 好像搞出了一个和题解不一样的做法(然而我考场上没写出来还是爆零0) 一个很显然的思路是考虑每个最小值的贡献. 预处理出每个数左边第一个比他小的数,右边第一个比他大的数. 那么\ ...
- AI在汽车中的应用:实用深度学习
https://mp.weixin.qq.com/s/NIza8E5clC18eMF_4GMwDw 深度学习的“深度”层面源于输入层和输出层之间实现的隐含层数目,隐含层利用数学方法处理(筛选/卷积)各 ...
- 8.Odoo产品分析 (二) – 商业板块(3) –CRM(2)
查看Odoo产品分析系列--目录 接上一篇Odoo产品分析 (二) – 商业板块(3) –CRM(1) 4. 设置 在配置–>设置中: 在分析"销售"模块时已经将其他的 ...
- Android 源码编译之旅
目录 前言 背景 安装软件 正文 Mac 分区 移动硬盘分区 Repo 下载源码 编译 源码导入 Android Studio 查看 碰到的问题 Could not find a supported ...
- uni-app 父组件引用子组件时怎么调用子组件的方法
1.写一个简单的子组件main/index.vue: <template> <view> </view> </template> <script& ...
- day10(闭包、import模块、函数命名空间)
#闭包:嵌套函数,内部函数调用外部函数的变量 # def outer(): # a = 1 # def inner(): # print(a) # inner() # outer() def oute ...
- (网页)html5 canvas清空画布方法(转)
总结以下三种清空canvas画布的方式: 1. 最简单的方法:由于canvas每当高度或宽度被重设时,画布内容就会被清空,因此可以用以下方法清空: function clearCanvas() { v ...
- c++屏蔽Win10系统快捷键
很久之前实现的功能,也是参考其他人的实现,时间太久,具体参考哪里已经记不得了. 这里不仅能屏蔽一般的快捷键,还可以屏蔽ctrl+atl+del. ; HHOOK keyHook = NULL; HHO ...