ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

redis单例模式写法

2021-05-26 20:54:25  阅读:124  来源: 互联网

标签:function handle redis 单例 ttl return 写法 public


<?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);
    }

}

 

标签:function,handle,redis,单例,ttl,return,写法,public
来源: https://blog.51cto.com/u_13481146/2819446

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有