1. <?php
  2.  
  3. /**只看红色重点
  4. * ===========================================================
  5. * ZW_Memory_Cache
  6. * Description
  7. * ZW_Memory_Cache
  8. * @Author wzhu.email@gmail.com
  9. * @Version 1.0
  10. * @Copyright Zhuweiwei
  11. * Copyright © 2008-2012
  12. * China. All Rights Reserved.
  13. * ===========================================================
  14. */
  15.  
  16. namespace ZW\Memory;
  17. use \Redis as Redis;
  18. use ZW\Conf\Memory as Conf;
  19.  
  20. class Handle {
  21.  
  22. private $handle = NULL;
  23. private static $_instance = NULL; //定义私有的属性变量
  24.  
  25. public static function getInstance() { //定义公用的静态方法
  26. if (NULL == self::$_instance) {
  27. self::$_instance = new self;
  28. }
  29. return self::$_instance;
  30. }
  31.  
  32. public function __construct() {
  33. $redis = new Redis(); //实例化redis
  34. $redis->connect(Conf::HOST, Conf::PORT);
  35. $redis->auth(Conf::AUTH);
  36. $this->handle = &$redis; //将变量与redis通过引用符关联在一起,以后直接使用handle即可,相当于将redis付给一个变量,这是另一种写法
  37. $this->handle->select(ENVIRONMENT);
  38. }
  39.  
  40. public function __destruct() {
  41. $this->handle->close();
  42. }
  43.  
  44. public function get($k) {
  45. return $this->handle->get($k . ''); //获取redis键名
  46. }
  47.  
  48. public function set($k, $v) {
  49. return $this->handle->set($k . '', $v . '');
  50. }
  51.  
  52. public function setex($k, $v, $ttl = SEC_HOUR) {
  53. return $this->handle->setex($k, intval($ttl), $v);
  54. }
  55.  
  56. public function del($k) {
  57. return $this->handle->delete($k);
  58. }
  59.  
  60. public function increment($k, $step = 1, $def = 0) {
  61. if (!$this->handle->exists($k)) {
  62. $this->handle->set($k, intval($def));
  63. }
  64. return $this->handle->incrBy($k, max(1, $step));
  65. }
  66.  
  67. public function decrement($k, $step = 1, $def = 0) {
  68. if (!$this->handle->exists($k)) {
  69. $this->handle->set($k, intval($def));
  70. }
  71. return $this->handle->decrBy($k, max(1, $step));
  72. }
  73.  
  74. public function arrGet(array $arrKey) {
  75. return $this->handle->mGet($arrKey);
  76. }
  77.  
  78. public function arrSet(array $arrKv) {
  79. return $this->handle->mset($arrKv);
  80. }
  81.  
  82. public function getListAt($k, $index) {
  83. return $this->handle->lGet($k, $index);
  84. }
  85.  
  86. public function setListAt($k, $index, $v) {
  87. return $this->handle->lSet($k, $index, $v);
  88. }
  89.  
  90. public function pushListHead($k, $v) {
  91. return $this->handle->lPush($k, $v);
  92. }
  93.  
  94. public function pushListTail($k, $v) {
  95. return $this->handle->rPush($k, $v);
  96. }
  97.  
  98. public function popListHead($k) {
  99. return $this->handle->lPop($k);
  100. }
  101.  
  102. public function popListTail($k) {
  103. return $this->handle->rPop($k);
  104. }
  105.  
  106. public function getListSize($k) {
  107. return $this->handle->lSize($k);
  108. }
  109.  
  110. public function ttl($k) {
  111. return $this->handle->ttl($k);
  112. }
  113.  
  114. public function setnx($k, $v){
  115. return $this->handle->setnx($k, $v);
  116. }
  117.  
  118. public function exists($k) {
  119. return $this->handle->exists($k);
  120. }
  121.  
  122. public function expire($k, $ttl) {
  123. $this->handle->expire($k, intval($ttl));
  124. }
  125.  
  126. public function persist($k) {
  127. $this->handle->persist($k);
  128. }
  129.  
  130. public function expireAt($k, $timeStamp) {
  131. $this->handle->expireAt($k, $timeStamp);
  132. }
  133.  
  134. public function append($k, $append) {
  135. $this->handle->append($k, $append);
  136. }
  137.  
  138. public function keys($regexKey) {
  139. return $this->handle->keys($regexKey);
  140. }
  141.  
  142. }

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. react-router 嵌套路由 内层route找不到

    今天在做嵌套路由的时候,没有报错,但是页面显示为空,搜索了一下资料,有两个原因: 1.exact精确匹配 <Route component={xxx} path="/" /& ...

  2. 前端学习 之 Bootstrap(二)

    一.代码 内联代码:用<code>包裹,但是需要用<和>表示尖括号. 键盘输入:用<kbd>包裹表示键盘输入的内容. 多行代码:用<pre>包裹多行代码 ...

  3. 洛谷P3246 [HNOI2016]序列(离线 差分 树状数组)

    题意 题目链接 Sol 好像搞出了一个和题解不一样的做法(然而我考场上没写出来还是爆零0) 一个很显然的思路是考虑每个最小值的贡献. 预处理出每个数左边第一个比他小的数,右边第一个比他大的数. 那么\ ...

  4. AI在汽车中的应用:实用深度学习

    https://mp.weixin.qq.com/s/NIza8E5clC18eMF_4GMwDw 深度学习的“深度”层面源于输入层和输出层之间实现的隐含层数目,隐含层利用数学方法处理(筛选/卷积)各 ...

  5. 8.Odoo产品分析 (二) – 商业板块(3) –CRM(2)

    查看Odoo产品分析系列--目录 接上一篇Odoo产品分析 (二) – 商业板块(3) –CRM(1) 4. 设置 在配置–>设置中:    在分析"销售"模块时已经将其他的 ...

  6. Android 源码编译之旅

    目录 前言 背景 安装软件 正文 Mac 分区 移动硬盘分区 Repo 下载源码 编译 源码导入 Android Studio 查看 碰到的问题 Could not find a supported ...

  7. uni-app 父组件引用子组件时怎么调用子组件的方法

    1.写一个简单的子组件main/index.vue: <template> <view> </view> </template> <script& ...

  8. day10(闭包、import模块、函数命名空间)

    #闭包:嵌套函数,内部函数调用外部函数的变量 # def outer(): # a = 1 # def inner(): # print(a) # inner() # outer() def oute ...

  9. (网页)html5 canvas清空画布方法(转)

    总结以下三种清空canvas画布的方式: 1. 最简单的方法:由于canvas每当高度或宽度被重设时,画布内容就会被清空,因此可以用以下方法清空: function clearCanvas() { v ...

  10. c++屏蔽Win10系统快捷键

    很久之前实现的功能,也是参考其他人的实现,时间太久,具体参考哪里已经记不得了. 这里不仅能屏蔽一般的快捷键,还可以屏蔽ctrl+atl+del. ; HHOOK keyHook = NULL; HHO ...