thinkphp3.2.3 版本使用redis缓存的时候无法使用认证
  7jPfnBIFtnum 26天前 25 0

我在使用thinkphp3.2.3的时候 发现如果是使用redis缓存 设置了认证的redis能连接成功 却无法 set 操作 ,检查发现是没有认证导致的  $redis->auth这一步没有,那么官方给出的 Redis.class.php没有的话,我们可以自己加上,在构造函数第29行 将以前的代码改为:

以前代码如下:

$options = array_merge(array (
'host' => C('REDIS_HOST') ? : '127.0.0.1',
'port' => C('REDIS_PORT') ? : 6379,
'timeout' => C('DATA_CACHE_TIMEOUT') ? : false,
'persistent' => false,
),$options);

加一行 'auth' => C('REDIS_AUTH_PASSWORD') ? C('REDIS_AUTH_PASSWORD'):null,//auth认证的密码  ,改为这样

$options = array_merge(array (
'host' => C('REDIS_HOST') ? : '127.0.0.1',
'port' => C('REDIS_PORT') ? : 6379,
'timeout' => C('DATA_CACHE_TIMEOUT') ? : false,
'auth' => C('REDIS_AUTH_PASSWORD') ? C('REDIS_AUTH_PASSWORD'):null,//auth认证的密码
'persistent' => false,
),$options);

这样就能在options中读取到是否启用认证的密码了,然后后面加一个判断

在这段代码后面

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

加上一个判断,判断是否启用了认证密码配置 启用了就去认证一下

if($this->options['auth']!==null)
{
$this->handler->auth($this->options['auth']); //说明有配置redis的认证配置密码 需要认证一下
}

总体来看,构造方法被改为了如下:

public function __construct($options=array()) {
if ( !extension_loaded('redis') ) {
E(L('_NOT_SUPPORT_').':redis');
}
$options = array_merge(array (
'host' => C('REDIS_HOST') ? : '127.0.0.1',
'port' => C('REDIS_PORT') ? : 6379,
'timeout' => C('DATA_CACHE_TIMEOUT') ? : false,
'auth' => C('REDIS_AUTH_PASSWORD') ? C('REDIS_AUTH_PASSWORD'):null,//auth认证的密码
'persistent' => false,
),$options);

$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;
$func = $options['persistent'] ? 'pconnect' : 'connect';
$this->handler = new \Redis;
$options['timeout'] === false ?
$this->handler->$func($options['host'], $options['port']) :
$this->handler->$func($options['host'], $options['port'], $options['timeout']);
if($this->options['auth']!=null)
{
$this->handler->auth($this->options['auth']); //说明有配置redis的认证配置密码 需要认证一下
}
}

然后配置文件里面加上 "REDIS_AUTH_PASSWORD"=>"redis认证密码" 即可

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 26天前 0

暂无评论

推荐阅读
7jPfnBIFtnum