1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Xervice\Redis\Session; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use DataProvider\RedisSessionDataProvider; |
8
|
|
|
use Symfony\Component\HttpFoundation\Session\Storage\Handler\AbstractSessionHandler; |
9
|
|
|
use Xervice\Redis\Exception\RedisException; |
10
|
|
|
use Xervice\Redis\RedisClient; |
11
|
|
|
|
12
|
|
|
class RedisSessionHandler extends AbstractSessionHandler |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var \Xervice\Redis\RedisClient |
16
|
|
|
*/ |
17
|
|
|
private $client; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $prefix; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var int |
26
|
|
|
*/ |
27
|
|
|
private $ttl; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var RedisSessionDataProvider |
31
|
|
|
*/ |
32
|
|
|
private $sessionDataProvider; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* RedisSessionHandler constructor. |
36
|
|
|
* |
37
|
|
|
* @param \Xervice\Redis\RedisClient $client |
38
|
|
|
* @param string $prefix |
39
|
|
|
*/ |
40
|
1 |
|
public function __construct(RedisClient $client, array $options = []) |
41
|
|
|
{ |
42
|
1 |
|
$this->client = $client; |
43
|
1 |
|
$this->prefix = isset($options['prefix']) ? $options['prefix'] : 'session:xervice.'; |
44
|
1 |
|
$this->ttl = isset($options['ttl']) ? $options['ttl'] : 86400; |
45
|
1 |
|
$this->sessionDataProvider = new RedisSessionDataProvider(); |
46
|
1 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $sessionId |
50
|
|
|
* |
51
|
|
|
* @return string |
52
|
|
|
* @throws \Xervice\Config\Exception\ConfigNotFound |
53
|
|
|
*/ |
54
|
1 |
|
protected function doRead($sessionId) |
55
|
|
|
{ |
56
|
|
|
try { |
57
|
1 |
|
$this->sessionDataProvider = $this->client->get($this->prefix . $sessionId); |
58
|
1 |
|
} catch (RedisException $exception) { |
59
|
1 |
|
$this->sessionDataProvider->setData(''); |
60
|
|
|
} |
61
|
|
|
|
62
|
1 |
|
return $this->sessionDataProvider->getData(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $sessionId |
67
|
|
|
* @param string $data |
68
|
|
|
* |
69
|
|
|
* @return bool |
70
|
|
|
* @throws \Xervice\Config\Exception\ConfigNotFound |
71
|
|
|
*/ |
72
|
|
|
protected function doWrite($sessionId, $data) |
73
|
|
|
{ |
74
|
|
|
$this->sessionDataProvider->setData($data); |
75
|
|
|
$this->client->setEx($this->prefix . $sessionId, $this->sessionDataProvider, 'ex', $this->ttl); |
76
|
|
|
|
77
|
|
|
return true; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $sessionId |
82
|
|
|
* |
83
|
|
|
* @return bool |
84
|
|
|
* @throws \Xervice\Config\Exception\ConfigNotFound |
85
|
|
|
*/ |
86
|
|
|
protected function doDestroy($sessionId) |
87
|
|
|
{ |
88
|
|
|
$this->client->delete($this->prefix . $sessionId); |
89
|
|
|
|
90
|
|
|
return true; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
|
|
public function close() |
97
|
|
|
{ |
98
|
|
|
return true; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param int $maxlifetime |
103
|
|
|
* |
104
|
|
|
* @return bool |
105
|
|
|
*/ |
106
|
|
|
public function gc($maxlifetime) |
107
|
|
|
{ |
108
|
|
|
return true; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param string $session_id |
113
|
|
|
* @param string $session_data |
114
|
|
|
* |
115
|
|
|
* @return bool |
116
|
|
|
* @throws \Xervice\Config\Exception\ConfigNotFound |
117
|
|
|
*/ |
118
|
|
|
public function updateTimestamp($session_id, $session_data) |
119
|
|
|
{ |
120
|
|
|
$this->client->expire($this->prefix . $session_id, $this->ttl); |
121
|
|
|
|
122
|
|
|
return true; |
123
|
|
|
} |
124
|
|
|
} |