From 9840dc7f548559a064dccc6210c4dd0f5213a9ce Mon Sep 17 00:00:00 2001 From: Niklas Keller Date: Sun, 20 Aug 2023 19:14:51 +0200 Subject: [PATCH] Fix compatibility with amphp/redis 2.x --- src/RedisSessionStorage.php | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/RedisSessionStorage.php b/src/RedisSessionStorage.php index 1aa4c33..baceffd 100644 --- a/src/RedisSessionStorage.php +++ b/src/RedisSessionStorage.php @@ -2,9 +2,8 @@ namespace Amp\Http\Server\Session; -use Amp\Redis\QueryExecutor; -use Amp\Redis\Redis; -use Amp\Redis\RedisSetOptions; +use Amp\Redis\Command\Option\SetOptions; +use Amp\Redis\RedisClient; use Amp\Serialization\CompressingSerializer; use Amp\Serialization\NativeSerializer; use Amp\Serialization\Serializer; @@ -13,25 +12,22 @@ final class RedisSessionStorage implements SessionStorage { public const DEFAULT_SESSION_LIFETIME = 3600; - private readonly Redis $redis; - - private readonly RedisSetOptions $setOptions; + private readonly SetOptions $setOptions; public function __construct( - QueryExecutor $executor, + private readonly RedisClient $client, private readonly Serializer $serializer = new CompressingSerializer(new NativeSerializer()), private readonly int $sessionLifetime = self::DEFAULT_SESSION_LIFETIME, private readonly string $keyPrefix = 'session:' ) { - $this->redis = new Redis($executor); - $this->setOptions = (new RedisSetOptions())->withTtl($this->sessionLifetime); + $this->setOptions = (new SetOptions())->withTtl($this->sessionLifetime); } public function write(string $id, array $data): void { if (empty($data)) { try { - $this->redis->delete($this->keyPrefix . $id); + $this->client->delete($this->keyPrefix . $id); } catch (\Throwable $error) { throw new SessionException("Couldn't delete session '{$id}''", 0, $error); } @@ -46,7 +42,7 @@ public function write(string $id, array $data): void } try { - $this->redis->set($this->keyPrefix . $id, $serializedData, $this->setOptions); + $this->client->set($this->keyPrefix . $id, $serializedData, $this->setOptions); } catch (\Throwable $error) { throw new SessionException("Couldn't persist data for session '{$id}'", 0, $error); } @@ -55,7 +51,7 @@ public function write(string $id, array $data): void public function read(string $id): array { try { - $result = $this->redis->get($this->keyPrefix . $id); + $result = $this->client->get($this->keyPrefix . $id); } catch (\Throwable $error) { throw new SessionException("Couldn't read data for session '{$id}'", 0, $error); } @@ -71,7 +67,7 @@ public function read(string $id): array } try { - $this->redis->expireIn($this->keyPrefix . $id, $this->sessionLifetime); + $this->client->expireIn($this->keyPrefix . $id, $this->sessionLifetime); } catch (\Throwable $error) { throw new SessionException("Couldn't renew expiry for session '{$id}'", 0, $error); }