[HttpFoundation] Added possibility to configure expiration time in redis session handler
This commit is contained in:
parent
1374abd6e8
commit
4a9d947b1a
|
@ -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
|
||||
*
|
||||
|
@ -51,12 +57,13 @@ class RedisSessionHandler extends AbstractSessionHandler
|
|||
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');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -72,7 +79,7 @@ class RedisSessionHandler extends AbstractSessionHandler
|
|||
*/
|
||||
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;
|
||||
}
|
||||
|
@ -108,6 +115,6 @@ class RedisSessionHandler extends AbstractSessionHandler
|
|||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -139,7 +139,37 @@ abstract class AbstractRedisSessionHandlerTestCase extends TestCase
|
|||
{
|
||||
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],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
Reference in New Issue