提示,因为协程客户端问题,该方式其实不太可用,类似组件应该已经在官方库中存在.

实现SessionHandler

和EasySwoole 2.x的实现方法接近,只是调用和配置略微有点不同,参考我提交到2.x的文档。
EasySwoole 2.x RedisSessionHandler 访问链接

<?php


namespace App\Utility;


use EasySwoole\EasySwoole\Config;

use Redis;

class RedisSession extends SessionHandler implements \SessionHandlerInterface
{
    private $options = [
        'handler' => null,
        'host' => null,
        'port' => null,
        'lifetime' => null,
        'prefix' => '',
    ];

    /**
     * @return bool
     */
    public function close()
    {
        return $this->options['handler']->close();
        // TODO: Implement close() method.
    }

    /**
     * @param string $session_id
     * @return bool
     */
    public function destroy($session_id): bool
    {
        $session_id = $this->options['prefix'] . $session_id;
        return $this->options['handler']->delete($session_id) >= 1 ? true : false;
    }

    /**
     * @param int $maxlifetime
     * @return bool
     */
    public function gc($maxlifetime): bool
    {
        // TODO: Implement gc() method.
        return true;
    }

    /**
     * @param $save_path
     * @param $name
     * @return bool
     */
    public function open($save_path, $name): bool
    {
        $conf = Config::getInstance()->getConf('redis'); //配置文件需要自行设置好加载和调用
        $this->options['host'] = $conf['host'];
        $this->options['port'] = $conf['port'];
        $this->options['prefix'] = $name . '_';
        $set = Config::getInstance()->getConf('session');
        if (!empty($set)) {
            $maxLifeTime = $set['lifetime'];
        } else {
            $maxLifeTime = 3600 * 24 * 30;
        }
        $this->options['lifeTime'] = $maxLifeTime;
        if (is_resource($this->options['handler'])) return true;
        //连接redis
        $redisHandle = new Redis();
        $redisHandle->connect($this->options['host'], $this->options['port']);
        if (!$redisHandle) {
            return false;
        }
        $this->options['handler'] = $redisHandle;
        return true;
    }

    /**
     * @param string $session_id
     * @return string
     */
    public function read($session_id)
    {
        $session_id = $this->options['prefix'] . $session_id;
        return $this->options['handler']->get($session_id);
    }

    /**
     * @param string $session_id
     * @param string $session_data
     * @return bool
     */
    public function write($session_id, $session_data)
    {
        $session_id = $this->options['prefix'] . $session_id;
        return $this->options['handler']->setex($session_id, $this->options['lifeTime'], $session_data);
    }

}

调用SessionHandler

直接在控制器进行session start时,使用该RedisSession作为参数传给session方法即可

protected function onRequest(?string $action): ?bool
    {
        $this->session(new RedisSession())->start();
        return true;
    }

public function testSession()
    {
        $this->session()->set('a', 1);
        $res = $this->session()->get('a');
        $this->response()->write($res);
    }