-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathobject-cache.php
More file actions
361 lines (311 loc) · 10.5 KB
/
Copy pathobject-cache.php
File metadata and controls
361 lines (311 loc) · 10.5 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<?php
/**
* WP Performance object-cache.php drop-in.
*
* Persistent object cache backed by Redis (phpredis). Falls back to a
* per-request in-memory cache if Redis is unavailable, so the site keeps
* working either way. Auto-installed/removed by the plugin.
*/
if (! defined('ABSPATH')) {
return;
}
function wp_cache_init()
{
$GLOBALS['wp_object_cache'] = new WPP_Object_Cache();
}
function wp_cache_add($key, $data, $group = '', $expire = 0)
{
return $GLOBALS['wp_object_cache']->add($key, $data, $group, (int) $expire);
}
function wp_cache_add_multiple(array $data, $group = '', $expire = 0)
{
$result = [];
foreach ($data as $key => $value) {
$result[$key] = wp_cache_add($key, $value, $group, $expire);
}
return $result;
}
function wp_cache_replace($key, $data, $group = '', $expire = 0)
{
return $GLOBALS['wp_object_cache']->replace($key, $data, $group, (int) $expire);
}
function wp_cache_set($key, $data, $group = '', $expire = 0)
{
return $GLOBALS['wp_object_cache']->set($key, $data, $group, (int) $expire);
}
function wp_cache_set_multiple(array $data, $group = '', $expire = 0)
{
$result = [];
foreach ($data as $key => $value) {
$result[$key] = wp_cache_set($key, $value, $group, $expire);
}
return $result;
}
function wp_cache_get($key, $group = '', $force = false, &$found = null)
{
return $GLOBALS['wp_object_cache']->get($key, $group, $force, $found);
}
function wp_cache_get_multiple($keys, $group = '', $force = false)
{
return $GLOBALS['wp_object_cache']->get_multiple($keys, $group, $force);
}
function wp_cache_delete($key, $group = '')
{
return $GLOBALS['wp_object_cache']->delete($key, $group);
}
function wp_cache_delete_multiple(array $keys, $group = '')
{
$result = [];
foreach ($keys as $key) {
$result[$key] = wp_cache_delete($key, $group);
}
return $result;
}
function wp_cache_incr($key, $offset = 1, $group = '')
{
return $GLOBALS['wp_object_cache']->incr($key, (int) $offset, $group);
}
function wp_cache_decr($key, $offset = 1, $group = '')
{
return $GLOBALS['wp_object_cache']->decr($key, (int) $offset, $group);
}
function wp_cache_flush()
{
return $GLOBALS['wp_object_cache']->flush();
}
function wp_cache_flush_runtime()
{
return $GLOBALS['wp_object_cache']->flush_runtime();
}
function wp_cache_supports($feature)
{
return in_array($feature, ['get_multiple', 'set_multiple', 'add_multiple', 'delete_multiple', 'flush_runtime'], true);
}
function wp_cache_close()
{
return true;
}
function wp_cache_add_global_groups($groups)
{
$GLOBALS['wp_object_cache']->add_global_groups($groups);
}
function wp_cache_add_non_persistent_groups($groups)
{
$GLOBALS['wp_object_cache']->add_non_persistent_groups($groups);
}
function wp_cache_switch_to_blog($blog_id)
{
$GLOBALS['wp_object_cache']->switch_to_blog((int) $blog_id);
}
function wp_cache_reset()
{
}
class WPP_Object_Cache
{
/** @var array<string,mixed> */
private array $cache = [];
private ?\Redis $redis = null;
private bool $connected = false;
/** @var array<string,bool> */
private array $globalGroups = [];
/** @var array<string,bool> */
private array $nonPersistent = ['counts' => true, 'plugins' => true];
private int $blogPrefix = 1;
private string $salt;
public int $cache_hits = 0;
public int $cache_misses = 0;
public function __construct()
{
$this->salt = defined('WP_CACHE_KEY_SALT') ? (string) WP_CACHE_KEY_SALT : '';
// Without a site-specific default, two installs sharing one Redis would
// collide on identical keys and read each other's options and users.
if ($this->salt === '') {
$this->salt = substr(md5(
(defined('DB_NAME') ? (string) DB_NAME : '')
. '|' . (string) ($GLOBALS['table_prefix'] ?? '')
. '|' . (defined('ABSPATH') ? (string) ABSPATH : '')
), 0, 12);
}
if (function_exists('get_current_blog_id')) {
$this->blogPrefix = (int) get_current_blog_id();
}
if (class_exists('Redis')) {
try {
$this->redis = new \Redis();
$host = defined('WP_REDIS_HOST') ? WP_REDIS_HOST : '127.0.0.1';
$port = defined('WP_REDIS_PORT') ? (int) WP_REDIS_PORT : 6379;
$this->connected = (bool) @$this->redis->connect($host, $port, 1.0);
if ($this->connected && defined('WP_REDIS_PASSWORD') && WP_REDIS_PASSWORD) {
@$this->redis->auth(WP_REDIS_PASSWORD);
}
if ($this->connected && defined('WP_REDIS_DATABASE')) {
@$this->redis->select((int) WP_REDIS_DATABASE);
}
} catch (\Throwable $e) {
$this->connected = false;
}
}
}
public function add_global_groups($groups): void
{
foreach ((array) $groups as $group) {
$this->globalGroups[$group] = true;
}
}
public function add_non_persistent_groups($groups): void
{
foreach ((array) $groups as $group) {
$this->nonPersistent[$group] = true;
}
}
public function switch_to_blog($blogId): void
{
$this->blogPrefix = (int) $blogId;
}
public function get($key, $group = 'default', $force = false, &$found = null)
{
$group = $group ?: 'default';
$id = $this->id($key, $group);
if (! $force && array_key_exists($id, $this->cache)) {
$found = true;
$this->cache_hits++;
return is_object($this->cache[$id]) ? clone $this->cache[$id] : $this->cache[$id];
}
if ($this->persistent($group)) {
$value = $this->call(static fn ($redis) => $redis->get($id), false);
if ($value !== false) {
$value = maybe_unserialize($value);
$this->cache[$id] = $value;
$found = true;
$this->cache_hits++;
return is_object($value) ? clone $value : $value;
}
}
$found = false;
$this->cache_misses++;
return false;
}
public function get_multiple($keys, $group = 'default', $force = false): array
{
$result = [];
foreach ((array) $keys as $key) {
$found = null;
$result[$key] = $this->get($key, $group, $force, $found);
}
return $result;
}
public function set($key, $data, $group = 'default', $expire = 0): bool
{
$group = $group ?: 'default';
$id = $this->id($key, $group);
$this->cache[$id] = is_object($data) ? clone $data : $data;
if ($this->persistent($group)) {
$serialized = maybe_serialize($data);
$this->call(static fn ($redis) => $expire > 0
? $redis->setex($id, (int) $expire, $serialized)
: $redis->set($id, $serialized));
}
return true;
}
public function add($key, $data, $group = 'default', $expire = 0): bool
{
$group = $group ?: 'default';
$id = $this->id($key, $group);
if (array_key_exists($id, $this->cache)) {
return false;
}
if ($this->persistent($group) && $this->call(static fn ($redis) => $redis->exists($id), false)) {
return false;
}
return $this->set($key, $data, $group, $expire);
}
public function replace($key, $data, $group = 'default', $expire = 0): bool
{
$found = null;
$this->get($key, $group, false, $found);
return $found ? $this->set($key, $data, $group, $expire) : false;
}
public function delete($key, $group = 'default'): bool
{
$group = $group ?: 'default';
$id = $this->id($key, $group);
unset($this->cache[$id]);
if ($this->persistent($group)) {
$this->call(static fn ($redis) => $redis->del($id));
}
return true;
}
public function incr($key, $offset = 1, $group = 'default')
{
$found = null;
$value = $this->get($key, $group, false, $found);
if (! $found) {
return false;
}
$value = max(0, (int) $value + (int) $offset);
$this->set($key, $value, $group);
return $value;
}
public function decr($key, $offset = 1, $group = 'default')
{
return $this->incr($key, -(int) $offset, $group);
}
/**
* Deletes only this install's keys. flushDb() would empty the whole Redis
* database, which is shared by default with anything else on the server.
*/
public function flush(): bool
{
$this->cache = [];
if (! $this->connected) {
return true;
}
$pattern = "wpp:{$this->salt}:*";
return $this->call(static function ($redis) use ($pattern) {
$redis->setOption(\Redis::OPT_SCAN, \Redis::SCAN_RETRY);
$cursor = null;
while (($keys = $redis->scan($cursor, $pattern, 500)) !== false) {
if ($keys === []) {
continue;
}
method_exists($redis, 'unlink') ? $redis->unlink($keys) : $redis->del($keys);
}
return true;
}) === true;
}
public function flush_runtime(): bool
{
$this->cache = [];
return true;
}
private function persistent(string $group): bool
{
return $this->connected && ! isset($this->nonPersistent[$group]);
}
/**
* Run a Redis command, degrading to the in-memory cache on any failure.
*
* phpredis throws RedisException not only when the connection drops but for
* server replies such as NOAUTH, LOADING, MISCONF and OOM. Uncaught, that is
* a fatal error on every front-end and admin request, which cannot then be
* switched off from the admin. Dropping the connection keeps the request
* alive on the runtime array cache instead.
*/
private function call(callable $command, mixed $fallback = false): mixed
{
if (! $this->connected) {
return $fallback;
}
try {
return $command($this->redis);
} catch (\Throwable $e) {
$this->connected = false;
return $fallback;
}
}
private function id($key, string $group): string
{
$prefix = isset($this->globalGroups[$group]) ? 'global' : (string) $this->blogPrefix;
return "wpp:{$this->salt}:{$prefix}:{$group}:{$key}";
}
}