-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemcachedSessionHandler.php
More file actions
101 lines (88 loc) · 2.24 KB
/
Copy pathMemcachedSessionHandler.php
File metadata and controls
101 lines (88 loc) · 2.24 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
<?php
namespace Detain\SessionSamurai;
class MemcachedSessionHandler implements \SessionHandlerInterface, \SessionIdInterface, \SessionUpdateTimestampHandlerInterface
{
protected \Memcached $memcached;
protected string $sessionName;
protected int $expire = 0;
protected string $prefix = '';
public function __construct(\Memcached $memcached, string $prefix = 'sess-')
{
$this->memcached = $memcached;
$this->prefix = $prefix;
}
/**
* {@inheritdoc}
*/
public function open(string $path, string $name): bool
{
$this->sessionName = $name;
$this->expire = (int) ini_get('session.gc_maxlifetime');
return true;
}
/**
* {@inheritdoc}
*/
public function close(): bool
{
return true;
}
/**
* {@inheritdoc}
*/
public function read(string $id): string
{
$data = $this->memcached->get($this->prefix . $id);
if ($data === false) {
return '';
}
return is_string($data) ? $data : '';
}
/**
* {@inheritdoc}
*/
public function write(string $id, string $data): bool
{
return (bool) $this->memcached->set($this->prefix . $id, $data, $this->expire);
}
/**
* {@inheritdoc}
*/
public function destroy(string $id): bool
{
return (bool) $this->memcached->delete($this->prefix . $id);
}
/**
* {@inheritdoc}
*/
public function gc(int $max_lifetime): int|false
{
$this->expire = $max_lifetime;
return 0;
}
/**
* {@inheritdoc}
*/
// phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
public function create_sid(): string
{
do {
$sessionId = bin2hex(random_bytes(32));
} while ($this->memcached->get($this->prefix . $sessionId) !== false);
return $sessionId;
}
/**
* {@inheritdoc}
*/
public function updateTimestamp(string $id, string $data): bool
{
return (bool) $this->memcached->touch($this->prefix . $id, $this->expire);
}
/**
* {@inheritdoc}
*/
public function validateId(string $id): bool
{
return $this->memcached->get($this->prefix . $id) !== false;
}
}