Repository navigation
Expand file tree
/
Copy pathRedisHandler.php
More file actions
74 lines (63 loc) · 1.53 KB
/
Copy pathRedisHandler.php
File metadata and controls
74 lines (63 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
namespace Bolt\Session\Handler;
use Predis\ClientInterface as Predis;
use Redis;
/**
* Redis Session Handler.
*
* @author Carson Full <carsonfull@gmail.com>
*/
class RedisHandler extends AbstractHandler implements LazyWriteHandlerInterface
{
/** @var Redis|Predis */
protected $redis;
/** @var int */
protected $ttl;
/**
* RedisHandler constructor.
*
* @param Redis|Predis $redis The Redis or Predis client
* @param int $maxlifetime Number of seconds before session expires
*/
public function __construct($redis, $maxlifetime)
{
if (!$redis instanceof Redis && !$redis instanceof Predis) {
throw new \InvalidArgumentException('Argument must be an instance of Redis or Predis\ClientInterface');
}
$this->redis = $redis;
$this->ttl = (int) $maxlifetime;
}
/**
* {@inheritdoc}
*/
public function read($sessionId)
{
if ($data = $this->redis->get($sessionId)) {
return $data;
}
return '';
}
/**
* {@inheritdoc}
*/
public function destroy($sessionId)
{
$this->redis->del($sessionId);
return true;
}
/**
* {@inheritdoc}
*/
public function write($sessionId, $data)
{
$this->redis->setex($sessionId, $this->ttl, $data);
return true;
}
/**
* {@inheritdoc}
*/
public function updateTimestamp($sessionId, $data)
{
$this->redis->expire($sessionId, $this->ttl);
}
}