-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedisSessionHandler.php
More file actions
124 lines (109 loc) · 3.03 KB
/
Copy pathRedisSessionHandler.php
File metadata and controls
124 lines (109 loc) · 3.03 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
<?php
namespace Detain\SessionSamurai;
use Redis;
use SessionHandlerInterface;
use SessionIdInterface;
use SessionUpdateTimestampHandlerInterface;
use InvalidArgumentException;
use RuntimeException;
/**
* Class RedisSessionHandler
*
* A session handler that stores PHP session data in Redis.
*/
class RedisSessionHandler implements SessionHandlerInterface, SessionIdInterface, SessionUpdateTimestampHandlerInterface
{
/** @var Redis */
private Redis $redis;
/** @var int Session ttl in seconds */
private int $ttl;
/** @var string Prefix for all session keys in Redis */
private string $keyPrefix;
/**
* @param Redis $redis An existing Redis connection.
* @param int $ttl Session TTL in seconds.
* @param string $keyPrefix Key prefix to isolate sessions.
*/
public function __construct(Redis &$redis, int $ttl = 86400, string $keyPrefix = 'PHPREDIS_SESSION:') {
$this->redis = &$redis;
$this->ttl = $ttl;
$this->keyPrefix = $keyPrefix;
}
/**
* {@inheritdoc}
*/
public function open(string $savePath, string $sessionName): bool
{
// Nothing to do since connection is done in constructor.
return true;
}
/**
* {@inheritdoc}
*/
public function close(): bool
{
return $this->redis->close();
}
/**
* {@inheritdoc}
*/
public function read(string $sessionId): string
{
$data = $this->redis->get($this->keyPrefix . $sessionId);
return is_string($data) ? $data : '';
}
/**
* {@inheritdoc}
*/
public function write(string $sessionId, string $data): bool
{
$key = $this->keyPrefix . $sessionId;
// Use SETEX to write data and expiry at once
return (bool) $this->redis->setex($key, $this->ttl, $data);
}
/**
* {@inheritdoc}
*/
public function destroy(string $sessionId): bool
{
$this->redis->del($this->keyPrefix . $sessionId);
return true;
}
/**
* {@inheritdoc}
*/
public function gc(int $maxttl): int|false
{
// Redis handles expiry automatically via TTL; no action needed.
return 0;
}
/**
* {@inheritdoc}
*/
// phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
public function create_sid(): string
{
// Generate a 32‐byte random ID, hex‐encoded, for 64 chars.
return bin2hex(random_bytes(32));
}
/**
* {@inheritdoc}
*/
public function validateId(string $sessionId): bool
{
// Check existence without resetting TTL
return $this->redis->exists($this->keyPrefix . $sessionId) === 1;
}
/**
* {@inheritdoc}
*/
public function updateTimestamp(string $sessionId, string $data): bool
{
$key = $this->keyPrefix . $sessionId;
if (!$this->redis->exists($key)) {
return false;
}
// Only update TTL, do not rewrite the payload
return (bool) $this->redis->expire($key, $this->ttl);
}
}