之1、 Thinkphp3.2数据库连查方法cache()时个bug会致使查询失败php
ERR: Redis server went away
获取不到Conf/redis.php的配置,全局方法S()是没有问题的,替换ThinkPHP/Library/Think/Cache/Driver/Redis.class.php的__construct()方法来修正。代码以下:html
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']); } }
之2、另外,此版本的Cache.class.php获取静态实例对象也有一个BUG,修正代码以下:git
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方法去使用。 github
之3、redis.conf配置参数DATA_CACHE_TIME注意不要设置成0,有的说设成0是永久缓存,查看了redis的官方函数,好像没有说明这一点,因此最好设置一个大于0的数字,以免缓存没有生效。(这里还有几个issue1 issue2 说明了不要设成0)redis