Skip to content

Commit

Permalink
[HttpFoundation] Added possibility to configure expiration time in re…
Browse files Browse the repository at this point in the history
…dis session handler
  • Loading branch information
mantulo authored and fabpot committed Nov 17, 2019
1 parent e6e2b4e commit 0c5217a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
15 changes: 11 additions & 4 deletions Session/Storage/Handler/RedisSessionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,15 @@ class RedisSessionHandler extends AbstractSessionHandler
*/
private $prefix;

/**
* @var int Time to live in seconds
*/
private $ttl;

/**
* List of available options:
* * prefix: The prefix to use for the keys in order to avoid collision on the Redis server.
* * prefix: The prefix to use for the keys in order to avoid collision on the Redis server
* * ttl: The time to live in seconds.
*
* @param \Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface|RedisProxy $redis
*
Expand All @@ -51,12 +57,13 @@ public function __construct($redis, array $options = [])
throw new \InvalidArgumentException(sprintf('%s() expects parameter 1 to be Redis, RedisArray, RedisCluster or Predis\ClientInterface, %s given', __METHOD__, \is_object($redis) ? \get_class($redis) : \gettype($redis)));
}

if ($diff = array_diff(array_keys($options), ['prefix'])) {
if ($diff = array_diff(array_keys($options), ['prefix', 'ttl'])) {
throw new \InvalidArgumentException(sprintf('The following options are not supported "%s"', implode(', ', $diff)));
}

$this->redis = $redis;
$this->prefix = $options['prefix'] ?? 'sf_s';
$this->ttl = $options['ttl'] ?? (int) ini_get('session.gc_maxlifetime');
}

/**
Expand All @@ -72,7 +79,7 @@ protected function doRead($sessionId): string
*/
protected function doWrite($sessionId, $data): bool
{
$result = $this->redis->setEx($this->prefix.$sessionId, (int) ini_get('session.gc_maxlifetime'), $data);
$result = $this->redis->setEx($this->prefix.$sessionId, $this->ttl, $data);

return $result && !$result instanceof ErrorInterface;
}
Expand Down Expand Up @@ -108,6 +115,6 @@ public function gc($maxlifetime): bool
*/
public function updateTimestamp($sessionId, $data)
{
return (bool) $this->redis->expire($this->prefix.$sessionId, (int) ini_get('session.gc_maxlifetime'));
return (bool) $this->redis->expire($this->prefix.$sessionId, $this->ttl);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,37 @@ public function getOptionFixtures(): array
{
return [
[['prefix' => 'session'], true],
[['ttl' => 1000], true],
[['prefix' => 'sfs', 'ttl' => 1000], true],
[['prefix' => 'sfs', 'foo' => 'bar'], false],
[['ttl' => 'sfs', 'foo' => 'bar'], false],
];
}

/**
* @dataProvider getTtlFixtures
*/
public function testUseTtlOption(int $ttl)
{
$options = [
'prefix' => self::PREFIX,
'ttl' => $ttl,
];

$handler = new RedisSessionHandler($this->redisClient, $options);
$handler->write('id', 'data');
$redisTtl = $this->redisClient->ttl(self::PREFIX.'id');

$this->assertLessThan($redisTtl, $ttl - 5);
$this->assertGreaterThan($redisTtl, $ttl + 5);
}

public function getTtlFixtures(): array
{
return [
['ttl' => 5000],
['ttl' => 120],
['ttl' => 60],
];
}
}

0 comments on commit 0c5217a

Please sign in to comment.