thinkphp3.2.3 版本使用redis緩衝添加認證

來源:互聯網
上載者:User

標籤: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緩衝添加認證

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.