Skip to content

Commit

Permalink
fix: do not keep session connection opened
Browse files Browse the repository at this point in the history
  • Loading branch information
mcharytoniuk committed Apr 22, 2024
1 parent 71d5891 commit 326d607
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 23 deletions.
39 changes: 39 additions & 0 deletions src/RedisConnection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Distantmagic\Resonance;

use Redis;

readonly class RedisConnection
{
public Redis $redis;

public function __construct(
RedisConfiguration $redisConfiguration,
private RedisConnectionPoolRepository $redisConnectionPoolRepository,
string $redisPrefix,

Check failure on line 16 in src/RedisConnection.php

View workflow job for this annotation

GitHub Actions / psalm

PossiblyUnusedParam

src/RedisConnection.php:16:16: PossiblyUnusedParam: Param #3 is never referenced in this method (see https://psalm.dev/134)
private string $connectionPoolName = 'default',
) {
$redisPrefix = $redisConfiguration
->connectionPoolConfiguration
->get($connectionPoolName)
->prefix
;

$this->redis = $this
->redisConnectionPoolRepository
->getConnection($connectionPoolName)
;
$this->redis->setOption(Redis::OPT_PREFIX, $redisPrefix);
}

public function __destruct()
{
$this->redisConnectionPoolRepository->putConnection(
$this->connectionPoolName,
$this->redis,
);
}
}
43 changes: 20 additions & 23 deletions src/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,49 +10,29 @@
readonly class Session
{
public Map $data;
private Redis $redis;

public function __construct(
RedisConfiguration $redisConfiguration,
private RedisConfiguration $redisConfiguration,
private RedisConnectionPoolRepository $redisConnectionPoolRepository,
private SerializerInterface $serializer,
private SessionConfiguration $sessionConfiguration,
public string $id,
) {
$redisPrefix = $redisConfiguration
->connectionPoolConfiguration
->get($this->sessionConfiguration->redisConnectionPool)
->prefix
;

$this->redis = $this
->redisConnectionPoolRepository
->getConnection($this->sessionConfiguration->redisConnectionPool)
;
$this->redis->setOption(Redis::OPT_PREFIX, $redisPrefix.'session:');

$this->data = new Map($this->restoreSessionData());
}

public function __destruct()
{
$this->redisConnectionPoolRepository->putConnection(
$this->sessionConfiguration->redisConnectionPool,
$this->redis,
);
}

public function persist(): void
{
$this->redis->set(
$this->getRedisConnection()->set(
$this->id,
$this->serializer->serialize($this->data->toArray())
);
}

private function getPersistedData(): mixed
{
$storedValue = $this->redis->get($this->id);
$storedValue = $this->getRedisConnection()->get($this->id);

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

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

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

return $redisConnection->redis;
}

private function restoreSessionData(): array
{
$stored = $this->getPersistedData();
Expand Down

0 comments on commit 326d607

Please sign in to comment.