zoukankan      html  css  js  c++  java
  • 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 ZWMemory;
    use Redis as Redis;
    use ZWConfMemory 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);
        }
    
    }
  • 相关阅读:
    Eric 的随机文件名 生成方法
    .NET模板引擎 EFPlatform.CodeGenerator (代码生成器 静态页生成 生成HTML 模板)
    Eric的行政区(省市)下列表控件和标签控件
    下载网页中远程图片的方法
    一起来玩Twister(扭扭乐)
    ASP.NET 安全认证(四)——巧妙实现 Form 表单认证跨站点、跨服务器的单点登录(Single Sign On) .
    DataList的编辑、更新、删除、全选、分页以及 <EditItemTemplate>模版中的DropDownList的数据绑定
    ASP.NET 安全认证(三)—— 用Form 表单认证实现单点登录(Single Sign On) .
    C# 中的委托和事件
    ASP.Net获取当前运行文件的文件名称
  • 原文地址:https://www.cnblogs.com/yszr/p/10621598.html
Copyright © 2011-2022 走看看