Skip to content

Commit

Permalink
fix: redis session gc
Browse files Browse the repository at this point in the history
  • Loading branch information
mcharytoniuk committed Apr 22, 2024
1 parent fc62796 commit ae97156
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Distantmagic\Resonance;

use Ds\Map;
use Redis;

readonly class Session
{
Expand All @@ -24,15 +23,25 @@ public function __construct(

public function persist(): void
{
$this->getRedisConnection()->set(
$redisConnection = new RedisConnection(
$this->redisConnectionPoolRepository,
$this->getRedisPrefix(),
);

$redisConnection->redis->set(
$this->id,
$this->serializer->serialize($this->data->toArray())
);
}

private function getPersistedData(): mixed
{
$storedValue = $this->getRedisConnection()->get($this->id);
$redisConnection = new RedisConnection(
$this->redisConnectionPoolRepository,
$this->getRedisPrefix(),
);

$storedValue = $redisConnection->redis->get($this->id);

if (!is_string($storedValue) || empty($storedValue)) {
return null;
Expand All @@ -41,20 +50,15 @@ private function getPersistedData(): mixed
return $this->serializer->unserialize($storedValue);
}

private function getRedisConnection(): Redis
private function getRedisPrefix(): string
{
$redisPrefix = $this->redisConfiguration
->connectionPoolConfiguration
->get($this->sessionConfiguration->redisConnectionPool)
->prefix
;

$redisConnection = new RedisConnection(
redisConnectionPoolRepository: $this->redisConnectionPoolRepository,
redisPrefix: $redisPrefix.'session:',
);

return $redisConnection->redis;
return $redisPrefix.'.session';
}

private function restoreSessionData(): array
Expand Down

0 comments on commit ae97156

Please sign in to comment.