object-cache.php 36.1 KB
Newer Older
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
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
<?php
/**
 * Adds a value to cache.
 *
 * If the specified key already exists, the value is not stored and the function
 * returns false.
 *
 * @param string $key        The key under which to store the value.
 * @param mixed  $value      The value to store.
 * @param string $group      The group value appended to the $key.
 * @param int    $expiration The expiration time, defaults to 0.
 *
 * @return bool                 Returns TRUE on success or FALSE on failure.
 */
function wp_cache_add( $key, $value, $group = '', $expiration = 0 ) {
	global $wp_object_cache;

	return $wp_object_cache->add( $key, $value, $group, $expiration );
}

/**
 * Adds a value to cache on a specific server.
 *
 * Using a server_key value, the object can be stored on a specified server as opposed
 * to a random server in the stack. Note that this method will add the key/value to the
 * _cache object as part of the runtime cache. It will add it to an array for the
 * specified server_key.
 *
 * @param string $server_key     The key identifying the server to store the value on.
 * @param string $key            The key under which to store the value.
 * @param mixed  $value          The value to store.
 * @param string $group          The group value appended to the $key.
 * @param int    $expiration     The expiration time, defaults to 0.
 *
 * @return bool                     Returns TRUE on success or FALSE on failure.
 */
function wp_cache_add_by_key( $server_key, $key, $value, $group = '', $expiration = 0 ) {
	global $wp_object_cache;

	return $wp_object_cache->addByKey( $server_key, $key, $value, $group, $expiration );
}

/**
 * Append data to an existing item.
 *
 * This method should throw an error if it is used with compressed data. This
 * is an expected behavior. Memcached casts the value to be appended to the initial value to the
 * type of the initial value. Be careful as this leads to unexpected behavior at times. Due to
 * how memcached treats types, the behavior has been mimicked in the internal cache to produce
 * similar results and improve consistency. It is recommend that appends only occur with data of
 * the same type.
 *
 * @link http://www.php.net/manual/en/memcached.append.php
 *
 * @param string $key    The key under which to store the value.
 * @param mixed  $value  Must be string as appending mixed values is not well-defined
 * @param string $group  The group value appended to the $key.
 *
 * @return bool             Returns TRUE on success or FALSE on failure.
 */
function wp_cache_append( $key, $value, $group = '' ) {
	global $wp_object_cache;

	return $wp_object_cache->append( $key, $value, $group );
}

/**
 * Append data to an existing item by server key.
 *
 * This method should throw an error if it is used with compressed data. This
 * is an expected behavior. Memcached casts the value to be appended to the initial value to the
 * type of the initial value. Be careful as this leads to unexpected behavior at times. Due to
 * how memcached treats types, the behavior has been mimicked in the internal cache to produce
 * similar results and improve consistency. It is recommend that appends only occur with data of
 * the same type.
 *
 * @link http://www.php.net/manual/en/memcached.appendbykey.php
 *
 * @param string $server_key     The key identifying the server to store the value on.
 * @param string $key            The key under which to store the value.
 * @param mixed  $value          Must be string as appending mixed values is not well-defined
 * @param string $group          The group value appended to the $key.
 *
 * @return bool                     Returns TRUE on success or FALSE on failure.
 */
function wp_cache_append_by_key( $server_key, $key, $value, $group = '' ) {
	global $wp_object_cache;

	return $wp_object_cache->appendByKey( $server_key, $key, $value, $group );
}

/**
 * Closes the cache.
 *
 * This function has ceased to do anything since WordPress 2.5. The
 * functionality was removed along with the rest of the persistent cache. This
 * does not mean that plugins can't implement this function when they need to
 * make sure that the cache is cleaned up after WordPress no longer needs it.
 *
 * @since 2.0.0
 *
 * @return  bool    Always returns True
 */
function wp_cache_close() {
	return true;
}

/**
 * Decrement a numeric item's value.
 *
 * @link http://www.php.net/manual/en/memcached.decrement.php
 *
 * @param string $key    The key under which to store the value.
 * @param int    $offset The amount by which to decrement the item's value.
 * @param string $group  The group value appended to the $key.
 *
 * @return int|bool         Returns item's new value on success or FALSE on failure.
 */
function wp_cache_decrement( $key, $offset = 1, $group = '' ) {
	global $wp_object_cache;

	return $wp_object_cache->decrement( $key, $offset, $group );
}

/**
 * Decrement a numeric item's value.
 *
 * Same as wp_cache_decrement. Original WordPress caching backends use wp_cache_decr. I
 * want both spellings to work.
 *
 * @link http://www.php.net/manual/en/memcached.decrement.php
 *
 * @param string $key    The key under which to store the value.
 * @param int    $offset The amount by which to decrement the item's value.
 * @param string $group  The group value appended to the $key.
 *
 * @return int|bool         Returns item's new value on success or FALSE on failure.
 */
function wp_cache_decr( $key, $offset = 1, $group = '' ) {
	return wp_cache_decrement( $key, $offset, $group );
}

/**
 * Remove the item from the cache.
 *
 * Remove an item from memcached with identified by $key after $time seconds. The
 * $time parameter allows an object to be queued for deletion without immediately
 * deleting. Between the time that it is queued and the time it's deleted, add,
 * replace, and get will fail, but set will succeed.
 *
 * @link http://www.php.net/manual/en/memcached.delete.php
 *
 * @param string $key    The key under which to store the value.
 * @param string $group  The group value appended to the $key.
 * @param int    $time   The amount of time the server will wait to delete the item in seconds.
 *
 * @return bool             Returns TRUE on success or FALSE on failure.
 */
function wp_cache_delete( $key, $group = '', $time = 0 ) {
	global $wp_object_cache;

	return $wp_object_cache->delete( $key, $group, $time );
}

/**
 * Remove the item from the cache by server key.
 *
 * Remove an item from memcached with identified by $key after $time seconds. The
 * $time parameter allows an object to be queued for deletion without immediately
 * deleting. Between the time that it is queued and the time it's deleted, add,
 * replace, and get will fail, but set will succeed.
 *
 * @link http://www.php.net/manual/en/memcached.deletebykey.php
 *
 * @param string $server_key The key identifying the server to store the value on.
 * @param string $key        The key under which to store the value.
 * @param string $group      The group value appended to the $key.
 * @param int    $time       The amount of time the server will wait to delete the item in seconds.
 *
 * @return bool                     Returns TRUE on success or FALSE on failure.
 */
function wp_cache_delete_by_key( $server_key, $key, $group = '', $time = 0 ) {
	global $wp_object_cache;

	return $wp_object_cache->deleteByKey( $server_key, $key, $group, $time );
}

/**
 * Fetch the next result.
 *
 * @link http://www.php.net/manual/en/memcached.fetch.php
 *
 * @return  array|bool   Returns the next result or FALSE otherwise.
 */
function wp_cache_fetch() {
	global $wp_object_cache;

	return $wp_object_cache->fetch();
}

/**
 * Fetch all remaining results from the last request.
 *
 * @return  array|bool  Returns the results or FALSE on failure.
 */
function wp_cache_fetch_all() {
	global $wp_object_cache;

	return $wp_object_cache->fetchAll();
}

/**
 * Invalidate all items in the cache.
 *
 * @param int $delay  Number of seconds to wait before invalidating the items.
 *
 * @return bool             Returns TRUE on success or FALSE on failure.
 */
function wp_cache_flush( $delay = 0 ) {
	global $wp_object_cache;

	return $wp_object_cache->flush( $delay );
}

/**
 * Retrieve object from cache.
 *
 * Gets an object from cache based on $key and $group. In order to fully support the $cache_cb and $cas_token
 * parameters, the runtime cache is ignored by this function if either of those values are set. If either of
 * those values are set, the request is made directly to the memcached server for proper handling of the
 * callback and/or token.
 *
 * Note that the $deprecated and $found args are only here for compatibility with the native wp_cache_get function.
 *
 * @link http://www.php.net/manual/en/memcached.get.php
 *
 * @param string      $key        The key under which to store the value.
 * @param string      $group      The group value appended to the $key.
 * @param null|string $cache_cb   Read-through caching callback.
 * @param null|float  $cas_token  The variable to store the CAS token in.
 *
 * @return bool|mixed               Cached object value.
 */
function wp_cache_get( $key, $group = '', $cache_cb = null, &$cas_token = null ) {
	global $wp_object_cache;

	/**
	 * Handles situations where the $force argument for the wp_cache_get function in core may be used. It is only
	 * used once in all of WP Core and since the function does not do anything with it, it is pointless to support.
	 * I'm catching the issue here to avoid conflicts.
	 */
	if ( true === $cache_cb )
		$cache_cb = null;

	if ( func_num_args() > 2 )
		return $wp_object_cache->get( $key, $group, '', false, $cache_cb, $cas_token );
	else
		return $wp_object_cache->get( $key, $group );
}

/**
 * Retrieve object from cache from specified server.
 *
 * Gets an object from cache based on $key, $group and $server_key. In order to fully support the $cache_cb and $cas_token
 * parameters, the runtime cache is ignored by this function if either of those values are set. If either of
 * those values are set, the request is made directly to the memcached server for proper handling of the
 * callback and/or token.
 *
 * @link http://www.php.net/manual/en/memcached.getbykey.php
 *
 * @param string      $server_key The key identifying the server to store the value on.
 * @param string      $key        The key under which to store the value.
 * @param string      $group      The group value appended to the $key.
 * @param null|string $cache_cb   Read-through caching callback.
 * @param null|float  $cas_token  The variable to store the CAS token in.
 *
 * @return bool|mixed               Cached object value.
 */
function wp_cache_get_by_key( $server_key, $key, $group = '', $cache_cb = null, &$cas_token = null ) {
	global $wp_object_cache;

	if ( func_num_args() > 3 )
		return $wp_object_cache->getByKey( $server_key, $key, $group, $cache_cb, $cas_token );
	else
		return $wp_object_cache->getByKey( $server_key, $key, $group );
}

/**
 * Request multiple keys without blocking.
 *
 * @link http://www.php.net/manual/en/memcached.getdelayed.php
 *
 * @param string|array $keys       Array or string of key(s) to request.
 * @param string|array $groups     Array or string of group(s) for the key(s). See buildKeys for more on how these are handled.
 * @param bool         $with_cas   Whether to request CAS token values also.
 * @param null         $value_cb   The result callback or NULL.
 *
 * @return bool                     Returns TRUE on success or FALSE on failure.
 */
function wp_cache_get_delayed( $keys, $groups = '', $with_cas = false, $value_cb = null ) {
	global $wp_object_cache;

	return $wp_object_cache->getDelayed( $keys, $groups, $with_cas, $value_cb );
}

/**
 * Request multiple keys without blocking from a specified server.
 *
 * @link http://www.php.net/manual/en/memcached.getdelayed.php
 *
 * @param string       $server_key The key identifying the server to store the value on.
 * @param string|array $keys       Array or string of key(s) to request.
 * @param string|array $groups     Array or string of group(s) for the key(s). See buildKeys for more on how these are handled.
 * @param bool         $with_cas   Whether to request CAS token values also.
 * @param null         $value_cb   The result callback or NULL.
 *
 * @return bool                     Returns TRUE on success or FALSE on failure.
 */
function wp_cache_get_delayed_by_key( $server_key, $keys, $groups = '', $with_cas = false, $value_cb = null ) {
	global $wp_object_cache;

	return $wp_object_cache->getDelayedByKey( $server_key, $keys, $groups, $with_cas, $value_cb );
}

/**
 * Gets multiple values from memcached in one request.
 *
 * See the buildKeys method definition to understand the $keys/$groups parameters.
 *
 * @link http://www.php.net/manual/en/memcached.getmulti.php
 *
 * @param array        $keys       Array of keys to retrieve.
 * @param string|array $groups     If string, used for all keys. If arrays, corresponds with the $keys array.
 * @param null|array   $cas_tokens The variable to store the CAS tokens for the found items.
 * @param int          $flags      The flags for the get operation.
 *
 * @return bool|array               Returns the array of found items or FALSE on failure.
 */
function wp_cache_get_multi( $keys, $groups = '', &$cas_tokens = null, $flags = null ) {
	global $wp_object_cache;

	if ( func_num_args() > 2 )
		return $wp_object_cache->getMulti( $keys, $groups, '', $cas_tokens, $flags );
	else
		return $wp_object_cache->getMulti( $keys, $groups );
}

/**
 * Gets multiple values from memcached in one request by specified server key.
 *
 * See the buildKeys method definition to understand the $keys/$groups parameters.
 *
 * @link http://www.php.net/manual/en/memcached.getmultibykey.php
 *
 * @param string       $server_key The key identifying the server to store the value on.
 * @param array        $keys       Array of keys to retrieve.
 * @param string|array $groups     If string, used for all keys. If arrays, corresponds with the $keys array.
 * @param null|array   $cas_tokens The variable to store the CAS tokens for the found items.
 * @param int          $flags      The flags for the get operation.
 *
 * @return bool|array               Returns the array of found items or FALSE on failure.
 */
function wp_cache_get_multi_by_key( $server_key, $keys, $groups = '', &$cas_tokens = null, $flags = null ) {
	global $wp_object_cache;

	if ( func_num_args() > 3 )
		return $wp_object_cache->getMultiByKey( $server_key, $keys, $groups, $cas_tokens, $flags );
	else
		return $wp_object_cache->getMultiByKey( $server_key, $keys, $groups );
}

/**
 * Retrieve a Memcached option value.
 *
 * @link http://www.php.net/manual/en/memcached.getoption.php
 *
 * @param int $option One of the Memcached::OPT_* constants.
 *
 * @return mixed        Returns the value of the requested option, or FALSE on error.
 */
function wp_cache_get_option( $option ) {
	global $wp_object_cache;

	return $wp_object_cache->getOption( $option );
}

/**
 * Return the result code of the last option.
 *
 * @link http://www.php.net/manual/en/memcached.getresultcode.php
 *
 * @return int  Result code of the last Memcached operation.
 */
function wp_cache_get_result_code() {
	global $wp_object_cache;

	return $wp_object_cache->getResultCode();
}

/**
 * Return the message describing the result of the last operation.
 *
 * @link http://www.php.net/manual/en/memcached.getresultmessage.php
 *
 * @return string   Message describing the result of the last Memcached operation.
 */
function wp_cache_get_result_message() {
	global $wp_object_cache;

	return $wp_object_cache->getResultMessage();
}

/**
 * Get server information by key.
 *
 * @link http://www.php.net/manual/en/memcached.getserverbykey.php
 *
 * @param string $server_key The key identifying the server to store the value on.
 *
 * @return array                Array with host, post, and weight on success, FALSE on failure.
 */
function wp_cache_get_server_by_key( $server_key ) {
	global $wp_object_cache;

	return $wp_object_cache->getServerByKey( $server_key );
}

/**
 * Get the list of servers in the pool.
 *
 * @link http://www.php.net/manual/en/memcached.getserverlist.php
 *
 * @return array    The list of all servers in the server pool.
 */
function wp_cache_get_server_list() {
	global $wp_object_cache;

	return $wp_object_cache->getServerList();
}

/**
 * Get server pool statistics.
 *
 * @link http://www.php.net/manual/en/memcached.getstats.php
 *
 * @return array    Array of server statistics, one entry per server.
 */
function wp_cache_get_stats() {
	global $wp_object_cache;

	return $wp_object_cache->getStats();
}

/**
 * Get server pool memcached version information.
 *
 * @link http://www.php.net/manual/en/memcached.getversion.php
 *
 * @return array    Array of server versions, one entry per server.
 */
function wp_cache_get_version() {
	global $wp_object_cache;

	return $wp_object_cache->getVersion();
}

/**
 * Increment a numeric item's value.
 *
 * @link http://www.php.net/manual/en/memcached.increment.php
 *
 * @param string $key    The key under which to store the value.
 * @param int    $offset The amount by which to increment the item's value.
 * @param string $group  The group value appended to the $key.
 *
 * @return int|bool         Returns item's new value on success or FALSE on failure.
 */
function wp_cache_increment( $key, $offset = 1, $group = '' ) {
	global $wp_object_cache;

	return $wp_object_cache->increment( $key, $offset, $group );
}

/**
 * Increment a numeric item's value.
 *
 * This is the same as wp_cache_increment, but kept for back compatibility. The original
 * WordPress caching backends use wp_cache_incr. I want both to work.
 *
 * @link http://www.php.net/manual/en/memcached.increment.php
 *
 * @param string $key    The key under which to store the value.
 * @param int    $offset The amount by which to increment the item's value.
 * @param string $group  The group value appended to the $key.
 *
 * @return int|bool         Returns item's new value on success or FALSE on failure.
 */
function wp_cache_incr( $key, $offset = 1, $group = '' ) {
	return wp_cache_increment( $key, $offset, $group );
}

/**
 * Prepend data to an existing item.
 *
 * This method should throw an error if it is used with compressed data. This is an expected behavior.
 * Memcached casts the value to be prepended to the initial value to the type of the initial value. Be
 * careful as this leads to unexpected behavior at times. For instance, prepending (float) 45.23 to
 * (int) 23 will result in 45, because the value is first combined (45.2323) then cast to "integer"
 * (the original value), which will be (int) 45. Due to how memcached treats types, the behavior has been
 * mimicked in the internal cache to produce similar results and improve consistency. It is recommend
 * that prepends only occur with data of the same type.
 *
 * @link http://www.php.net/manual/en/memcached.prepend.php
 *
 * @param string $key    The key under which to store the value.
 * @param string $value  Must be string as prepending mixed values is not well-defined.
 * @param string $group  The group value prepended to the $key.
 *
 * @return bool             Returns TRUE on success or FALSE on failure.
 */
function wp_cache_prepend( $key, $value, $group = '' ) {
	global $wp_object_cache;

	return $wp_object_cache->prepend( $key, $value, $group );
}

/**
 * Append data to an existing item by server key.
 *
 * This method should throw an error if it is used with compressed data. This is an expected behavior.
 * Memcached casts the value to be prepended to the initial value to the type of the initial value. Be
 * careful as this leads to unexpected behavior at times. For instance, prepending (float) 45.23 to
 * (int) 23 will result in 45, because the value is first combined (45.2323) then cast to "integer"
 * (the original value), which will be (int) 45. Due to how memcached treats types, the behavior has been
 * mimicked in the internal cache to produce similar results and improve consistency. It is recommend
 * that prepends only occur with data of the same type.
 *
 * @link http://www.php.net/manual/en/memcached.prependbykey.php
 *
 * @param string $server_key     The key identifying the server to store the value on.
 * @param string $key            The key under which to store the value.
 * @param string $value          Must be string as prepending mixed values is not well-defined.
 * @param string $group          The group value prepended to the $key.
 *
 * @return bool                     Returns TRUE on success or FALSE on failure.
 */
function wp_cache_prepend_by_key( $server_key, $key, $value, $group = '' ) {
	global $wp_object_cache;

	return $wp_object_cache->prependByKey( $server_key, $key, $value, $group );
}

/**
 * Replaces a value in cache.
 *
 * This method is similar to "add"; however, is does not successfully set a value if
 * the object's key is not already set in cache.
 *
 * @link http://www.php.net/manual/en/memcached.replace.php
 *
 * @param string $key        The key under which to store the value.
 * @param mixed  $value      The value to store.
 * @param string $group      The group value appended to the $key.
 * @param int    $expiration The expiration time, defaults to 0.
 *
 * @return bool                 Returns TRUE on success or FALSE on failure.
 */
function wp_cache_replace( $key, $value, $group = '', $expiration = 0 ) {
	global $wp_object_cache;

	return $wp_object_cache->replace( $key, $value, $group, $expiration );
}

/**
 * Replaces a value in cache on a specific server.
 *
 * This method is similar to "addByKey"; however, is does not successfully set a value if
 * the object's key is not already set in cache.
 *
 * @link http://www.php.net/manual/en/memcached.addbykey.php
 *
 * @param string $server_key     The key identifying the server to store the value on.
 * @param string $key            The key under which to store the value.
 * @param mixed  $value          The value to store.
 * @param string $group          The group value appended to the $key.
 * @param int    $expiration     The expiration time, defaults to 0.
 *
 * @return bool                     Returns TRUE on success or FALSE on failure.
 */
function wp_cache_replace_by_key( $server_key, $key, $value, $group = '', $expiration = 0 ) {
	global $wp_object_cache;

	return $wp_object_cache->replaceByKey( $server_key, $key, $value, $group, $expiration );
}

/**
 * Sets a value in cache.
 *
 * The value is set whether or not this key already exists in memcached.
 *
 * @link http://www.php.net/manual/en/memcached.set.php
 *
 * @param string $key        The key under which to store the value.
 * @param mixed  $value      The value to store.
 * @param string $group      The group value appended to the $key.
 * @param int    $expiration The expiration time, defaults to 0.
 *
 * @return bool                 Returns TRUE on success or FALSE on failure.
 */
function wp_cache_set( $key, $value, $group = '', $expiration = 0 ) {
	global $wp_object_cache;

	return $wp_object_cache->set( $key, $value, $group, $expiration );
}

/**
 * Sets a value in cache.
 *
 * The value is set whether or not this key already exists in memcached.
 *
 * @link http://www.php.net/manual/en/memcached.set.php
 *
 * @param string $server_key     The key identifying the server to store the value on.
 * @param string $key            The key under which to store the value.
 * @param mixed  $value          The value to store.
 * @param string $group          The group value appended to the $key.
 * @param int    $expiration     The expiration time, defaults to 0.
 *
 * @return bool                     Returns TRUE on success or FALSE on failure.
 */
function wp_cache_set_by_key( $server_key, $key, $value, $group = '', $expiration = 0 ) {
	global $wp_object_cache;

	return $wp_object_cache->setByKey( $server_key, $key, $value, $group, $expiration );
}

/**
 * Set multiple values to cache at once.
 *
 * By sending an array of $items to this function, all values are saved at once to
 * memcached, reducing the need for multiple requests to memcached. The $items array
 * keys and values are what are stored to memcached. The keys in the $items array
 * are merged with the $groups array/string value via buildKeys to determine the
 * final key for the object.
 *
 * @param array        $items      An array of key/value pairs to store on the server.
 * @param string|array $groups     Group(s) to merge with key(s) in $items.
 * @param int          $expiration The expiration time, defaults to 0.
 *
 * @return bool                     Returns TRUE on success or FALSE on failure.
 */
function wp_cache_set_multi( $items, $groups = '', $expiration = 0 ) {
	global $wp_object_cache;

	return $wp_object_cache->setMulti( $items, $groups, $expiration );
}

/**
 * Set multiple values to cache at once on specified server.
 *
 * By sending an array of $items to this function, all values are saved at once to
 * memcached, reducing the need for multiple requests to memcached. The $items array
 * keys and values are what are stored to memcached. The keys in the $items array
 * are merged with the $groups array/string value via buildKeys to determine the
 * final key for the object.
 *
 * @param string       $server_key The key identifying the server to store the value on.
 * @param array        $items      An array of key/value pairs to store on the server.
 * @param string|array $groups     Group(s) to merge with key(s) in $items.
 * @param int          $expiration The expiration time, defaults to 0.
 *
 * @return bool                     Returns TRUE on success or FALSE on failure.
 */
function wp_cache_set_multi_by_key( $server_key, $items, $groups = 'default', $expiration = 0 ) {
	global $wp_object_cache;

	return $wp_object_cache->setMultiByKey( $server_key, $items, $groups, $expiration );
}

/**
 * Set a Memcached option.
 *
 * @link http://www.php.net/manual/en/memcached.setoption.php
 *
 * @param int   $option Option name.
 * @param mixed $value  Option value.
 *
 * @return bool             Returns TRUE on success or FALSE on failure.
 */
function wp_cache_set_option( $option, $value ) {
	global $wp_object_cache;

	return $wp_object_cache->setOption( $option, $value );
}

/**
 * Sets up Object Cache Global and assigns it.
 *
 * @global  WP_Object_Cache $wp_object_cache    WordPress Object Cache
 * @return  void
 */
function wp_cache_init() {
	global $wp_object_cache;
	$wp_object_cache = new WP_Object_Cache();
}

/**
 * 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.
 *
 * @return  void
 */
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-Memcached groups.
 *
 * @param   string|array $groups     A group or an array of groups to add.
 *
 * @return  void
 */
function wp_cache_add_non_persistent_groups( $groups ) {
	global $wp_object_cache;
	$wp_object_cache->add_non_persistent_groups( $groups );
}

class WP_Object_Cache {

	/**
	 * Holds the Redis client.
	 *
	 * @var Predis\Client
	 */
	public $redis;

	/**
	 * Holds the non-Redis objects.
	 *
	 * @var array
	 */
	public $cache = array();

	/**
	 * List of global groups.
	 *
	 * @var array
	 */
	public $global_groups = array( 'users', 'userlogins', 'usermeta', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss' );

	/**
	 * List of groups not saved to Redis.
	 *
	 * @var array
	 */
	public $no_redis_groups = array( 'comment', 'counts' );

	/**
	 * Prefix used for global groups.
	 *
	 * @var string
	 */
	public $global_prefix = '';

	/**
	 * Prefix used for non-global groups.
	 *
	 * @var string
	 */
	public $blog_prefix = '';

	/**
	 * Instantiate the Redis class.
	 *
	 * Instantiates the Redis class.
	 *
	 * @param   null $persistent_id      To create an instance that persists between requests, use persistent_id to specify a unique ID for the instance.
	 */
	public function __construct() {
		include 'predis/autoload.php';

		$this->redis = new Predis\Client( '' );

		global $blog_id, $table_prefix;

		/**
		 * This approach is borrowed from Sivel and Boren. Use the salt for easy cache invalidation and for
		 * multi single WP installs on the same server.
		 */
		if ( ! defined( 'WP_CACHE_KEY_SALT' ) ) {
			define( 'WP_CACHE_KEY_SALT', '' );
		}

		// Assign global and blog prefixes for use with keys
		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 ) . ':';
		}
	}

	/**
	 * Adds a value to cache.
	 *
	 * If the specified key already exists, the value is not stored and the function
	 * returns false.
	 *
	 * @param   string $key            The key under which to store the value.
	 * @param   mixed  $value          The value to store.
	 * @param   string $group          The group value appended to the $key.
	 * @param   int    $expiration     The expiration time, defaults to 0.
	 * @param   string $server_key     The key identifying the server to store the value on.
	 * @param   bool   $byKey          True to store in internal cache by key; false to not store by key
	 *
	 * @return  bool                        Returns TRUE on success or FALSE on failure.
	 */
	public function add( $key, $value, $group = 'default', $expiration = 0, $server_key = '', $byKey = false ) {
		$derived_key = $this->buildKey( $key, $group );

		// If group is a non-Redis group, save to runtime cache, not Redis
		if ( in_array( $group, $this->no_redis_groups ) ) {

			// Add does not set the value if the key exists; mimic that here
			if ( isset( $this->cache[$derived_key] ) ) {
				return false;
			}

			$this->add_to_internal_cache( $derived_key, $value );

			return true;
		}

Hans Kuijpers's avatar
Hans Kuijpers committed
835
		if ( $this->redis->exists( $derived_key ) ) {
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
			return false;
		}

		// Save to Redis
		$result = $this->redis->setex( $derived_key, $value, $expiration );

		return $result;
	}

	/**
	 * Remove the item from the cache.
	 *
	 * Remove an item from memcached with identified by $key after $time seconds. The
	 * $time parameter allows an object to be queued for deletion without immediately
	 * deleting. Between the time that it is queued and the time it's deleted, add,
	 * replace, and get will fail, but set will succeed.
	 *
	 * @link http://www.php.net/manual/en/memcached.delete.php
	 *
	 * @param   string $key        The key under which to store the value.
	 * @param   string $group      The group value appended to the $key.
	 * @param   int    $time       The amount of time the server will wait to delete the item in seconds.
	 * @param   string $server_key The key identifying the server to store the value on.
	 * @param   bool   $byKey      True to store in internal cache by key; false to not store by key
	 *
	 * @return  bool                    Returns TRUE on success or FALSE on failure.
	 */
	public function delete( $key, $group = 'default', $time = 0, $server_key = '', $byKey = false ) {
		$derived_key = $this->buildKey( $key, $group );

		// Remove from no_mc_groups array
		if ( in_array( $group, $this->no_redis_groups ) ) {
			if ( isset( $this->cache[$derived_key] ) ) {
				unset( $this->cache[$derived_key] );
			}

			return true;
		}

		$result = $this->redis->del( $derived_key );

		unset( $this->cache[$derived_key] );

		return $result;
	}

	/**
	 * Invalidate all items in the cache.
	 *
	 * @param   int $delay      Number of seconds to wait before invalidating the items.
	 *
	 * @return  bool                Returns TRUE on success or FALSE on failure.
	 */
	public function flush( $delay = 0 ) {
		$result = $this->redis->flushall();

		return $result;
	}

	/**
	 * Retrieve object from cache.
	 *
	 * Gets an object from cache based on $key and $group.
	 *
	 * @param   string        $key        The key under which to store the value.
	 * @param   string        $group      The group value appended to the $key.
	 * @param   string        $server_key The key identifying the server to store the value on.
	 * @param   bool          $byKey      True to store in internal cache by key; false to not store by key
	 *
	 * @return  bool|mixed                  Cached object value.
	 */
	public function get( $key, $group = 'default', $server_key = '', $byKey = false ) {
		$derived_key = $this->buildKey( $key, $group );

		if ( ! in_array( $group, $this->no_redis_groups ) ) {
			$value = $this->redis->get( $derived_key );
		} else {
			if ( isset( $this->cache[$derived_key] ) ) {
				return is_object( $this->cache[$derived_key] ) ? clone $this->cache[$derived_key] : $this->cache[$derived_key];
			} elseif ( in_array( $group, $this->no_redis_groups ) ) {
				return false;
			} else {
				$value = $this->redis->get( $derived_key );
			}
		}

		$this->add_to_internal_cache( $derived_key, $value );

		return is_object( $value ) ? clone $value : $value;
	}

	/**
	 * Sets a value in cache.
	 *
	 * The value is set whether or not this key already exists in memcached.
	 *
	 * @link http://www.php.net/manual/en/memcached.set.php
	 *
	 * @param   string $key        The key under which to store the value.
	 * @param   mixed  $value      The value to store.
	 * @param   string $group      The group value appended to the $key.
	 * @param   int    $expiration The expiration time, defaults to 0.
	 * @param   string $server_key The key identifying the server to store the value on.
	 * @param   bool   $byKey      True to store in internal cache by key; false to not store by key
	 *
	 * @return  bool                    Returns TRUE on success or FALSE on failure.
	 */
	public function set( $key, $value, $group = 'default', $expiration = 0, $server_key = '' ) {
		$derived_key = $this->buildKey( $key, $group );

		// If group is a non-Redis group, save to runtime cache, not Redis
		if ( in_array( $group, $this->no_redis_groups ) ) {
			$this->add_to_internal_cache( $derived_key, $value );

			return true;
		}

		// Save to Redis
		$result = $this->redis->setex( $derived_key, $value, absint( $expiration ) );

		$this->add_to_internal_cache( $derived_key, $value );

		return $result;
	}

	/**
	 * Builds a key for the cached object using the blog_id, key, and group values.
	 *
	 * @author  Ryan Boren   This function is inspired by the original WP Memcached Object cache.
	 * @link    http://wordpress.org/extend/plugins/memcached/
	 *
	 * @param   string $key        The key under which to store the value.
	 * @param   string $group      The group value appended to the $key.
	 *
	 * @return  string
	 */
	public function buildKey( $key, $group = 'default' ) {
		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" );
	}

	/**
	 * Simple wrapper for saving object to the internal cache.
	 *
	 * @param   string $derived_key    Key to save value under.
	 * @param   mixed  $value          Object value.
	 */
	public function add_to_internal_cache( $derived_key, $value ) {
		$this->cache[ $derived_key ] = $value;
	}

	/**
	 * Get a value specifically from the internal, run-time cache, not Redis.
	 *
	 * @param   int|string $key        Key value.
	 * @param   int|string $group      Group that the value belongs to.
	 *
	 * @return  bool|mixed              Value on success; false on failure.
	 */
	public function get_from_runtime_cache( $key, $group ) {
		$derived_key = $this->buildKey( $key, $group );

		if ( isset( $this->cache[ $derived_key ] ) ) {
			return $this->cache[ $derived_key ];
		}

		return false;
	}
Hans Kuijpers's avatar
Hans Kuijpers committed
1013
}