修正thinkphp 3.2 的一个BUG: ERR: Redis server went away

之一、 Thinkphp3.2数据库连查方法cache()时个bug会导致查询失败

ERR: Redis server went away

获取不到Conf/redis.php的配置,全局方法S()是没有问题的,替换ThinkPHP/Library/Think/Cache/Driver/Redis.class.php的__construct()方法来修正。代码如下:

public function __construct($options=array()) {
    if ( !extension_loaded('redis') ) {
        E(L('_NOT_SUPPERT_').':redis');
    }
    if(empty($options)) {
        $options = array (
            'host'          => C('REDIS_HOST') ? C('REDIS_HOST') : '127.0.0.1',
            'port'          => C('REDIS_PORT') ? C('REDIS_PORT') : 6379,
            'timeout'       => C('DATA_CACHE_TIMEOUT') ? C('DATA_CACHE_TIMEOUT') : false,
            'auth'    => C('REDIS_AUTH') ? C('REDIS_AUTH'):null,//auth认证
            'persistent'    => false,
        );
    }
    $this->options =  $options;
    $this->options['expire'] =  isset($options['expire'])?  $options['expire']  :   C('DATA_CACHE_TIME');
    $this->options['prefix'] =  isset($options['prefix'])?  $options['prefix']  :   C('DATA_CACHE_PREFIX');
    $this->options['length'] =  isset($options['length'])?  $options['length']  :   0;
    $this->options['persistent'] =  isset($options['persistent'])?  $options['persistent']  :   C('REDIS_PERSISTENT');
    $func = $options['persistent'] ? 'pconnect' : 'connect';
    $this->handler  = new \Redis;

    //2018.9.4 修正cache()方法连查时没有获取到配置的bug
    if(!isset($this->options['host'])){
        $this->options['host'] = $options['host'] = C('REDIS_HOST') ? C('REDIS_HOST') : '127.0.0.1';
    }
    if(!isset($this->options['port'])){
        $this->options['port'] = $options['port'] = C('REDIS_PORT') ? C('REDIS_PORT') : 6379;
    }
    if(!isset($this->options['timeout'])){
        $this->options['timeout'] = $options['timeout'] = C('REDIS_TIMEOUT') ? C('REDIS_TIMEOUT') : 300;
    }
    if(!isset($this->options['auth'])){
        $this->options['auth'] = $options['auth'] = C('REDIS_AUTH') ? C('REDIS_AUTH') : null;
    }
    if(!isset($this->options['expire'])){
        $this->options['expire'] = $options['expire'] = C('DATA_CACHE_TIME') ? C('DATA_CACHE_TIME') : 0;
    }

    $options['timeout'] === false ?
        $this->handler->$func($options['host'], $options['port']) :
        $this->handler->$func($options['host'], $options['port'], $options['timeout']);


    //Auth参数
    if($this->options['auth']!=null)
    {
        $this->handler->auth($this->options['auth']);
    }
}

之二、另外,此版本的Cache.class.php获取静态实例对象也有一个BUG,修正代码如下:

 

static function getInstance($type='',$options=array()) {
static $_instance  =  array();

      //--BEGIN: http://www.thinkphp.cn/bug/3491.html
      $options_array = array();
      if(isset($options['type']) && !empty($options['type'])){
          $options_array['type'] = $options['type'];
      }else{
          $options_array['type'] = $type;
      }
      if(isset($options['host']) && !empty($options['host'])){
          $options_array['host'] = $options['host'];
      }
      if(isset($options['port']) && !empty($options['port'])){
          $options_array['port'] = $options['port'];
      }
      //--END

$guid  =  $type.to_guid_string($options_array);
if(!isset($_instance[$guid])){
   $obj   =  new Cache();
   $_instance[$guid]  =  $obj->connect($type,$options);
}
return $_instance[$guid];
  }

因为使用$options参数进行初始化时,$options里会带有缓存key,而这个key是经常变化的,不能作为唯一实例的判断条件,及后面的to_guid_string方法去使用。 

 

之三、redis.conf配置参数DATA_CACHE_TIME注意不要设置成0,有的说设成0是永久缓存,查看了redis的官方函数,好像没有说明这一点,所以最好设置一个大于0的数字,以避免缓存没有生效。(这里还有几个issue1 issue2 说明了不要设成0)

转载于:https://my.oschina.net/swingcoder/blog/1942445

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值