EasySwoole Session 組件
由于在 Swoole 協(xié)程下,php 自帶的 session 函數(shù)是不能使用的。為此,EasySwoole 提供了獨立的 session 組件,實現(xiàn) php 的 session 功能。
Session 組件目前最新穩(wěn)定版本為 3.x。針對 2.x 版本的組件使用文檔請看 Session 2.x,其他舊版本的組件使用文檔請以 Github 為準(zhǔn)。
組件要求
- php: >=7.1.0
- easyswoole/spl: ^1.3
- easyswoole/utility: ^1.1
- easyswoole/component: ^2.1
安裝方法
從框架 3.4.4 版本開始,框架默認(rèn)自帶該組件,不用再次再裝,其他版本請使用 composer 安裝,安裝方法如下所示。
composer require easyswoole/session=3.x
倉庫地址
基本使用
注冊 session handler
使用 session 前,需要先注冊 session handler。接下來的示例使用的 session handler 是 EasySwoole 內(nèi)置的 session handler,開箱即用。
注冊步驟如下:
- 首先我們定義一個
session工具類繼承自session組件的\EasySwoole\EasySwoole\Session類。用戶可以自行定義類繼承并實現(xiàn)。下面為提供的一個參考工具類。
新增 App\Tools\Session.php,內(nèi)容如下:
<?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。
具體實現(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');
// 可以自己實現(xiàn)一個標(biāo)準(zhǔn)的 session handler,下面使用組件內(nèi)置實現(xiàn)的 session handler
// 基于文件存儲,傳入 EASYSWOOLE_TEMP_DIR . '/Session' 目錄作為 session 數(shù)據(jù)文件存儲位置
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 回調(diào),相當(dāng)于原來的 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);
}
// 存儲 sessionId 方便調(diào)用,也可以通過其它方式存儲
$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: 注冊 HTTP_GLOBAL_AFTER_REQUEST 回調(diào),相當(dāng)于原來的 afterRequest 事件
// session 數(shù)據(jù)落地【必不可少這一步】
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());
// 響應(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());
}
}
然后訪問 http://127.0.0.1:9501/session/set (示例請求地址)就可以進(jìn)行測試設(shè)置 session,訪問 http://127.0.0.1:9501/session/flush (示例請求地址)就可以清空所有 session 數(shù)據(jù)。其他示例請用戶自行測試。