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