-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathobject-cache.php
644 lines (500 loc) · 15.6 KB
/
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
<?php
/*
Plugin Name: Memcached
Description: Memcached backend for the WP Object Cache.
Plugin URI: http://wordpress.org/extend/plugins/memcached/
Author: Ryan Boren, Denis de Bernardy, Matt Martz
Install this file to wp-content/object-cache.php
*/
/*
* Users with setups where multiple installs share a common wp-config.php or $table_prefix
* can use this to guarantee uniqueness for the keys generated by this object cache
*/
defined( 'WP_CACHE_KEY_SALT' ) or define( 'WP_CACHE_KEY_SALT', DB_NAME );
/**
* Should the connection to memcache be persistent (default true)
*/
defined( 'WP_MEMCACHE_PERSISTENT' ) or define( 'WP_MEMCACHE_PERSISTENT', true );
/**
* Value in seconds which will be used for connecting to the daemon. (default 1)
*/
defined( 'WP_MEMCACHE_TIMEOUT' ) or define( 'WP_MEMCACHE_TIMEOUT', 1 );
/**
* Number of buckets to create for this server which in turn control its probability
* of it being selected. The probability is relative to the total weight of all servers.
* (default 1)
*/
defined( 'WP_MEMCACHE_WEIGHT' ) or define( 'WP_MEMCACHE_WEIGHT', 1 );
/**
* Controls how often a failed server will be retried, the default value is 15 seconds
*/
defined( 'WP_MEMCACHE_RETRY' ) or define( 'WP_MEMCACHE_RETRY', 15 );
/**
* Controls if memcache writes to log
*/
defined( 'WP_MEMCACHE_DISABLE_LOGGING' ) or define( 'WP_MEMCACHE_DISABLE_LOGGING', false );
/**
* Disable / Enable memcache flushing
*/
defined( 'WP_MEMCACHE_DISABLE_FLUSHING' ) or define( 'WP_MEMCACHE_DISABLE_FLUSHING', true );
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) {
global $wp_object_cache;
return $wp_object_cache->get($key, $group, $force);
}
function wp_cache_get_multi($groups, $force = false) {
global $wp_object_cache;
return $wp_object_cache->get_multi($groups, $force);
}
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_switch_to_blog( $blog_id ) {
global $wp_object_cache;
return $wp_object_cache->switch_to_blog( $blog_id );
}
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);
}
class WP_Object_Cache {
var $global_groups = array();
var $no_mc_groups = array();
var $cache = array();
var $mc = array();
var $stats = array();
var $group_ops = array();
var $cache_enabled = true;
var $default_expiration = 0;
public function get_alloptions() {
// Check our internal cache, to avoid the more expensive get-multi
$key = $this->key( 'alloptions', 'options' );
if ( isset( $this->cache[ $key ] ) ) {
return $this->cache[ $key ];
}
$keys = $this->get( 'alloptionskeys', 'options' );
if ( empty( $keys ) || ! is_array( $keys ) ) {
return array();
}
$data = $this->get_multi( array( 'options' => array_keys( $keys ) ) );
if ( empty( $data ) || empty( $data['options'] ) ) {
return array();
}
$this->cache[ $key ] = $data['options'];
return $this->cache[ $key ];
}
public function set_alloptions( $data ) {
$internal_cache_key = $this->key( 'alloptions', 'options' );
$existing = $internal_cache = $this->get_alloptions();
$keys = $this->get( 'alloptionskeys', 'options' );
if ( empty( $keys ) || ! is_array( $keys ) ) {
$keys = array();
}
// While you could use array_diff here, it ends up being a bit more
// complicated than just checking
foreach ( $data as $key => $value ) {
if ( isset( $existing[ $key ] ) && $existing[ $key ] === $value ) {
continue;
}
if ( ! isset( $keys[ $key ] ) ) {
$keys[ $key ] = true;
}
if ( ! $this->set( $key, $value, 'options' ) ) {
return false;
}
$internal_cache[ $key ] = $value;
}
// Remove deleted elements
foreach ( $existing as $key => $value ) {
if ( isset( $data[ $key ] ) ) {
continue;
}
if ( isset( $keys[ $key ] ) ) {
unset( $keys[ $key ] );
}
if ( ! $this->delete( $key, 'options' ) ) {
return false;
}
unset( $internal_cache[ $key ] );
}
if ( ! $this->set( 'alloptionskeys', $keys, 'options' ) ) {
return false;
}
$this->cache[ $internal_cache_key ] = $internal_cache;
return true;
}
public function delete_alloptions() {
$key = $this->key( 'alloptions', 'options' );
$this->cache[ $key ] = array();
return $this->delete( 'alloptionskeys', 'options' );
}
function add($id, $data, $group = 'default', $expire = 0) {
if ( $id === 'alloptions' && $group === 'options' ) {
return $this->set_alloptions( $data );
}
$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;
$time = microtime(true);
$result = $mc->add($key, $data, false, $expire);
$time_taken = microtime(true) - $time;
if ( false !== $result ) {
@ ++$this->stats['add'];
$this->stats['add_time'] += $time_taken;
$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->close();
}
}
function delete($id, $group = 'default') {
if ( $id === 'alloptions' && $group === 'options' ) {
return $this->delete_alloptions();
}
$key = $this->key($id, $group);
if ( in_array($group, $this->no_mc_groups) ) {
unset($this->cache[$key]);
return true;
}
$mc =& $this->get_mc($group);
$time = microtime(true);
$result = $mc->delete($key);
$time_taken = microtime(true) - $time;
@ ++$this->stats['delete'];
$this->stats['delete_time'] += $time_taken;
$this->group_ops[$group][] = "delete $id";
if ( false !== $result ) {
unset( $this->cache[ $key ] );
}
return $result;
}
function flush() {
// Return true is flushing is disabled
if ( ! WP_MEMCACHE_DISABLE_FLUSHING ) {
return true;
}
// Did someone try and wipe our stats? >:(
// This occurs during unit tests, where WP reaches in and resets the
// stats array.
if ( empty( $this->stats ) ) {
$this->reset_stats();
}
$ret = true;
foreach ( array_keys( $this->mc ) as $group ) {
$ret &= $this->mc[ $group ]->flush();
}
return $ret;
}
/**
* Flush the local (in-memory) object cache
*
* Forces all future requests to fetch from memcache. Can be used to
* alleviate memory pressure in long-running requests.
*/
public function flush_local() {
$this->cache = array();
$this->group_ops = array();
}
protected function reset_stats() {
$this->stats = array( 'get' => 0, 'get_time' => 0, 'add' => 0, 'add_time' => 0, 'delete' => 0, 'delete_time' => 0, 'set' => 0, 'set_time' => 0 );
}
function get($id, $group = 'default', $force = false) {
if ( $id === 'alloptions' && $group === 'options' ) {
return $this->get_alloptions();
}
$key = $this->key($id, $group);
$mc =& $this->get_mc($group);
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 {
$time = microtime(true);
$value = $mc->get($key);
if ( NULL === $value )
$value = false;
$time_taken = microtime(true) - $time;
$this->cache[$key] = $value;
@ ++$this->stats['get'];
$this->stats['get_time'] += $time_taken;
$this->group_ops[$group][] = "get $id";
}
if ( 'checkthedatabaseplease' === $value ) {
unset( $this->cache[$key] );
$value = false;
}
return $value;
}
function get_multi( $groups ) {
/*
format: $get['group-name'] = array( 'key1', 'key2' );
*/
$return = array();
$to_get = array();
foreach ( $groups as $group => $ids ) {
$mc =& $this->get_mc( $group );
$return[ $group ] = array();
foreach ( $ids as $id ) {
$key = $this->key( $id, $group );
if ( isset( $this->cache[ $key ] ) ) {
if ( is_object( $this->cache[ $key ] ) ) {
$return[ $group ][ $id ] = clone $this->cache[ $key ];
} else {
$return[ $group ][ $id ] = $this->cache[ $key ];
}
continue;
} else if ( in_array( $group, $this->no_mc_groups ) ) {
$return[ $group ][ $id ] = false;
continue;
} else {
$to_get[ $key ] = array( $group, $id );
}
}
}
if ( $to_get ) {
$vals = $mc->get( array_keys( $to_get ) );
foreach ( $to_get as $key => $bits ) {
if ( ! isset( $vals[ $key ] ) ) {
continue;
}
list( $group, $id ) = $bits;
$return[ $group ][ $id ] = $vals[ $key ];
$this->cache[ $key ] = $vals[ $key ];
}
}
@ ++$this->stats['get_multi'];
$this->group_ops[$group][] = "get_multi $id";
return $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, false, $expire );
if ( false !== $result ) {
$this->cache[ $key ] = $data;
}
return $result;
}
function set($id, $data, $group = 'default', $expire = 0) {
if ( $id === 'alloptions' && $group === 'options' ) {
return $this->set_alloptions( $data );
}
$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);
$time = microtime(true);
/**
* If the expiry exceeds 30 days, we have to sent the expire
* as a unix timestamp. This is because PHP Memcache extension
* uses this hueristic.
*
* To make this consistant, we always use absolute unix timestamps.
*/
if ( $expire ) {
$expire += time();
}
$result = $mc->set($key, $data, false, $expire);
$time_taken = microtime(true) - $time;
@ ++$this->stats['set'];
$this->stats['set_time'] += $time_taken;
$this->group_ops[$group][] = "set $id";
return $result;
}
function switch_to_blog( $blog_id ) {
global $wpdb;
$table_prefix = $wpdb->prefix;
$blog_id = (int) $blog_id;
$this->blog_prefix = ( is_multisite() ? $blog_id : $table_prefix ) . ':';
}
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() {
echo "<p>\n";
foreach ( $this->stats as $stat => $n ) {
if ( ! $n ) {
continue;
}
echo "<strong>$stat</strong> $n";
echo "<br/>\n";
}
echo "</p>\n";
echo "<h3>Memcached:</h3>";
foreach ( $this->group_ops as $group => $ops ) {
if ( !isset($_GET['debug_queries']) && 500 < count($ops) ) {
$ops = array_slice( $ops, 0, 500 );
echo "<big>Too many to show! <a href='" . add_query_arg( 'debug_queries', 'true' ) . "'>Show them anyway</a>.</big>\n";
}
echo "<h4>$group commands</h4>";
echo "<pre>\n";
$lines = array();
foreach ( $ops as $op ) {
$lines[] = $this->colorize_debug_line($op);
}
print_r($lines);
echo "</pre>\n";
}
}
function &get_mc($group) {
if ( isset( $this->mc[ $group ] ) ) {
return $this->mc[ $group ];
}
return $this->mc['default'];
}
function failure_callback( $host, $port ) {
if ( !WP_MEMCACHE_DISABLE_LOGGING ) {
error_log( "Memcache Connection failure for $host:$port\n" );
}
}
function __construct() {
global $memcached_servers, $blog_id, $table_prefix;
if ( isset( $memcached_servers ) ) {
$buckets = $memcached_servers;
} else {
$buckets = array( '127.0.0.1:11211' );
}
reset( $buckets );
if ( is_int( key( $buckets ) ) ) {
$buckets = array( 'default' => $buckets );
}
foreach ( $buckets as $bucket => $servers) {
$this->mc[$bucket] = new Memcache();
foreach ( $servers as $server ) {
list ( $node, $port ) = explode(':', $server);
if ( ! $port ) {
$port = ini_get( 'memcache.default_port' );
}
$port = intval( $port );
if ( ! $port ) {
$port = 11211;
}
$this->mc[$bucket]->addServer($node, $port, WP_MEMCACHE_PERSISTENT, WP_MEMCACHE_WEIGHT, WP_MEMCACHE_TIMEOUT, WP_MEMCACHE_RETRY, true, array($this, 'failure_callback'));
$this->mc[$bucket]->setCompressThreshold(20000, 0.2);
}
}
$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->reset_stats();
$this->cache_hits =& $this->stats['get'];
$this->cache_misses =& $this->stats['add'];
}
}