-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathXoopsCacheAdapter.php
More file actions
242 lines (184 loc) · 5.91 KB
/
XoopsCacheAdapter.php
File metadata and controls
242 lines (184 loc) · 5.91 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
declare(strict_types=1);
/**
* You may not change or alter any portion of this comment or credits
* of supporting developers from this source code or any supporting source code
* which is considered copyrighted (c) material of the original comment or credit authors.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
/**
* @copyright 2000-2026 XOOPS Project (https://xoops.org/)
* @license GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
* @author XOOPS Development Team
*/
namespace Xoops\Helpers\Provider;
use Xoops\Helpers\Contracts\CacheInterface;
/**
* Multi-tier cache adapter with automatic fallback.
*
* Tries backends in order of preference:
* 1. XoopsCache (native XOOPS cache, if available)
* 2. APCu (if extension loaded and enabled)
* 3. File-based cache (always available)
*
* The file cache stores serialized data with expiration timestamps
* in XOOPS_VAR_PATH/caches/xmf/ (or system temp directory).
*/
class XoopsCacheAdapter implements CacheInterface
{
public function __construct(
private readonly string $prefix = 'xmf_',
) {}
public function get(string $key): mixed
{
$prefixed = $this->prefix . $key;
if (class_exists('XoopsCache', false)) {
$payload = \XoopsCache::read($prefixed);
if (
$payload === false
|| !is_array($payload)
|| !array_key_exists('value', $payload)
|| ($payload['__xmf_hit'] ?? false) !== true
) {
return null;
}
return $payload['value'];
}
if ($this->apcuAvailable()) {
$value = apcu_fetch($prefixed, $success);
return $success ? $value : null;
}
return $this->fileGet($prefixed);
}
public function set(string $key, mixed $value, int $ttl = 3600): bool
{
$prefixed = $this->prefix . $key;
if (class_exists('XoopsCache', false)) {
return \XoopsCache::write($prefixed, ['__xmf_hit' => true, 'value' => $value], $ttl);
}
if ($this->apcuAvailable()) {
return apcu_store($prefixed, $value, $ttl);
}
return $this->fileSet($prefixed, $value, $ttl);
}
public function forget(string $key): bool
{
$prefixed = $this->prefix . $key;
if (class_exists('XoopsCache', false)) {
return \XoopsCache::delete($prefixed);
}
if ($this->apcuAvailable()) {
return apcu_delete($prefixed);
}
return $this->fileForget($prefixed);
}
public function flush(): bool
{
if (class_exists('XoopsCache', false)) {
return \XoopsCache::clear();
}
if ($this->apcuAvailable()) {
return apcu_clear_cache();
}
return $this->fileFlush();
}
public function has(string $key): bool
{
return $this->get($key) !== null;
}
public function many(array $keys, mixed $default = null): array
{
$result = [];
foreach ($keys as $key) {
$value = $this->get($key);
$result[$key] = $value ?? $default;
}
return $result;
}
public function putMany(array $values, int $ttl = 3600): bool
{
$success = true;
foreach ($values as $key => $value) {
if (!$this->set($key, $value, $ttl)) {
$success = false;
}
}
return $success;
}
// ── File cache backend ─────────────────────────────────
private function fileGet(string $key): mixed
{
$file = $this->cacheFilePath($key);
if (!file_exists($file)) {
return null;
}
$content = @file_get_contents($file);
if ($content === false) {
return null;
}
// File backend only supports scalar/array payloads — disallow object instantiation
$data = @unserialize($content, ['allowed_classes' => false]);
if (!is_array($data) || !array_key_exists('value', $data)) {
return null;
}
if ($data['expires'] !== 0 && $data['expires'] < time()) {
@unlink($file);
return null;
}
return $data['value'];
}
private function fileSet(string $key, mixed $value, int $ttl): bool
{
$file = $this->cacheFilePath($key);
$dir = dirname($file);
if (!is_dir($dir)) {
@mkdir($dir, 0755, true);
}
$data = [
'value' => $value,
'expires' => $ttl > 0 ? time() + $ttl : 0,
];
return @file_put_contents($file, serialize($data), LOCK_EX) !== false;
}
private function fileForget(string $key): bool
{
$file = $this->cacheFilePath($key);
if (file_exists($file)) {
return @unlink($file);
}
return true;
}
private function fileFlush(): bool
{
$dir = $this->cacheDir();
if (!is_dir($dir)) {
return true;
}
$files = glob($dir . '/*.cache');
if ($files === false) {
return true;
}
foreach ($files as $file) {
@unlink($file);
}
return true;
}
private function cacheDir(): string
{
if (defined('XOOPS_VAR_PATH')) {
return XOOPS_VAR_PATH . '/caches/xmf';
}
return sys_get_temp_dir() . '/xmf_cache';
}
private function cacheFilePath(string $key): string
{
return $this->cacheDir() . '/' . md5($key) . '.cache';
}
private function apcuAvailable(): bool
{
return extension_loaded('apcu') && function_exists('apcu_enabled') && apcu_enabled();
}
}