Skip to content

Commit 35931bd

Browse files
committed
WIP [HttpFoundation] Add RedisSessionHandler
1 parent 99e31fd commit 35931bd

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

src/Symfony/Component/HttpFoundation/Session/Storage/Handler/RedisSessionHandler.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,26 +41,25 @@ class RedisSessionHandler extends AbstractSessionHandler
4141

4242
/**
4343
* List of available options:
44-
* * prefix: The prefix to use for the Redis keys in order to avoid collision
45-
* * expiretime: The time to live in seconds.
44+
* * prefix: The prefix to use for the keys in order to avoid collision
45+
* * expiretime: The time to live in seconds. Defaults to 1 day.
4646
*
47-
* @param \Redis $redis A \Redis instance
48-
* @param array $options An associative array of Redis options
47+
* @param \Redis $redis A \Redis instance
48+
* @param null|array $options An associative array of options
4949
*
5050
* @throws \InvalidArgumentException When unsupported options are passed
5151
*/
52-
public function __construct(\Redis $redis, array $options = null)
52+
public function __construct(\Redis $redis, ?array $options = null)
5353
{
5454
$this->redis = $redis;
55+
$options = (array) $options;
5556

5657
if ($diff = array_diff(array_keys($options), array('prefix', 'expiretime'))) {
57-
throw new \InvalidArgumentException(sprintf(
58-
'The following options are not supported "%s"', implode(', ', $diff)
59-
));
58+
throw new \InvalidArgumentException(sprintf('The following options are not supported "%s"', implode(', ', $diff)));
6059
}
6160

6261
$this->ttl = isset($options['expiretime']) ? (int) $options['expiretime'] : 86400;
63-
$this->prefix = isset($options['prefix']) ? $options['prefix'] : 'sf2s';
62+
$this->prefix = $options['prefix'] ?? 'sf_s';
6463
}
6564

6665
/**
@@ -76,18 +75,17 @@ protected function doRead($sessionId)
7675
*/
7776
protected function doWrite($sessionId, $data)
7877
{
79-
return $this->redis->set($this->prefix.$sessionId, $data, $this->ttl);
78+
return $this->redis->setEx($this->prefix.$sessionId, $this->ttl, $data);
8079
}
8180

8281
/**
8382
* {@inheritdoc}
8483
*/
8584
protected function doDestroy($sessionId)
8685
{
87-
// number of deleted items
88-
$count = $this->redis->del($this->prefix.$sessionId);
86+
$this->redis->delete($this->prefix.$sessionId);
8987

90-
return 1 === $count;
88+
return true;
9189
}
9290

9391
/**

src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/RedisSessionHandlerTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class RedisSessionHandlerTest extends TestCase
2929
protected $storage;
3030

3131
/**
32-
* @var \PHPUnit_Framework_MockObject_MockObject|\Redis $redis
32+
* @var \PHPUnit_Framework_MockObject_MockObject|\Redis
3333
*/
3434
protected $redis;
3535

@@ -76,8 +76,8 @@ public function testWriteSession()
7676
{
7777
$this->redis
7878
->expects($this->once())
79-
->method('set')
80-
->with(self::PREFIX.'id', 'data', self::TTL)
79+
->method('setEx')
80+
->with(self::PREFIX.'id', self::TTL, 'data')
8181
->will($this->returnValue(true))
8282
;
8383

@@ -88,7 +88,7 @@ public function testDestroySession()
8888
{
8989
$this->redis
9090
->expects($this->once())
91-
->method('del')
91+
->method('delete')
9292
->with(self::PREFIX.'id')
9393
->will($this->returnValue(1))
9494
;

0 commit comments

Comments
 (0)