forked from wp-plugins/memcached-is-your-friend
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmemcached-class-object-cache.php
500 lines (394 loc) · 13 KB
/
memcached-class-object-cache.php
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
<?php
/*
Code adapted from Memcached Redux Plugin
Author Scott Taylor
*/
if ( class_exists( 'Memcached' ) ) {
function wp_cache_add( $key, $data, $group = '', $expire = 0 ) {
global $wp_object_cache;
return $wp_object_cache->add( $key, $data, $group, $expire );
}
function wp_cache_incr( $key, $n = 1, $group = '' ) {
global $wp_object_cache;
return $wp_object_cache->incr( $key, $n, $group );
}
function wp_cache_decr( $key, $n = 1, $group = '' ) {
global $wp_object_cache;
return $wp_object_cache->decr( $key, $n, $group );
}
function wp_cache_close() {
global $wp_object_cache;
return $wp_object_cache->close();
}
function wp_cache_delete( $key, $group = '' ) {
global $wp_object_cache;
return $wp_object_cache->delete( $key, $group );
}
function wp_cache_flush() {
global $wp_object_cache;
return $wp_object_cache->flush();
}
function wp_cache_get( $key, $group = '', $force = false, &$found = null ) {
global $wp_object_cache;
return $wp_object_cache->get( $key, $group, $force, $found );
}
/**
* $keys_and_groups = array(
* array( 'key', 'group' ),
* array( 'key', '' ),
* array( 'key', 'group' ),
* array( 'key' )
* );
*
*/
function wp_cache_get_multi( $key_and_groups, $bucket = 'default' ) {
global $wp_object_cache;
return $wp_object_cache->get_multi( $key_and_groups, $bucket );
}
/**
* $items = array(
* array( 'key', 'data', 'group' ),
* array( 'key', 'data' )
* );
*
*/
function wp_cache_set_multi( $items, $expire = 0, $group = 'default' ) {
global $wp_object_cache;
return $wp_object_cache->set_multi( $items, $expire = 0, $group = 'default' );
}
function wp_cache_init() {
global $wp_object_cache;
$wp_object_cache = new WP_Object_Cache();
}
function wp_cache_replace( $key, $data, $group = '', $expire = 0 ) {
global $wp_object_cache;
return $wp_object_cache->replace( $key, $data, $group, $expire );
}
function wp_cache_set( $key, $data, $group = '', $expire = 0 ) {
global $wp_object_cache;
if ( defined( 'WP_INSTALLING' ) == false ) {
return $wp_object_cache->set( $key, $data, $group, $expire );
} else {
return $wp_object_cache->delete( $key, $group );
}
}
function wp_cache_add_global_groups( $groups ) {
global $wp_object_cache;
$wp_object_cache->add_global_groups( $groups );
}
function wp_cache_add_non_persistent_groups( $groups ) {
global $wp_object_cache;
$wp_object_cache->add_non_persistent_groups( $groups );
}
function wordpress_memcached_get_stats() {
global $wp_object_cache;
return $wp_object_cache->stats();
}
class WP_Object_Cache {
public $global_groups = array(); // (was private)
public $no_mc_groups = array(); // (was private)
public $cache = array(); // (was private)
public $mc = array(); // (was private)
public $stats = array( 'add' => 0, 'delete' => 0, 'get' => 0, 'get_multi' => 0, ); // (was private)
public $group_ops = array(); // (was private)
public $memcache_debug = array(); // added for ElasticPress compatibility
public $cache_enabled = true; // modified to allow wordpress to properly disable object cache in wp-activate.php +22 (was private)
private $default_expiration = 0;
function add( $id, $data, $group = 'default', $expire = 0 ) {
$key = $this->key( $id, $group );
if ( is_object( $data ) ) {
$data = clone $data;
}
if ( in_array( $group, $this->no_mc_groups ) ) {
$this->cache[ $key ] = $data;
return true;
} elseif ( isset( $this->cache[ $key ] ) && $this->cache[ $key ] !== false ) {
return false;
}
$mc =& $this->get_mc( $group );
$expire = ( $expire == 0 ) ? $this->default_expiration : $expire;
$result = $mc->add( $key, $data, $expire );
if ( false !== $result ) {
if ( isset( $this->stats['add'] ) ) {
++ $this->stats['add'];
}
$this->group_ops[ $group ][] = "add $id";
$this->cache[ $key ] = $data;
}
return $result;
}
function add_global_groups( $groups ) {
if ( ! is_array( $groups ) ) {
$groups = (array) $groups;
}
$this->global_groups = array_merge( $this->global_groups, $groups );
$this->global_groups = array_unique( $this->global_groups );
}
function add_non_persistent_groups( $groups ) {
if ( ! is_array( $groups ) ) {
$groups = (array) $groups;
}
$this->no_mc_groups = array_merge( $this->no_mc_groups, $groups );
$this->no_mc_groups = array_unique( $this->no_mc_groups );
}
function incr( $id, $n = 1, $group = 'default' ) {
$key = $this->key( $id, $group );
$mc =& $this->get_mc( $group );
$this->cache[ $key ] = $mc->increment( $key, $n );
return $this->cache[ $key ];
}
function decr( $id, $n = 1, $group = 'default' ) {
$key = $this->key( $id, $group );
$mc =& $this->get_mc( $group );
$this->cache[ $key ] = $mc->decrement( $key, $n );
return $this->cache[ $key ];
}
function close() {
foreach ( $this->mc as $bucket => $mc ) {
$mc->quit();
}
}
function delete( $id, $group = 'default' ) {
$key = $this->key( $id, $group );
if ( in_array( $group, $this->no_mc_groups ) ) {
unset( $this->cache[ $key ] );
return true;
}
$mc =& $this->get_mc( $group );
$result = $mc->delete( $key );
if ( isset( $this->stats['delete'] ) ) {
++ $this->stats['delete'];
}
$this->group_ops[ $group ][] = "delete $id";
if ( false !== $result ) {
unset( $this->cache[ $key ] );
}
return $result;
}
function flush() {
// Don't flush if multi-blog.
if ( function_exists( 'is_site_admin' ) || defined( 'CUSTOM_USER_TABLE' ) && defined( 'CUSTOM_USER_META_TABLE' ) ) {
return true;
}
$ret = true;
foreach ( array_keys( $this->mc ) as $group ) {
$ret &= $this->mc[ $group ]->flush();
}
return $ret;
}
function get( $id, $group = 'default', $force = false, &$found = null ) {
$key = $this->key( $id, $group );
$mc =& $this->get_mc( $group );
if ( null !== $found ) {
$found = true;
}
if ( isset( $this->cache[ $key ] ) && ( ! $force || in_array( $group, $this->no_mc_groups ) ) ) {
if ( is_object( $this->cache[ $key ] ) ) {
$value = clone $this->cache[ $key ];
} else {
$value = $this->cache[ $key ];
}
} else if ( in_array( $group, $this->no_mc_groups ) ) {
$this->cache[ $key ] = $value = false;
} else {
$value = $mc->get( $key );
if ($mc->getResultCode() == Memcached::RES_NOTFOUND) {
$value = false;
if ( null !== $found ) {
$found = false;
}
}
$this->cache[ $key ] = $value;
}
if ( isset( $this->stats['get'] ) ) {
++ $this->stats['get'];
}
$this->group_ops[ $group ][] = "get $id";
if ( 'checkthedatabaseplease' === $value ) {
unset( $this->cache[ $key ] );
$value = false;
}
return $value;
}
function get_multi( $keys, $group = 'default' ) {
$return = array();
$gets = array();
foreach ( $keys as $i => $values ) {
$mc =& $this->get_mc( $group );
$values = (array) $values;
if ( empty( $values[1] ) ) {
$values[1] = 'default';
}
list( $id, $group ) = (array) $values;
$key = $this->key( $id, $group );
if ( isset( $this->cache[ $key ] ) ) {
if ( is_object( $this->cache[ $key ] ) ) {
$return[ $key ] = clone $this->cache[ $key ];
} else {
$return[ $key ] = $this->cache[ $key ];
}
} else if ( in_array( $group, $this->no_mc_groups ) ) {
$return[ $key ] = false;
} else {
$gets[ $key ] = $key;
}
}
if ( ! empty( $gets ) ) {
$null = null;
$results = $mc->getMulti( $gets, $null, Memcached::GET_PRESERVE_ORDER );
$joined = array_combine( array_keys( $gets ), array_values( $results ) );
$return = array_merge( $return, $joined );
}
@ ++ $this->stats['get_multi'];
$this->group_ops[ $group ][] = "get_multi $id";
$this->cache = array_merge( $this->cache, $return );
return array_values( $return );
}
function key( $key, $group ) {
if ( empty( $group ) ) {
$group = 'default';
}
if ( false !== array_search( $group, $this->global_groups ) ) {
$prefix = $this->global_prefix;
} else {
$prefix = $this->blog_prefix;
}
return preg_replace( '/\s+/', '', WP_CACHE_KEY_SALT . "$prefix$group:$key" );
}
function replace( $id, $data, $group = 'default', $expire = 0 ) {
$key = $this->key( $id, $group );
$expire = ( $expire == 0 ) ? $this->default_expiration : $expire;
$mc =& $this->get_mc( $group );
if ( is_object( $data ) ) {
$data = clone $data;
}
$result = $mc->replace( $key, $data, $expire );
if ( false !== $result ) {
$this->cache[ $key ] = $data;
}
return $result;
}
function set( $id, $data, $group = 'default', $expire = 0 ) {
$key = $this->key( $id, $group );
if ( isset( $this->cache[ $key ] ) && ( 'checkthedatabaseplease' === $this->cache[ $key ] ) ) {
return false;
}
if ( is_object( $data ) ) {
$data = clone $data;
}
$this->cache[ $key ] = $data;
if ( in_array( $group, $this->no_mc_groups ) ) {
return true;
}
$expire = ( $expire == 0 ) ? $this->default_expiration : $expire;
$mc =& $this->get_mc( $group );
$result = $mc->set( $key, $data, $expire );
return $result;
}
function set_multi( $items, $expire = 0, $group = 'default' ) {
$sets = array();
$mc =& $this->get_mc( $group );
$expire = ( $expire == 0 ) ? $this->default_expiration : $expire;
foreach ( $items as $i => $item ) {
if ( empty( $item[2] ) ) {
$item[2] = 'default';
}
list( $id, $data, $group ) = $item;
$key = $this->key( $id, $group );
if ( isset( $this->cache[ $key ] ) && ( 'checkthedatabaseplease' === $this->cache[ $key ] ) ) {
continue;
}
if ( is_object( $data ) ) {
$data = clone $data;
}
$this->cache[ $key ] = $data;
if ( in_array( $group, $this->no_mc_groups ) ) {
continue;
}
$sets[ $key ] = $data;
}
if ( ! empty( $sets ) ) {
$mc->setMulti( $sets, $expire );
}
}
function colorize_debug_line( $line ) {
$colors = array(
'get' => 'green',
'set' => 'purple',
'add' => 'blue',
'delete' => 'red'
);
$cmd = substr( $line, 0, strpos( $line, ' ' ) );
$cmd2 = "<span style='color:{$colors[$cmd]}'>$cmd</span>";
return $cmd2 . substr( $line, strlen( $cmd ) ) . "\n";
}
function stats() {
$stats_text = '';
foreach ( $this->mc as $bucket => $mc ) {
$stats = $mc->getStats();
foreach ( $stats as $key => $details ) {
$stats_text .= 'memcached: ' . $key . "\n\r";
foreach ( $details as $name => $value ) {
$stats_text .= $name . ': ' . $value . "\n\r";
}
$stats_text .= "\n\r";
}
}
return $stats_text;
}
function &get_mc( $group ) {
if ( isset( $this->mc[ $group ] ) ) {
return $this->mc[ $group ];
}
return $this->mc['default'];
}
function failure_callback( $host, $port ) {
}
function __construct() {
global $memcached_servers;
if ( isset( $memcached_servers ) ) {
$buckets = $memcached_servers;
} else {
$buckets = array( '127.0.0.1' );
}
reset( $buckets );
if ( is_int( key( $buckets ) ) ) {
$buckets = array( 'default' => $buckets );
}
foreach ( $buckets as $bucket => $servers ) {
$this->mc[ $bucket ] = new Memcached();
$instances = array();
foreach ( $servers as $server ) {
@list( $node, $port ) = explode( ':', $server );
if ( empty( $port ) ) {
$port = ini_get( 'memcache.default_port' );
}
$port = intval( $port );
if ( ! $port ) {
$port = 11211;
}
$instances[] = array( $node, $port, 1 );
}
$this->mc[ $bucket ]->addServers( $instances );
}
global $blog_id, $table_prefix;
$this->global_prefix = '';
$this->blog_prefix = '';
if ( function_exists( 'is_multisite' ) ) {
$this->global_prefix = ( is_multisite() || defined( 'CUSTOM_USER_TABLE' ) && defined( 'CUSTOM_USER_META_TABLE' ) ) ? '' : $table_prefix;
$this->blog_prefix = ( is_multisite() ? $blog_id : $table_prefix ) . ':';
}
$this->cache_hits =& $this->stats['get'];
$this->cache_misses =& $this->stats['add'];
}
}
} else {
// No Memcached
if ( function_exists( 'wp_using_ext_object_cache' ) ) {
// In 3.7+, we can handle this smoothly
wp_using_ext_object_cache( false );
} else {
// In earlier versions, there isn't a clean bail-out method.
wp_die( 'Memcached class not available.' );
}
}