Skip to content

Commit

Permalink
feature #34405 [HttpFoundation] Added possibility to configure expira…
Browse files Browse the repository at this point in the history
…tion time in redis session handler (mantulo)

This PR was squashed before being merged into the 4.4 branch (closes #34405).

Discussion
----------

[HttpFoundation] Added possibility to configure expiration time in redis session handler

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       |  n/a
| License       | MIT
| Doc PR        | n/a

Add possibility to manually configure expiration time in redis session handler.

Commits
-------

4a9d947b1a [HttpFoundation] Added possibility to configure expiration time in redis session handler
  • Loading branch information
fabpot committed Nov 17, 2019
2 parents c351789 + 0c5217a commit a558b18
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|RedisClusterProxy $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 a558b18

Please sign in to comment.