EasySwoole Session 組件
由于在 Swoole
協(xié)程下,php
自帶的 session
函數(shù)是不能使用的。為此,EasySwoole
提供了獨(dú)立的 session
組件,實(shí)現(xiàn) php
的 session
功能。
Session
組件目前最新穩(wěn)定版本為 3.x
。針對(duì) 2.x
版本的組件使用文檔請(qǐng)看 Session 2.x,其他舊版本的組件使用文檔請(qǐng)以 Github 為準(zhǔn)。
組件要求
- php: >=7.1.0
- easyswoole/spl: ^1.3
- easyswoole/utility: ^1.1
- easyswoole/component: ^2.1
安裝方法
從框架 3.4.4
版本開(kāi)始,框架默認(rèn)自帶該組件,不用再次再裝,其他版本請(qǐng)使用 composer 安裝,安裝方法如下所示。
composer require easyswoole/session=3.x
倉(cāng)庫(kù)地址
基本使用
注冊(cè) session handler
使用 session
前,需要先注冊(cè) session handler
。接下來(lái)的示例使用的 session handler
是 EasySwoole
內(nèi)置的 session handler
,開(kāi)箱即用。
注冊(cè)步驟如下:
- 首先我們定義一個(gè)
session
工具類繼承自session
組件的\EasySwoole\EasySwoole\Session
類。用戶可以自行定義類繼承并實(shí)現(xiàn)。下面為提供的一個(gè)參考工具類。
新增 App\Tools\Session.php
,內(nèi)容如下:
<?php
namespace App\Tools;
use EasySwoole\Component\Singleton;
class Session extends \EasySwoole\Session\Session
{
use Singleton;
}
- 注冊(cè)
session handler
。修改EasySwoole
全局event
文件(即框架根目錄的EasySwooleEvent.php
文件),在mainServerCreate
全局事件和HTTP
的 全局HTTP_GLOBAL_ON_REQUEST
事件中注冊(cè)session handler
。
具體實(shí)現(xiàn)代碼如下:
<?php
namespace EasySwoole\EasySwoole;
use App\Tools\Session;
use EasySwoole\Component\Di;
use EasySwoole\EasySwoole\AbstractInterface\Event;
use EasySwoole\EasySwoole\Swoole\EventRegister;
use EasySwoole\Http\Request;
use EasySwoole\Http\Response;
use EasySwoole\Session\FileSession;
use EasySwoole\Utility\Random;
class EasySwooleEvent implements Event
{
public static function initialize()
{
date_default_timezone_set('Asia/Shanghai');
// 可以自己實(shí)現(xiàn)一個(gè)標(biāo)準(zhǔn)的 session handler,下面使用組件內(nèi)置實(shí)現(xiàn)的 session handler
// 基于文件存儲(chǔ),傳入 EASYSWOOLE_TEMP_DIR . '/Session' 目錄作為 session 數(shù)據(jù)文件存儲(chǔ)位置
Session::getInstance(new FileSession(EASYSWOOLE_TEMP_DIR . '/Session'));
Di::getInstance()->set(SysConst::HTTP_GLOBAL_ON_REQUEST, function (Request $request, Response $response) {
// TODO: 注冊(cè) HTTP_GLOBAL_ON_REQUEST 回調(diào),相當(dāng)于原來(lái)的 onRequest 事件
// 獲取客戶端 Cookie 中 easy_session 參數(shù)
$sessionId = $request->getCookieParams('easy_session');
if (!$sessionId) {
$sessionId = Random::character(32); // 生成 sessionId
// 設(shè)置向客戶端響應(yīng) Cookie 中 easy_session 參數(shù)
$response->setCookie('easy_session', $sessionId);
}
// 存儲(chǔ) sessionId 方便調(diào)用,也可以通過(guò)其它方式存儲(chǔ)
$request->withAttribute('easy_session', $sessionId);
Session::getInstance()->create($sessionId); // 創(chuàng)建并返回該 sessionId 的 context
});
Di::getInstance()->set(SysConst::HTTP_GLOBAL_AFTER_REQUEST, function (Request $request, Response $response) {
// TODO: 注冊(cè) HTTP_GLOBAL_AFTER_REQUEST 回調(diào),相當(dāng)于原來(lái)的 afterRequest 事件
// session 數(shù)據(jù)落地【必不可少這一步】
Session::getInstance()->close($request->getAttribute('easy_session'));
// gc 會(huì)清除所有 session,切勿操作
// Session::getInstance()->gc(time());
});
}
public static function mainServerCreate(EventRegister $register)
{
}
}
在 EasySwoole
中使用 session
注冊(cè) session handler
之后,我們就可以在 EasySwoole 控制器
的任意位置使用了。
簡(jiǎn)單使用示例代碼如下:
<?php
namespace App\HttpController;
use EasySwoole\Http\AbstractInterface\Controller;
use EasySwoole\Session\Context;
class Session extends Controller
{
protected function session(): ?Context
{
// 封裝一個(gè)方法,方便我們快速獲取 session context
$sessionId = $this->request()->getAttribute('easy_session');
return \App\Tools\Session::getInstance()->create($sessionId);
}
// 將值保存在 session 中
public function set()
{
// $this->session()->set('key', 'value');
// 把 'test_session_key' 作為鍵,time() 的值作為值,保存在 session 中
$this->session()->set('test_session_key', time());
// 響應(yīng)客戶端
$this->writeJson(200, 'success!');
}
// 獲取 session 中的值
public function get()
{
// $this->session()->get('key');
// 從 session 中獲取 key 為 'test_session_key' 的值
$ret = $this->session()->get('test_session_key');
// 響應(yīng)客戶端
$this->writeJson(200, $ret);
}
// 獲取 session 中所有數(shù)據(jù)
public function all()
{
// 獲取 session 中所有數(shù)據(jù)
$ret = $this->session()->allContext();
// 響應(yīng)客戶端
$this->writeJson(200, $ret);
}
// 刪除 session 中的值
public function del()
{
// $this->session()->del('key');
// 刪除 session 中 key 為 'test_session_key' 的值
$this->session()->del('test_session_key');
// 再次獲取 session 中所有數(shù)據(jù)并響應(yīng)客戶端
$this->writeJson(200, $this->session()->allContext());
}
// 清空 session 中所有數(shù)據(jù)
public function flush()
{
// 清空 session 中所有數(shù)據(jù)
$this->session()->flush();
// 再次獲取 session 中所有數(shù)據(jù)并響應(yīng)客戶端
$this->writeJson(200, $this->session()->allContext());
}
// 重新設(shè)置(覆蓋) session 中的數(shù)據(jù)
public function setData()
{
// 重新設(shè)置(覆蓋) session 中的數(shù)據(jù)
$ret = $this->session()->setData([
'test_session_key' => 1,
'test_session_key1' => 2
]);
// 再次獲取 session 中所有數(shù)據(jù)并響應(yīng)給客戶端
$this->writeJson(200, $ret->allContext());
}
}
然后訪問(wèn) http://127.0.0.1:9501/session/set
(示例請(qǐng)求地址)就可以進(jìn)行測(cè)試設(shè)置 session
,訪問(wèn) http://127.0.0.1:9501/session/flush
(示例請(qǐng)求地址)就可以清空所有 session
數(shù)據(jù)。其他示例請(qǐng)用戶自行測(cè)試。