Repository navigation
Expand file tree
/
Copy pathRedis.php
More file actions
164 lines (140 loc) · 3.15 KB
/
Copy pathRedis.php
File metadata and controls
164 lines (140 loc) · 3.15 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
declare(strict_types=1);
namespace Rancoud\Session;
use Predis\Client as Predis;
use SessionHandlerInterface;
use SessionUpdateTimestampHandlerInterface;
/**
* Class Redis.
*/
class Redis implements SessionHandlerInterface, SessionUpdateTimestampHandlerInterface
{
/** @var Predis */
protected Predis $redis;
/** @var int */
protected int $lifetime = 1440;
/**
* @param string|array $configuration
*/
public function setNewRedis($configuration): void
{
$this->redis = new Predis($configuration);
}
/**
* @param Predis $redis
*/
public function setCurrentRedis($redis): void
{
$this->redis = $redis;
}
/**
* @param int $lifetime
*/
public function setLifetime(int $lifetime): void
{
$this->lifetime = $lifetime;
}
/**
* @param string $savePath
* @param string $sessionName
*
* @return bool
*/
public function open($savePath, $sessionName): bool
{
return true;
}
/**
* @return bool
*/
public function close(): bool
{
return true;
}
/**
* @param string $sessionId
*
* @return string
*/
public function read($sessionId): string
{
return (string) $this->redis->get($sessionId);
}
/**
* @param string $sessionId
* @param string $data
*
* @return bool
*/
public function write($sessionId, $data): bool
{
$this->redis->set($sessionId, $data);
$this->redis->expireat($sessionId, \time() + $this->lifetime);
return true;
}
/**
* @param string $sessionId
*
* @return bool
*/
public function destroy($sessionId): bool
{
$this->redis->del([$sessionId]);
return true;
}
/**
* @param int $lifetime
*
* @return bool
*/
public function gc($lifetime): bool
{
return true;
}
/**
* Checks format and id exists, if not session_id will be regenerate.
*
* @param string $key
*
* @return bool
*/
public function validateId($key): bool
{
if (\preg_match('/^[a-zA-Z0-9-]{127}+$/', $key) !== 1) {
return false;
}
$exist = $this->redis->exists($key);
return $exist === 1;
}
/**
* Updates the timestamp of a session when its data didn't change.
*
* @param string $sessionId
* @param string $sessionData
*
* @return bool
*/
public function updateTimestamp($sessionId, $sessionData): bool
{
return $this->write($sessionId, $sessionData);
}
/**
* @throws \Exception
*
* @return string
*/
public function create_sid(): string
{
$string = '';
$caracters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-';
$countCaracters = 62;
for ($i = 0; $i < 127; ++$i) {
$string .= $caracters[\random_int(0, $countCaracters)];
}
$exist = $this->redis->exists($string);
if ($exist !== 0) {
return $this->create_sid();
}
return $string;
}
}