-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpCacheSessionHandler.php
More file actions
134 lines (121 loc) · 3.3 KB
/
Copy pathOpCacheSessionHandler.php
File metadata and controls
134 lines (121 loc) · 3.3 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
<?php
namespace Detain\SessionSamurai;
/**
* Session handler using PHP files compiled by OPcache for fast reads.
* Data is serialized into PHP files under $cacheDir; OPcache keeps them
* in memory so subsequent reads bypass disk I/O.
*/
class OpCacheSessionHandler implements \SessionHandlerInterface, \SessionIdInterface, \SessionUpdateTimestampHandlerInterface
{
private string $cacheDir;
public function __construct(string $cacheDir = '')
{
$this->cacheDir = $cacheDir !== '' ? $cacheDir : sys_get_temp_dir() . '/opcache_sessions';
}
/**
* {@inheritdoc}
*/
public function open(string $savePath, string $sessionName): bool
{
if (!is_dir($this->cacheDir)) {
mkdir($this->cacheDir, 0700, true);
}
return true;
}
/**
* {@inheritdoc}
*/
public function close(): bool
{
return true;
}
/**
* {@inheritdoc}
*/
public function read(string $sessionId): string
{
$file = $this->getFilePath($sessionId);
if (!file_exists($file)) {
return '';
}
$data = @include($file);
return is_string($data) ? $data : '';
}
/**
* {@inheritdoc}
*/
public function write(string $sessionId, string $sessionData): bool
{
$file = $this->getFilePath($sessionId);
$content = '<?php return ' . var_export($sessionData, true) . ';';
if (file_put_contents($file, $content, LOCK_EX) === false) {
return false;
}
if (function_exists('opcache_invalidate')) {
opcache_invalidate($file, true);
}
return true;
}
/**
* {@inheritdoc}
*/
public function destroy(string $sessionId): bool
{
$file = $this->getFilePath($sessionId);
if (file_exists($file)) {
if (function_exists('opcache_invalidate')) {
opcache_invalidate($file, true);
}
unlink($file);
}
return true;
}
/**
* {@inheritdoc}
*/
public function gc(int $maxLifetime): int|false
{
$count = 0;
$expiry = time() - $maxLifetime;
foreach (glob($this->cacheDir . '/*.php') ?: [] as $file) {
if (filemtime($file) < $expiry) {
if (function_exists('opcache_invalidate')) {
opcache_invalidate($file, true);
}
unlink($file);
$count++;
}
}
return $count;
}
/**
* {@inheritdoc}
*/
// phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
public function create_sid(): string
{
return bin2hex(random_bytes(32));
}
/**
* {@inheritdoc}
*/
public function validateId(string $sessionId): bool
{
return file_exists($this->getFilePath($sessionId));
}
/**
* {@inheritdoc}
*/
public function updateTimestamp(string $sessionId, string $sessionData): bool
{
$file = $this->getFilePath($sessionId);
if (!file_exists($file)) {
return false;
}
return touch($file);
}
private function getFilePath(string $sessionId): string
{
return $this->cacheDir . '/' . preg_replace('/[^a-f0-9]/', '', $sessionId) . '.php';
}
}