-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject-cache.php
More file actions
642 lines (574 loc) · 21.5 KB
/
object-cache.php
File metadata and controls
642 lines (574 loc) · 21.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
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
<?php
/*
Plugin Name: WP Memcached
Description: Memcached backend for the WordPress Object Cache.
Version: 4.1.0
Author: Dave Long, Kris Linnell
Install this file to wp-content/object-cache.php
*/
// Initialize constants if not already set
if( !defined( 'WP_OBJECT_CACHE' ) ) {
define( 'WP_OBJECT_CACHE', true );
}
if( !defined( 'DISABLE_FILE_PATH' ) ) {
define( 'DISABLE_FILE_PATH', dirname( __FILE__ ) );
}
if( !defined( 'OBJECT_CACHE_EXPIRE' ) ) {
define( 'OBJECT_CACHE_EXPIRE', 3600 );
}
// Include server config file if present
if ( file_exists( ABSPATH . "wp-content/memcached-servers.php" ) ) {
include( ABSPATH . "wp-content/memcached-servers.php" );
}
/**
* Adds data to the cache. Proxied to Memcached::set since
* WordPress frequently uses Add when it should use Set()
* resulting in old data getting 'stuck' in Memcache.
*
* @uses $wp_object_cache Object Cache Class
* @see WP_Object_Cache::set()
*
* @param int|string $id The id used to identify this item in the cache
* @param mixed $data The data to add to the cache
* @param string $group The cache group to add this id to
* @param int $expire When the cache data should be expired
* @return bool Returns TRUE on success or FALSE on failure.
*/
function wp_cache_add( $id, $data, $group = 'default', $expire = 0 ) {
global $wp_object_cache;
return $wp_object_cache->set( $id, $data, $group, $expire );
}
/**
* Added for potential legacy compatibility; doesn't do anything.
*
* @return bool Always returns True
*/
function wp_cache_close() {
return true;
}
/**
* Decrement numeric cache item's value
*
* @uses $wp_object_cache Object Cache Class
* @see WP_Object_Cache::decr()
*
* @param int|string $id The cache id to increment
* @param int $offset The amount by which to decrement the item's value. Default is 1.
* @param string $group The cache group this id is stored in.
* @return bool|int Returns item's new value on success or FALSE on failure.
*/
function wp_cache_decr( $id, $n = 1, $group = 'default' ) {
global $wp_object_cache;
return $wp_object_cache->decr( $id, $n, $group );
}
/**
* Removes the cache contents matching id and group.
*
* @uses $wp_object_cache Object Cache Class
* @see WP_Object_Cache::delete()
*
* @param int|string $id The id used to identify this item in the cache
* @param string $group The cache group this id is stored in.
* @return bool True on successful removal, FALSE on failure.
*/
function wp_cache_delete( $id, $group = 'default' ) {
global $wp_object_cache;
return $wp_object_cache->delete( $id, $group );
}
/**
* Removes all cache items from session cache.
*
* @uses $wp_object_cache Object Cache Class
* @see WP_Object_Cache::flush()
*
* @return bool Always returns TRUE
*/
function wp_cache_flush() {
global $wp_object_cache;
return $wp_object_cache->flush();
}
/**
* Retrieves the cache contents from the cache by id and group.
*
* @uses $wp_object_cache Object Cache Class
* @see WP_Object_Cache::get()
*
* @param int|string $id The id used to identify this item in the cache
* @param string $group The cache group this id is stored in.
* @return bool|mixed Returns the value stored in the cache or FALSE on failure.
*/
function wp_cache_get( $id, $group = 'default' ) {
global $wp_object_cache;
return $wp_object_cache->get( $id, $group );
}
/**
* Increment numeric cache item's value
*
* @uses $wp_object_cache Object Cache Class
* @see WP_Object_Cache::incr()
*
* @param int|string $id The cache id to increment
* @param int $offset The amount by which to increment the item's value. Default is 1.
* @param string $group The cache group this id is stored in.
* @return bool|int Returns new item's value on success or FALSE on failure.
*/
function wp_cache_incr( $id, $n = 1, $group = 'default' ) {
global $wp_object_cache;
return $wp_object_cache->incr( $id, $n, $group );
}
/**
* Sets up Object Cache Global and assigns it.
*
* @global WP_Object_Cache $wp_object_cache WordPress Object Cache
*/
function wp_cache_init() {
global $wp_object_cache;
// Only create new if cache doesn't exist
if( !isset( $wp_object_cache ) ) {
$wp_object_cache = new WP_Object_Cache();
}
else {
//if it does exist, run its init sequence, which will check active and if not active attempt to become active
$wp_object_cache->init();
}
}
/**
* Replaces the contents of the cache with new data.
* Proxied to Memcached::set.
*
* @uses $wp_object_cache Object Cache Class
* @see WP_Object_Cache::set()
*
* @param int|string $id The id used to identify this item in the cache
* @param mixed $data The data to add to cache
* @param string $group The cache group this id is stored in.
* @param int $expire When the cache data should be expired
* @return bool Returns TRUE on success or FALSE on failure.
*/
function wp_cache_replace( $id, $data, $group = 'default', $expire = 0 ) {
global $wp_object_cache;
return $wp_object_cache->set( $id, $data, $group, $expire );
}
/**
* Saves the data to the cache.
*
* @uses $wp_object_cache Object Cache Class
* @see WP_Object_Cache::set()
*
* @param int|string $id The id used to identify this item in the cache
* @param mixed $data The data to add to cache
* @param string $group The cache group this id is stored in.
* @param int $expire When the cache data should be expired
* @return bool Returns TRUE on success or FALSE on failure.
*/
function wp_cache_set( $id, $data, $group = 'default', $expire = 0 ) {
global $wp_object_cache;
return $wp_object_cache->set( $id, $data, $group, $expire );
}
/**
* Returns array with log, counts, & group/key list
*
* @uses $wp_object_cache Object Cache Class
* @see WP_Object_Cache::set()
*
* @return mixed Returns array with stats for current page load
*/
function wp_cache_stats() {
global $wp_object_cache;
return $wp_object_cache->stats();
}
/**
* Adds a group or set of groups to the list of global groups.
*
* @param string|array $groups A group or an array of groups to add
*/
function wp_cache_add_global_groups( $groups ) {
global $wp_object_cache;
$wp_object_cache->add_global_groups( $groups );
}
/**
* Adds a group or set of groups to the list of non-persistent groups.
*
* @param string|array $groups A group or an array of groups to add
*/
function wp_cache_add_non_persistent_groups( $groups ) {
global $wp_object_cache;
$wp_object_cache->add_non_persistent_groups( $groups );
}
/**
* WordPress Object Cache
*
* The WordPress Object Cache is a two-level peristent/non-persistent
* cache consisting of a local session cache and an interface to a
* Memcached instance.
*/
class WP_Object_Cache {
// Class variables
var $global_groups = array ( 'users', 'userlogins', 'usermeta', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss' );
var $no_mc_groups = array( 'comment', 'counts' );
var $autoload_groups = array ( 'options' );
// Local variables
var $cache = array();
var $stats = array();
var $default_expiration = 3600;
// Initialize memcache pointer
var $memcache_active = false;
var $mc = null;
var $active = 0;
/**
* Constructor - Sets up object properties
*
* @return WP_Object_Cache
*/
function __construct() {
global $memcached_servers;
// Initialize local counters
$this->stats['log'] = array();
$this->stats['counts'] = array();
$this->stats['servers'] = array();
$this->stats['map'] = array();
$this->stats['counts']['session_hits'] = 0;
$this->stats['counts']['mc_hits'] = 0;
$this->stats['counts']['misses'] = 0;
$this->stats['counts']['excluded'] = 0;
$this->stats['counts']['failures'] = 0;
$this->stats['counts']['updates'] = 0;
array_push( $this->stats['log'], 'Object Cache constructor ' . rand( 1000,9999 ) );
// Only setup memcache if we're using it
if( file_exists( DISABLE_FILE_PATH . '/no-cache.txt' ) || !WP_OBJECT_CACHE ) {
array_push( $this->stats['log'], 'Using session cache only - object cache has been deactivated.' );
}
elseif( class_exists( 'Memcached' ) ) {
if ( isset( $memcached_servers ) ) {
$servers = $memcached_servers;
}
else {
$servers = array( '127.0.0.1:11211' );
}
$this->mc = new Memcached();
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->addServer( $node, $port );
}
// Confirm memcached is working before setting active
$status = $this->mc->set( 'memcache-test-value', 1, 1 );
if( $status ) {
$this->memcache_active = true;
$this->mc->delete( 'memcache-test-value' );
}
else {
array_push( $this->stats['log'], 'Using session cache only - error connecting to memcached.' );
}
}
// Memcached has been deactivated
else {
array_push( $this->stats['log'], 'Using session cache only - object cache is not available.' );
}
// Run internal init sequence
$this->init();
}
/* Begin Public Functions */
/**
* Decrement numeric cache item's value
*
* @param int|string $id The cache id to decrement
* @param int $offset The amount by which to decrement the item's value. Default is 1.
* @param string $group The cache group this id is stored in.
* @return bool|int Returns item's new value on success or FALSE on failure.
*/
public function decr( $id, $n, $group ) {
$key = $this->key( $id, $group );
array_push( $this->stats['log'], 'Decrementing ' . $group . ', ' . $id . ' in memcache.' );
$this->cache[ $key ] = $this->mc->decrement( $key, $n );
return $this->cache[ $key ];
}
/**
* Remove the contents of the cache id in the group
*
* @param int|string $id The id used to identify this item in the cache
* @param string $group The cache group this id is stored in.
* @return bool Returns TRUE on success or FALSE on failure.
*/
public function delete( $id, $group ) {
$key = $this->key( $id, $group );
// Delete $key from non-persistent cache
unset( $this->cache[ $key ] );
// Return TRUE if in no_mc, not using memcache, or blog_id not defined and key not in a global group
if ( in_array( $group, $this->no_mc_groups ) || ( !in_array( $group, $this->global_groups ) && $this->active != 1 ) || !$this->memcache_active ) {
array_push( $this->stats['log'], 'Unable to delete ' . $group . ', ' . $id . ' from cache - key is in no_mc or memcache not available' );
return true;
}
else {
array_push( $this->stats['log'], 'Deleting ' . $group . ', ' . $id . ' from cache.' );
return $this->mc->delete( $key );
}
}
/**
* Clears the non-persistent cache of all data
*
* @return bool Always returns TRUE
*/
public function flush() {
array_push( $this->stats['log'], 'Flushing session cache.' );
// Clear session cache
$this->cache = array();
return true;
}
/**
* Retrieves item from the cache, if it exists
*
* Non-persistent cache is searched first; Then memcache is checked.
* The first found value is returned; a memcache get will update
* the non-persistent cache.
*
* @param int|string $id The id used to identify this item in the cache
* @param string $group The cache group this id is stored in.
* @return bool|mixed Returns the value stored in the cache or FALSE on failure.
*/
public function get( $id, $group = 'default' ) {
$token = '';
$key = $this->key( $id, $group );
// Check non-persistent session cache first
if ( isset( $this->cache[ $key ] ) ) {
$value = $this->cache[ $key ];
@++$this->stats['counts']['session_hits'];
array_push( $this->stats['log'], 'Retrieved ' . $group . ', ' . $id . ' from session cache. Session hits:' . $this->stats['counts']['session_hits'] );
}
// Check for no_mc groups, inactive mecache or blog_id not defined and key not in a global group
elseif ( in_array( $group, $this->no_mc_groups ) || ( !in_array( $group, $this->global_groups ) && $this->active != 1 ) || !$this->memcache_active ) {
$value = false;
if ( !$this->memcache_active || $this->active != 1 ) {
@++$this->stats['counts']['misses'];
array_push( $this->stats['log'], 'Unable to retrieve ' . $group . ', ' . $id . '; memcache not active. Misses: ' . $this->stats['counts']['misses'] );
}
else {
@++$this->stats['counts']['excluded'];
array_push( $this->stats['log'], 'Unable to retrieve ' . $id . '; ' . $group . ' is a no_mc group. Excluded Items:' . $this->stats['counts']['excluded'] );
}
}
// If all else fails, check memcache
else {
$value = $this->mc->get( $key, NULL, $token );
// Token will not be set if item is not in memcache
if( empty( $token ) ) {
$value = false;
@++$this->stats['counts']['misses'];
array_push( $this->stats['log'], 'Unable to retrieve ' . $group . ', ' . $id . '; memcache miss. Misses:' . $this->stats['counts']['misses'] );
}
else {
// Save valid value in session
$this->cache[ $key ] = $value;
@++$this->stats['counts']['mc_hits'];
array_push( $this->stats['log'], 'Retrieved ' . $group . ', ' . $id . ' from memcache. Memcache hits:' . $this->stats['counts']['mc_hits'] );
if( !is_array( $this->stats['map'][ $group ] ) ) {
$this->stats['map'][ $group ] = array();
}
if( !in_array( $id, $this->stats['map'][ $group ] ) ) {
array_push( $this->stats['map'][ $group ], $id );
}
}
}
// Return cached item or false if not stored
return $value;
}
/**
* Increment numeric cache item's value
*
* @param int|string $id The cache id to increment
* @param int $offset The amount by which to increment the item's value. Default is 1.
* @param string $group The cache group this id is stored in.
* @return bool|int Returns item's new value on success or FALSE on failure.
*/
public function incr( $id, $n, $group ) {
$key = $this->key( $id, $group );
array_push( $this->stats['log'], 'Incrementing ' . $group . ', ' . $id . ' in memcache.' );
$this->cache[ $key ] = $this->mc->increment( $key, $n );
return $this->cache[ $key ];
}
/**
* Function to prevent non-global writes to memcache when blog_id not defined
* Also prevents multiple instances being built on a single page load
*/
public function init() {
global $blog_id, $wpdb;
if ( $this->active != 1 ) {
// global $blog_id cannot be trusted until db_prefix has been set
if( empty( $wpdb->prefix ) ) {
array_push( $this->stats['log'], 'Init function: Unable to determine blog_id - only global keys will work.' );
}
else {
$this->active = 1;
$this->default_expiration = rand( OBJECT_CACHE_EXPIRE, 1.5 * OBJECT_CACHE_EXPIRE );
array_push( $this->stats['log'], 'Init function: blog_id is ' . $blog_id . ' & default expiration is ' . $this->default_expiration . ' seconds. Setting active.' );
}
}
}
/**
* Sets the data contents into the cache
*
* The cache contents is grouped by the $group parameter followed by the
* $key. This allows for duplicate ids in unique groups. Therefore, naming of
* the group should be used with care and should follow normal function
* naming guidelines outside of core WordPress usage.
*
* @param int|string $id The id used to identify this item in the cache
* @param mixed $data The contents to store in the cache
* @param string $group The cache group this id is stored in.
* @param int $expire Seconds to retain $data in memcache; default 3600
* @return bool Returns TRUE on success or FALSE on failure.
*/
public function set( $id, $data, $group = 'default', $expire = 0 ) {
$token = '';
$key = $this->key( $id, $group );
if( $expire == 0 ) {
$expire = $this->default_expiration;
}
// Don't write value to memcache if it's the same as one we already have
if( isset( $this->cache[ $key ] ) && ( strcasecmp( serialize( $this->cache[ $key ] ), serialize( $data ) ) == 0 ) ) {
array_push( $this->stats['log'], 'Not adding ' . $group . ', ' . $id . ' to cache - value already in session cache.' );
return true;
}
// Return TRUE if in no_mc, not using memcache, or blog_id not defined and key not in a global group
if ( in_array( $group, $this->no_mc_groups ) || ( !in_array( $group, $this->global_groups ) && $this->active != 1 ) || !$this->memcache_active ) {
$this->cache[ $key ] = $data;
if( !is_array( $this->stats['map'][ $group ] ) ) {
$this->stats['map'][ $group ] = array();
}
if( !in_array( $id, $this->stats['map'][ $group ] ) ) {
array_push( $this->stats['map'][ $group ], $id );
}
array_push( $this->stats['log'], 'Adding ' . $group . ', ' . $id . ' to session cache only - memcache not available or no_mc group.' );
return true;
}
$cached = $this->mc->get( $key, NULL, $token );
if( $expire == 0 ) {
$expire = $this->default_expiration;
}
// Token will not be set if item is not in memcache; use add()
if( empty( $token ) ) {
if( $this->mc->add( $key, $data, $expire ) ) {
$this->cache[ $key ] = $data;
@++$this->stats['counts']['updates'];
array_push( $this->stats['log'], 'Adding ' . $group . ', ' . $id . ' to cache with expiration ' . $expire . '. Updates: ' . $this->stats['counts']['updates'] );
if( !is_array( $this->stats['map'][ $group ] ) ) {
$this->stats['map'][ $group ] = array();
}
if( !in_array( $id, $this->stats['map'][ $group ] ) ) {
array_push( $this->stats['map'][ $group ], $id );
}
return true;
}
// Memcache write failure
else {
$this->mc->delete( $key );
@++$this->stats['counts']['failures'];
array_push( $this->stats['log'], 'Not adding ' . $group . ', ' . $id . ' to cache - memcache write failure. Write failures:' . $this->stats['counts']['failures'] );
return false;
}
}
// If data is already in memcache, do not re-add it
elseif ( strcasecmp( serialize( $cached ), serialize( $data ) ) == 0 ) {
array_push( $this->stats['log'], 'Not adding ' . $group . ', ' . $id . ' to cache - value already in memcache.' );
return true;
}
// We have a valid token, and the values are different. Update it
else {
if( $this->mc->cas( $token, $key, $data, $expire ) ) {
$this->cache[ $key ] = $data;
if( !is_array( $this->stats['map'][ $group ] ) ) {
$this->stats['map'][ $group ] = array();
}
if( !in_array( $id, $this->stats['map'][ $group ] ) ) {
array_push( $this->stats['map'][ $group ], $id );
}
@++$this->stats['counts']['updates'];
array_push( $this->stats['log'], 'Updating ' . $group . ', ' . $id . ' in cache with expiration ' . $expire . '. Updates: ' . $this->stats['counts']['updates'] );
return true;
}
// Memcache write failure
else {
$this->mc->delete( $key );
@++$this->stats['counts']['failures'];
array_push( $this->stats['log'], 'Not adding ' . $group . ', ' . $id . ' to cache - memcache write failure. Write failures:' . $this->stats['counts']['failures'] );
return false;
}
}
}
/**
* Function to retrieve single page load stats & log
*
* @return mixed Returns Array containing stat / log info
*/
public function stats() {
if( $this->memcache_active ) {
$this->stats['server_stats'] = $this->mc->getStats();
$this->stats['server_list'] = $this->mc->getServerList();
}
return $this->stats;
}
/**
* Add groups to store globally for the site
*
* @param mixed $groups one or more groups to add to global groups
*/
public function add_global_groups( $groups ) {
array_push( $this->stats['log'], 'Adding ' . var_export( $groups, true ) . ' to global 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 );
}
/**
* Add groups to store in session cache only
*
* @param mixed $groups one or more groups to add to non-persistent groups
*/
public function add_non_persistent_groups( $groups ) {
array_push( $this->stats['log'], 'Adding ' . var_export( $groups, true ) . ' to non-persistent 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 );
}
/* End Public Functions */
/* Begin Private Functions */
/**
* Private function to generate key from object $id and $group
*
* @param int|string $id The id used to identify this item in the cache
* @param string $group The cache group this id is stored in.
* @return string MD5 encoded keystring
*/
private function key( $id, $group = 'default' ) {
global $blog_id;
// Determine key string prefix
if ( in_array( $group, $this->global_groups ) ) {
// Use 'Global' for any global keys
$prefix = 'global';
}
elseif( $this->active ) {
// If MC is fully active, global $blog_id can be trusted
$prefix = $blog_id;
}
else {
// If we're only using session cache, use a special prefix
$prefix = 'session';
}
$keystring = DB_NAME . "::$prefix::$group::$id";
$keystring = preg_replace( '/\s+/', '', $keystring );
$hashed_key = md5( $keystring );
array_push( $this->stats['log'], "Generated key $hashed_key from $keystring." );
return $hashed_key;
}
/* End Private Functions */
}
?>