💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、豆包、星火、月之暗面及文生图、文生视频 广告
## [更改session存储引擎](https://www.workerman.net/doc/workerman/http/session-control.html#%E6%9B%B4%E6%94%B9session%E5%AD%98%E5%82%A8%E5%BC%95%E6%93%8E) workerman为session提供了文件存储引擎和redis存储引擎。默认使用文件存储引擎。如果想更改为redis存储引擎,请参考如下代码。 ~~~php <?php use Workerman\Worker; use Workerman\Protocols\Http\Session; use Workerman\Protocols\Http\Session\RedisSessionHandler; use Workerman\Connection\TcpConnection; use Workerman\Protocols\Http\Request; require_once __DIR__ . '/vendor/autoload.php'; $worker = new Worker('http://0.0.0.0:8080'); // redis配置 $config = [ 'host' => '127.0.0.1', // 必选参数 'port' => 6379, // 必选参数 'timeout' => 2, // 可选参数 'auth' => '******', // 可选参数 'database' => 1, // 可选参数 'prefix' => 'session_' // 可选参数 ]; // 使用 Workerman\Protocols\Http\Session::handlerClass方法来更改session底层驱动类 Session::handlerClass(RedisSessionHandler::class, $config); $worker->onMessage = function(TcpConnection $connection, Request $request) { $session = $request->session(); $session->set('somekey', rand()); $connection->send($session->get('somekey')); }; Worker::runAll(); ~~~ ## [设置session存储位置](https://www.workerman.net/doc/workerman/http/session-control.html#%E8%AE%BE%E7%BD%AEsession%E5%AD%98%E5%82%A8%E4%BD%8D%E7%BD%AE) 使用默认存储引擎时session数据默认存储在磁盘中,默认位置为`session_save_path()`的返回的位置。 你可以使用以下方法改变存储位置。 ~~~php use Workerman\Worker; use \Workerman\Protocols\Http\Session\FileSessionHandler; use Workerman\Connection\TcpConnection; use Workerman\Protocols\Http\Request; require_once __DIR__ . '/vendor/autoload.php'; // 设置session文件存储位置 FileSessionHandler::sessionSavePath('/tmp/session'); $worker = new Worker('http://0.0.0.0:8080'); $worker->onMessage = function(TcpConnection $connection, Request $request) { $session = $request->session(); $session->set('name', 'tome'); $connection->send($session->get('name')); }; // 运行worker Worker::runAll(); ~~~ ## [session文件清理](https://www.workerman.net/doc/workerman/http/session-control.html#session%E6%96%87%E4%BB%B6%E6%B8%85%E7%90%86) 使用默认session存储引擎时磁盘上会有多个session文件, workerman 会根据php.ini中设置的`session.gc_probability``session.gc_divisor``session.gc_maxlifetime`选项清理过期的session文件。关于这三个选项说明参见[php手册](https://www.php.net/manual/zh/session.configuration.php#ini.session.gc-probability) ## [更改存储驱动](https://www.workerman.net/doc/workerman/http/session-control.html#%E6%9B%B4%E6%94%B9%E5%AD%98%E5%82%A8%E9%A9%B1%E5%8A%A8) 除了文件session存储引擎和redis session存储引擎,workerman允许你通过标准的[SessionHandlerInterface](https://www.php.net/manual/zh/class.sessionhandlerinterface.php)接口添加新的session存储引擎,比如mangoDb session存储引擎、MySQL session存储引擎等。 **添加新的session存储引擎流程** 1. 实现[SessionHandlerInterface](https://www.php.net/manual/zh/class.sessionhandlerinterface.php)接口 2. 使用`Workerman\Protocols\Http\Session::handlerClass($class_name, $config)`方法替换底层SessionHandler接口 **实现 SessionHandlerInterface 接口** 自定义session存储驱动须实现 SessionHandlerInterface 接口。这个接口包含以下方法: ~~~php SessionHandlerInterface { /* Methods */ abstract public read ( string $session_id ) : string abstract public write ( string $session_id , string $session_data ) : bool abstract public destroy ( string $session_id ) : bool abstract public gc ( int $maxlifetime ) : int abstract public close ( void ) : bool abstract public open ( string $save_path , string $session_name ) : bool } ~~~ **SessionHandlerInterface 说明** * read方法用来从存储中读取session\_id对应的所有session数据。请不要对数据进行反序列化操作,框架会自动完成。 * write方法用来向存储写入session\_id对应的session数据。请不要对数据进行序列化操作,框架已经自动完成。 * destroy方法用来销毁session\_id对应的session数据。 * gc方法用来删除过期的session数据,存储应该对最后修改时间大于maxlifetime的所有session执行删除操作 * close 无需任何操作,直接返回true即可 * open 无需任何操作,直接返回true接口 **替换底层驱动** 实现完SessionHandlerInterface接口后,使用以下方法更改session底层驱动。 ~~~php Workerman\Protocols\Http\Session::handlerClass($class_name, $config); ~~~ * $class\_name 为实现SessionHandlerInterface接口的SessionHandler类的名字。如果有命名空间则需要带上完整的命名空间 * $config 为SessionHandler类的构造函数的参数 **具体实现** *注意,这个MySessionHandler类仅仅为了说明更改session底层驱动的流程,MySessionHandler并不能用于生产环境。* ~~~php <?php use Workerman\Worker; use Workerman\Protocols\Http\Session; use Workerman\Connection\TcpConnection; use Workerman\Protocols\Http\Request; require_once __DIR__ . '/vendor/autoload.php'; $worker = new Worker('http://0.0.0.0:8080'); class MySessionHandler implements SessionHandlerInterface { protected static $store = []; public function __construct($config) { // ['host' => 'localhost'] var_dump($config); } public function open($save_path, $name) { return true; } public function read($session_id) { return isset(static::$store[$session_id]) ? static::$store[$session_id]['content'] : ''; } public function write($session_id, $session_data) { static::$store[$session_id] = ['content' => $session_data, 'timestamp' => time()]; } public function close() { return true; } public function destroy($session_id) { unset(static::$store[$session_id]); return true; } public function gc($maxlifetime) { $time_now = time(); foreach (static::$store as $session_id => $info) { if ($time_now - $info['timestamp'] > $maxlifetime) { unset(static::$store[$session_id]); } } } } //  假设新实现的SessionHandler类需要一些配置传入 $config = ['host' => 'localhost']; // 使用 Workerman\Protocols\Http\Session::handlerClass($class_name, $config) 来更改session底层驱动类 Session::handlerClass(MySessionHandler::class, $config); $worker->onMessage = function(TcpConnection $connection, Request $request) { $session = $request->session(); $session->set('somekey', rand()); $connection->send($session->get('somekey')); }; Worker::runAll(); ~~~