轉(zhuǎn)載?00天火00?文章保存一下先。惭墓。
說明
好像是tp3.2的bug,自帶的redis驅(qū)動(dòng)不是那么好用坛梁。。腊凶。找了方法修改優(yōu)化了一下划咐,親測(cè)可用。
確認(rèn)已經(jīng)安裝了redis服務(wù)器
確認(rèn)php中已經(jīng)安裝了redis擴(kuò)展
TP版本:thinkphp_3.2.3_full.zip
S緩存使用redis
復(fù)制以下內(nèi)容到文本中钧萍,改名為Redis.class.php褐缠。
替換ThinkPHP\Library\Think\Cache\Driver\Redis.class.php(注意先備份)
* Redis緩存驅(qū)動(dòng)
*/classRedisextendsCache{/**? ? * 架構(gòu)函數(shù)? ? *@paramarray $options 緩存參數(shù)? ? *@accesspublic? ? */publicfunction__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('REDIS_TIMEOUT') ? C('REDIS_TIMEOUT') :false,'auth'=> C('REDIS_AUTH') ? C('REDIS_AUTH'):null,//auth認(rèn)證'persistent'=> C('REDIS_PERSISTENT') ? C('REDIS_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;? ? ? ? $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']);//Auth參數(shù)if($this->options['auth']!=null)? ? ? ? {$this->handler->auth($this->options['auth']);? ? ? ? }? ? }/**? ? * 讀取緩存? ? *@accesspublic? ? *@paramstring $name 緩存變量名? ? *@returnmixed? ? */publicfunctionget($name){? ? ? ? N('cache_read',1);? ? ? ? $value =$this->handler->get($this->options['prefix'].$name);? ? ? ? $jsonData? = json_decode( $value,true);return($jsonData ===NULL) ? $value : $jsonData;//檢測(cè)是否為JSON數(shù)據(jù) true 返回JSON解析數(shù)組, false返回源數(shù)據(jù)}/**? ? * 寫入緩存? ? *@accesspublic? ? *@paramstring $name 緩存變量名? ? *@parammixed $value? 存儲(chǔ)數(shù)據(jù)? ? *@paraminteger $expire? 有效時(shí)間(秒)? ? *@returnboolean? ? */publicfunctionset($name, $value, $expire = null){? ? ? ? N('cache_write',1);if(is_null($expire)) {? ? ? ? ? ? $expire? =$this->options['expire'];? ? ? ? }? ? ? ? $name? =$this->options['prefix'].$name;//對(duì)數(shù)組/對(duì)象數(shù)據(jù)進(jìn)行緩存處理,保證數(shù)據(jù)完整性$value? =? (is_object($value) || is_array($value)) ? json_encode($value) : $value;if(is_int($expire)) {? ? ? ? ? ? $result =$this->handler->setex($name, $expire, $value);? ? ? ? }else{? ? ? ? ? ? $result =$this->handler->set($name, $value);? ? ? ? }if($result &&$this->options['length']>0) {// 記錄緩存隊(duì)列$this->queue($name);? ? ? ? }return$result;? ? }/**? ? * 刪除緩存? ? *@accesspublic? ? *@paramstring $name 緩存變量名? ? *@returnboolean? ? */publicfunctionrm($name){return$this->handler->delete($this->options['prefix'].$name);? ? }/**? ? * 清除緩存? ? *@accesspublic? ? *@returnboolean? ? */publicfunctionclear(){return$this->handler->flushDB();? ? }}
SESSION使用redis
復(fù)制以下內(nèi)容到文本中划煮,改名為Redis.class.php送丰。
復(fù)制到ThinkPHP\Library\Think\Session\Driver\Redis.class.php
*? +----------------------------------------------------------------------
*? | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
*? +----------------------------------------------------------------------
*? | Copyright (c) 2006-2013 http://thinkphp.cn All rights reserved.
*? +----------------------------------------------------------------------
*? | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
*? +----------------------------------------------------------------------
*/namespaceThink\Session\Driver;/**
* Redis Session驅(qū)動(dòng)
*/classRedis{/**
* Redis句柄
*/private$handler;private$get_result;publicfunction__construct(){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('REDIS_TIMEOUT') ? C('REDIS_TIMEOUT') :false,'persistent'=> C('REDIS_PERSISTENT') ? C('REDIS_PERSISTENT') :false,'auth'=> C('REDIS_AUTH') ? C('REDIS_AUTH') :false,? ? ? ? ? ? );? ? ? ? }? ? ? ? $options['host'] = explode(',', $options['host']);? ? ? ? $options['port'] = explode(',', $options['port']);? ? ? ? $options['auth'] = explode(',', $options['auth']);foreach($options['host']as$key=>$value) {if(!isset($options['port'][$key])) {? ? ? ? ? ? ? ? $options['port'][$key] = $options['port'][0];? ? ? ? ? ? }if(!isset($options['auth'][$key])) {? ? ? ? ? ? ? ? $options['auth'][$key] = $options['auth'][0];? ? ? ? ? ? }? ? ? ? }$this->options =? $options;? ? $expire = C('SESSION_EXPIRE');$this->options['expire'] =isset($expire) ? (int)$expire : (int)ini_get('session.gc_maxlifetime');;$this->options['prefix'] =isset($options['prefix']) ?? $options['prefix']? :? C('SESSION_PREFIX');$this->handler? =new\Redis;? ? }/**? ? * 連接Redis服務(wù)端? ? *@accesspublic? ? *@parambool $is_master : 是否連接主服務(wù)器? ? */publicfunctionconnect($is_master = true){if($is_master) {? ? ? ? ? ? $i =0;? ? ? ? }else{? ? ? ? ? ? $count = count($this->options['host']);if($count ==1) {? ? ? ? ? ? ? ? $i =0;? ? ? ? ? ? }else{? ? ? ? ? ? ? ? $i = rand(1, $count -1);//多個(gè)從服務(wù)器隨機(jī)選擇}? ? ? ? }? ? ? ? $func =$this->options['persistent'] ?'pconnect':'connect';try{if($this->options['timeout'] ===false) {? ? ? ? ? ? ? ? $result =$this->handler->$func($this->options['host'][$i],$this->options['port'][$i]);if(!$result)thrownew\Think\Exception('Redis Error',100);? ? ? ? ? ? }else{? ? ? ? ? ? ? ? $result =$this->handler->$func($this->options['host'][$i],$this->options['port'][$i],$this->options['timeout']);if(!$result)thrownew\Think\Exception('Redis Error',101);? ? ? ? ? ? }if($this->options['auth'][$i]) {? ? ? ? ? ? ? ? $result =$this->handler->auth($this->options['auth'][$i]);if(!$result) {thrownew\Think\Exception('Redis Error',102);? ? ? ? ? ? ? ? }? ? ? ? ? ? }? ? ? ? }catch( \Exception$e ) {exit('Error Message:'.$e->getMessage().'
Error Code:'.$e->getCode().'');? ? ? ? }? ? }/**? ? ? +----------------------------------------------------------? ? * 打開Session? ? ? +----------------------------------------------------------? ? *@accesspublic? ? ? +----------------------------------------------------------? ? *@paramstring $savePath? ? ? *@parammixed $sessName? ? ? ? +----------------------------------------------------------? ? */publicfunctionopen($savePath, $sessName){returntrue;? ? }/**? ? ? +----------------------------------------------------------? ? * 關(guān)閉Session? ? ? +----------------------------------------------------------? ? *@accesspublic? ? ? +----------------------------------------------------------? ? */publicfunctionclose(){if($this->options['persistent'] =='pconnect') {$this->handler->close();? ? ? ? }returntrue;? ? }/**? ? ? +----------------------------------------------------------? ? * 讀取Session? ? ? +----------------------------------------------------------? ? *@accesspublic? ? ? +----------------------------------------------------------? ? *@paramstring $sessID? ? ? +----------------------------------------------------------? ? */publicfunctionread($sessID){$this->connect(0);$this->get_result =$this->handler->get($this->options['prefix'].$sessID);//延長(zhǎng)有效期$this->handler->expire($this->options['prefix'].$sessID,C('SESSION_EXPIRE'));return$this->get_result;? ? }/**? ? ? +----------------------------------------------------------? ? * 寫入Session? ? ? +----------------------------------------------------------? ? *@accesspublic? ? ? +----------------------------------------------------------? ? *@paramstring $sessID? ? ? *@paramString $sessData? ? ? ? +----------------------------------------------------------? ? */publicfunctionwrite($sessID, $sessData){if(!$sessData || $sessData ==$this->get_result) {returntrue;? ? ? ? }$this->connect(1);? ? ? ? $expire? =$this->options['expire'];? ? ? ? $sessID? =$this->options['prefix'].$sessID;if(is_int($expire) && $expire >0) {? ? ? ? ? ? $result =$this->handler->setex($sessID, $expire, $sessData);? ? ? ? ? ? $re = $result ?'true':'false';? ? ? ? }else{? ? ? ? ? ? $result =$this->handler->set($sessID, $sessData);? ? ? ? ? ? $re = $result ?'true':'false';? ? ? ? }return$result;? ? }/**? ? ? +----------------------------------------------------------? ? * 刪除Session? ? ? +----------------------------------------------------------? ? *@accesspublic? ? ? +----------------------------------------------------------? ? *@paramstring $sessID? ? ? +----------------------------------------------------------? ? */publicfunctiondestroy($sessID){$this->connect(1);return$this->handler->delete($this->options['prefix'].$sessID);? ? }/**? ? ? +----------------------------------------------------------? ? * Session 垃圾回收? ? ? +----------------------------------------------------------? ? *@accesspublic? ? ? +----------------------------------------------------------? ? *@paramstring $sessMaxLifeTime? ? ? +----------------------------------------------------------? ? */publicfunctiongc($sessMaxLifeTime){returntrue;? ? }/**? ? ? +----------------------------------------------------------? ? * 打開Session? ? ? +----------------------------------------------------------? ? *@accesspublic? ? ? +----------------------------------------------------------? ? *@paramstring $savePath? ? ? *@parammixed $sessName? ? ? ? +----------------------------------------------------------? ? */publicfunctionexecute(){? ? ? ? session_set_save_handler(array(&$this,"open"),array(&$this,"close"),array(&$this,"read"),array(&$this,"write"),array(&$this,"destroy"),array(&$this,"gc")? ? ? ? );? ? }publicfunction__destruct(){if($this->options['persistent'] =='pconnect') {$this->handler->close();? ? ? ? }? ? ? ? session_write_close();? ? }}
config 配置
//SESSION 配置'SESSION_AUTO_START'=>true,//是否開啟session'SESSION_TYPE'=>'Redis',//session 驅(qū)動(dòng)'SESSION_PREFIX'=>'sess_',//session前綴'SESSION_EXPIRE'=>'7200',//session有效期(單位:秒) 0表示永久緩存缔俄,當(dāng)session被訪問時(shí)弛秋,時(shí)間重新計(jì)算器躏。//緩存 配置'DATA_CACHE_TYPE'=>'Redis',//默認(rèn)動(dòng)態(tài)緩存為Redis'DATA_CACHE_PREFIX'=>'Redis_',//緩存前綴'DATA_CACHE_TIME'=>'0',//緩存時(shí)間 0為永久 當(dāng)緩存被訪問時(shí),時(shí)間不重新計(jì)算//Redis 配置'REDIS_RW_SEPARATE'=>true,//Redis讀寫分離 true 開啟'REDIS_HOST'=>'127.0.0.1',//redis服務(wù)器ip蟹略,多臺(tái)用逗號(hào)隔開登失;讀寫分離開啟時(shí),第一臺(tái)負(fù)責(zé)寫挖炬,其它[隨機(jī)]負(fù)責(zé)讀揽浙;'REDIS_PORT'=>'6379',//端口號(hào)'REDIS_TIMEOUT'=>'30',//超時(shí)時(shí)間(秒)'REDIS_PERSISTENT'=>false,//是否長(zhǎng)連接 false=短連接'REDIS_AUTH'=>'123456',//AUTH認(rèn)證密碼
測(cè)試
publicfunctionindex(){$test['a']='123';$test['b']='1234';$test['c']['a']='ca123';$test['c']['b']='cb123';? ? ? ? S('test',$test);$_SESSION['test']=$test;? ? ? ? echo"S緩存:";? ? ? ? dump(S('test'));? ? ? ? echo"SESSION:";? ? ? ? dump($_SESSION['test']);exit;}
使用redis管理工具查看
原文地址:http://www.reibang.com/p/57c31e9955ff