zoukankan      html  css  js  c++  java
  • thinkphp3.2.3 版本使用redis缓存的时候无法使用认证

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

  • 相关阅读:
    [sql]在case语句中不同情况下then的数据的数据类型不一致ORA-00932: inconsistent datatypes: expected NUMBER got CHAR
    环境迁移 小记
    linux下安装oracle遇到的问题
    正向代理与反向代理
    文件夹与SVN脱离关系
    shell 脚本中$$,$#,$?
    在MySQL中单列索引和联合索引的区别
    Java中Map、HashMap、LinkedHashMap、TreeMap的区别
    Error、Exception与RuntimeException的区别
    设计模式--单例模式
  • 原文地址:https://www.cnblogs.com/lizhaoyao/p/6060794.html
Copyright © 2011-2022 走看看