码迷,mamicode.com
首页 > Web开发 > 详细

thinkphp3.2.3 版本使用redis缓存添加认证

时间:2017-03-23 18:58:44      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:logs   const   merge   启用   persist   判断   连接   cache   方法   

我在使用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认证密码" 即可


 文章来源:http://www.cnblogs.com/lizhaoyao/p/6060794.html

 

thinkphp3.2.3 版本使用redis缓存添加认证

标签:logs   const   merge   启用   persist   判断   连接   cache   方法   

原文地址:http://www.cnblogs.com/bndong/p/6606490.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!