PHP使用Redis實現(xiàn)Session共享的實現(xiàn)示例_第1頁
PHP使用Redis實現(xiàn)Session共享的實現(xiàn)示例_第2頁
PHP使用Redis實現(xiàn)Session共享的實現(xiàn)示例_第3頁
PHP使用Redis實現(xiàn)Session共享的實現(xiàn)示例_第4頁
PHP使用Redis實現(xiàn)Session共享的實現(xiàn)示例_第5頁
全文預覽已結束

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

第PHP使用Redis實現(xiàn)Session共享的實現(xiàn)示例小型web服務,session數(shù)據基本是保存在本地(更多是本地磁盤文件),但是當部署多臺服務,且需要共享session,確保每個服務都能共享到同一份session數(shù)據.

redis數(shù)據存儲在內存中,性能好,配合持久化可確保數(shù)據完整.

設計方案

1.通過php自身session配置實現(xiàn)

#使用redis作為存儲方案

session.save_handler=redis

session.save_path="tcp://:6379"

#若設置了連接密碼,則使用如下

session.save_path="tcp://:6379auth=密碼"

測試代碼

ini_set("session.save_handler","redis");

ini_set("session.save_path","tcp://:6379");

session_start();

echo"pre

$_SESSION['usertest'.rand(1,5)]=1;

var_dump($_SESSION);

echo"/pre

輸出↓

array(2){

["usertest1"]=

int(88)

["usertest3"]=

int(1)

}

usertest1|i:1;usertest3|i:1;

評價

優(yōu)點:實現(xiàn)簡單,無需修改php代碼

缺點:配置不支持多樣化,只能應用于簡單場景

2.設置用戶自定義會話存儲函數(shù)

通過session_set_save_handler()函數(shù)設置用戶自定義會話函數(shù).

session_set_save_handler(callable$open,callable$close,callable$read,callable$write,callable$destroy,callable$gc[,callable$create_sid[,callable$validate_sid[,callable$update_timestamp]]]):bool

#=php5.4

session_set_save_handler(object$sessionhandler[,bool$register_shutdown=TRUE]):bool

在配置完會話存儲函數(shù)后,再執(zhí)行session_start()即可.

具體代碼略,以下提供一份Memcached的(來自Symfony框架代碼):

*ThisfileispartoftheSymfonypackage.

*(c)FabienPotencierfabien@

*Forthefullcopyrightandlicenseinformation,pleaseviewtheLICENSE

*filethatwasdistributedwiththissourcecode.

namespaceSymfony\Component\HttpFoundation\Session\Storage\Handler;

*MemcacheSessionHandler.

*@authorDrakdrak@

classMemcacheSessionHandlerimplements\SessionHandlerInterface

*@var\MemcacheMemcachedriver.

private$memcache;

*@varintTimetoliveinseconds

private$ttl;

*@varstringKeyprefixforsharedenvironments.

private$prefix;

*Constructor.

*Listofavailableoptions:

**prefix:Theprefixtouseforthememcachekeysinordertoavoidcollision

**expiretime:Thetimetoliveinseconds

*@param\Memcache$memcacheA\Memcacheinstance

*@paramarray$optionsAnassociativearrayofMemcacheoptions

*@throws\InvalidArgumentExceptionWhenunsupportedoptionsarepassed

publicfunction__construct(\Memcache$memcache,array$options=array())

if($diff=array_diff(array_keys($options),array('prefix','expiretime'))){

thrownew\InvalidArgumentException(sprintf(

'Thefollowingoptionsarenotsupported"%s"',implode(',',$diff)

$this-memcache=$memcache;

$this-ttl=isset($options['expiretime'])(int)$options['expiretime']:86400;

$this-prefix=isset($options['prefix'])$options['prefix']:'sf2s';

*{@inheritdoc}

publicfunctionopen($savePath,$sessionName)

returntrue;

*{@inheritdoc}

publicfunctionclose()

return$this-memcache-close();

*{@inheritdoc}

publicfunctionread($sessionId)

return$this-memcache-get($this-prefix.$sessionId):'';

*{@inheritdoc}

publicfunctionwrite($sessionId,$data)

return$this-memcache-set($this-prefix.$sessionId,$data,0,time()+$this-ttl);

*{@inheritdoc}

publicfunctiondestroy($sessionId)

return$this-memcache-delete($this-prefix.$sessionId);

*{@inheritdoc}

publicfunctiongc($maxlifetime)

//notrequiredherebecausememcachewillautoexpiretherecordsanyhow

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論