Rdeis类文件

3 篇文章 0 订阅

<?php
namespace Lib;

/**
 * Class
 */
class Redis
{
    private static $_ins;
    private $_redis;
    private function __construct()
    {
        $this->_re_connect();
    }
    /**
     * @return \Redis
     */
    static function instance()
    {
        return self::$_ins ?: (self::$_ins = new self());
    }
    private function _getRedis()
    {
        if ($this->_redis === null) {
            $this->_redis = new \Redis();
        }
        return $this->_redis;
    }
    /**
     * @return mixed
     */
    private function _re_connect()
    {
        if (defined('REDIS_PORT') && is_numeric(REDIS_PORT)) {
            $this->_getRedis()->pconnect(REDIS_HOST, REDIS_PORT);
        } else {
            $this->_getRedis()->pconnect(REDIS_HOST);
        }
        if (defined('REDIS_PASS') && is_string(REDIS_PASS)) {
            $this->_getRedis()->auth(REDIS_PASS);
        }
    }
    /**
     * @param $name
     * @param $arguments
     * @return mixed
     * @throws \RedisException
     */
    public function __call($name, $arguments)
    {
        if (method_exists($this->_redis, $name)) {
            try {
                return call_user_func_array([$this->_redis, $name], $arguments);
            } catch (\RedisException $e) {
                $this->_destruct();
                $this->_re_connect();
                return call_user_func_array([$this->_redis, $name], $arguments);
            }
        }
        trigger_error('Call to undefined method \\Redis::' . $name . '()', E_USER_ERROR);
    }
    public function __get($name)
    {
        if (property_exists($this->_redis, $name)) {
            return $this->_redis->{$name};
        }
        trigger_error('Undefined property: \\Redis::' . $name, E_USER_NOTICE);
    }
    public function __set($name, $val)
    {
        if ($name && $val) {
            $this->_redis->{$name} = $val;
            return;
        }
        /* if (property_exists($this->_redis, $name)) {
                 $this->_redis->{$name} = $val;
                 return;
             }
           trigger_error('Undefined property: \Redis::' . $name, E_USER_NOTICE);*/
    }
    private function _destruct()
    {
        if ($this->_redis) {
            $this->_redis->close();
            unset($this->_redis);
        }
        $this->_redis = null;
    }
    public function __destruct()
    {
        $this->_destruct();
    }
}

使用前需设置//redis配置
define('REDIS_HOST', '127.0.0.1');
//端口默认可注释配置
//define('REDIS_PORT', '6379');
//define('REDIS_PASS', '');

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值