@@ -30,9 +30,15 @@ class RedisSessionHandler extends AbstractSessionHandler
3030 */
3131 private $ prefix ;
3232
33+ /**
34+ * @var int Time to live in seconds
35+ */
36+ private $ ttl ;
37+
3338 /**
3439 * List of available options:
35- * * prefix: The prefix to use for the keys in order to avoid collision on the Redis server.
40+ * * prefix: The prefix to use for the keys in order to avoid collision on the Redis server
41+ * * ttl: The time to live in seconds.
3642 *
3743 * @param \Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface|RedisProxy $redis
3844 *
@@ -51,12 +57,13 @@ public function __construct($redis, array $options = [])
5157 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 )));
5258 }
5359
54- if ($ diff = array_diff (array_keys ($ options ), ['prefix ' ])) {
60+ if ($ diff = array_diff (array_keys ($ options ), ['prefix ' , ' ttl ' ])) {
5561 throw new \InvalidArgumentException (sprintf ('The following options are not supported "%s" ' , implode (', ' , $ diff )));
5662 }
5763
5864 $ this ->redis = $ redis ;
5965 $ this ->prefix = $ options ['prefix ' ] ?? 'sf_s ' ;
66+ $ this ->ttl = $ options ['ttl ' ] ?? (int ) ini_get ('session.gc_maxlifetime ' );
6067 }
6168
6269 /**
@@ -72,7 +79,7 @@ protected function doRead($sessionId): string
7279 */
7380 protected function doWrite ($ sessionId , $ data ): bool
7481 {
75- $ result = $ this ->redis ->setEx ($ this ->prefix .$ sessionId , ( int ) ini_get ( ' session.gc_maxlifetime ' ) , $ data );
82+ $ result = $ this ->redis ->setEx ($ this ->prefix .$ sessionId , $ this -> ttl , $ data );
7683
7784 return $ result && !$ result instanceof ErrorInterface;
7885 }
@@ -108,6 +115,6 @@ public function gc($maxlifetime): bool
108115 */
109116 public function updateTimestamp ($ sessionId , $ data )
110117 {
111- return (bool ) $ this ->redis ->expire ($ this ->prefix .$ sessionId , ( int ) ini_get ( ' session.gc_maxlifetime ' ) );
118+ return (bool ) $ this ->redis ->expire ($ this ->prefix .$ sessionId , $ this -> ttl );
112119 }
113120}
0 commit comments