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
835
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
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
|
<?php /** * Hummingbird Redis Object Cache * * @link https://premium.wpmudev.org/project/wp-hummingbird/ * @since 2.5.0 * @package Hummingbird * * @wordpress-plugin * Plugin Name: Hummingbird Redis Object Cache * Plugin URI: https://premium.wpmudev.org/project/wp-hummingbird/ * Description: Hummingbird object cache powered by Redis. * Author: WPMU DEV * Author URI: https://profiles.wordpress.org/wpmudev/ * * Based on Eric Mann's and Erick Hitter's Redis Object Cache: https://github.com/ericmann/Redis-Object-Cache */
if ( ! defined( 'WPHB_REDIS_HOST' ) && ! defined( 'WPHB_REDIS_PORT' ) ) { return; }
/** * Adds data to the cache, if the cache key doesn't already exist. * * @global WP_Object_Cache $wp_object_cache Object cache global instance. * * @param int|string $key The cache key to use for retrieval later. * @param mixed $data The data to add to the cache. * @param string $group Optional. The group to add the cache to. Enables the same key * to be used across groups. Default empty. * @param int $expire Optional. When the cache data should expire, in seconds. * Default 0 (no expiration). * @return bool False if cache key and group already exist, true on success. * @throws Exception Exception. */ function wp_cache_add( $key, $data, $group = '', $expire = 0 ) { global $wp_object_cache; return $wp_object_cache->add( $key, $data, $group, (int) $expire ); }
/** * 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. * * @return true Always returns true. */ function wp_cache_close() { return true; }
/** * Decrements numeric cache item's value. * * @global WP_Object_Cache $wp_object_cache Object cache global instance. * * @param int|string $key The cache key to decrement. * @param int $offset Optional. The amount by which to decrement the item's value. Default 1. * @param string $group Optional. The group the key is in. Default empty. * @return false|int False on failure, the item's new value on success. */ function wp_cache_decr( $key, $offset = 1, $group = '' ) { global $wp_object_cache; return $wp_object_cache->decr( $key, $offset, $group ); }
/** * Removes the cache contents matching key and group. * * @global WP_Object_Cache $wp_object_cache Object cache global instance. * * @param int|string $key What the contents in the cache are called. * @param string $group Optional. Where the cache contents are grouped. Default empty. * @return bool True on successful removal, false on failure. */ function wp_cache_delete( $key, $group = '' ) { global $wp_object_cache; return $wp_object_cache->delete( $key, $group ); }
/** * Removes all cache items. * * @global WP_Object_Cache $wp_object_cache Object cache global instance. * * @return bool False on failure, true on success */ function wp_cache_flush() { global $wp_object_cache; return $wp_object_cache->flush(); }
/** * Retrieves the cache contents from the cache by key and group. * * @global WP_Object_Cache $wp_object_cache Object cache global instance. * * @param int|string $key The key under which the cache contents are stored. * @param string $group Optional. Where the cache contents are grouped. Default empty. * @param bool $force Optional. Whether to force an update of the local cache from the persistent * cache. Default false. * @param bool $found Optional. Whether the key was found in the cache (passed by reference). * Disambiguates a return of false, a storable value. Default null. * @return bool|mixed False on failure to retrieve contents or the cache * contents on success */ function wp_cache_get( $key, $group = '', $force = false, &$found = null ) { global $wp_object_cache; return $wp_object_cache->get( $key, $group, $force, $found ); }
/** * Retrieve multiple values from cache. * * Gets multiple values from cache, including across multiple groups. Mirrors the Memcached Object Cache * plugin's argument and return-value formats. * Usage: array( 'group0' => array( 'key0', 'key1', 'key2', ), 'group1' => array( 'key0' ) ) * * @global WP_Object_Cache $wp_object_cache Object cache global instance. * * @param array $groups Array of groups and keys to retrieve. * @return bool|mixed Array of cached values, keys in the format $group:$key. Non-existent keys false */ function wp_cache_get_multi( $groups ) { global $wp_object_cache; return $wp_object_cache->get_multi( $groups ); }
/** * Increment numeric cache item's value * * @global WP_Object_Cache $wp_object_cache Object cache global instance. * * @param int|string $key The key for the cache contents that should be incremented. * @param int $offset Optional. The amount by which to increment the item's value. Default 1. * @param string $group Optional. The group the key is in. Default empty. * @return false|int False on failure, the item's new value on success. */ function wp_cache_incr( $key, $offset = 1, $group = '' ) { global $wp_object_cache; return $wp_object_cache->incr( $key, $offset, $group ); }
/** * Sets up Object Cache Global and assigns it. * * @global WP_Object_Cache $wp_object_cache WordPress Object Cache * * @throws Exception Exception. */ function wp_cache_init() { global $wp_object_cache; $wp_object_cache = new WP_Object_Cache(); }
/** * Replaces the contents of the cache with new data. * * @global WP_Object_Cache $wp_object_cache Object cache global instance. * * @param int|string $key The key for the cache data that should be replaced. * @param mixed $data The new data to store in the cache. * @param string $group Optional. The group for the cache data that should be replaced. * Default empty. * @param int $expire Optional. When to expire the cache contents, in seconds. * Default 0 (no expiration). * @return bool False if original value does not exist, true if contents were replaced * @throws Exception Exception. */ function wp_cache_replace( $key, $data, $group = '', $expire = 0 ) { global $wp_object_cache; return $wp_object_cache->replace( $key, $data, $group, (int) $expire ); }
/** * Saves the data to the cache. * * Differs from wp_cache_add() and wp_cache_replace() in that it will always write data. * * @global WP_Object_Cache $wp_object_cache Object cache global instance. * * @param int|string $key The cache key to use for retrieval later. * @param mixed $data The contents to store in the cache. * @param string $group Optional. Where to group the cache contents. Enables the same key * to be used across groups. Default empty. * @param int $expire Optional. When to expire the cache contents, in seconds. * Default 0 (no expiration). * @return bool False on failure, true on success */ function wp_cache_set( $key, $data, $group = '', $expire = 0 ) { global $wp_object_cache; return $wp_object_cache->set( $key, $data, $group, (int) $expire ); }
/** * Switches the internal blog ID. * * This changes the blog id used to create keys in blog specific groups. * * @global WP_Object_Cache $wp_object_cache Object cache global instance. * * @param int $blog_id Site ID. */ function wp_cache_switch_to_blog( $blog_id ) { global $wp_object_cache; $wp_object_cache->switch_to_blog( $blog_id ); }
/** * Adds a group or set of groups to the list of global groups. * * @global WP_Object_Cache $wp_object_cache Object cache global instance. * * @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. * * @global WP_Object_Cache $wp_object_cache Object cache global instance. * * @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 ); }
/** * Class WP_Object_Cache */ class WP_Object_Cache {
/** * The Redis client. * * @var mixed */ private $redis;
/** * The Redis server version. * * @var null|string */ private $redis_version = null;
/** * Track if Redis is available * * @var bool */ private $redis_connected = false;
/** * Holds the non-Redis objects. * * @var array */ public $cache = array();
/** * Name of the used Redis client * * @var bool */ public $redis_client = null;
/** * List of global groups. * * @var array */ public $global_groups = array( 'blog-details', 'blog-id-cache', 'blog-lookup', 'global-posts', 'networks', 'rss', 'sites', 'site-details', 'site-lookup', 'site-options', 'site-transient', 'users', 'useremail', 'userlogins', 'usermeta', 'user_meta', 'userslugs', );
/** * List of groups that will not be flushed. * * @var array */ public $unflushable_groups = array();
/** * List of groups not saved to Redis. * * @var array */ public $ignored_groups = array( 'counts', 'plugins' );
/** * Prefix used for global groups. * * @var string */ public $global_prefix = '';
/** * Prefix used for non-global groups. * * @var string */ public $blog_prefix = '';
/** * Track how many requests were found in cache * * @var int */ public $cache_hits = 0;
/** * Track how may requests were not cached * * @var int */ public $cache_misses = 0;
/** * Error message for redis connection * * @var int */ public $redis_error = '';
/** * WP_Object_Cache constructor. * * @throws Exception Exception. */ public function __construct() { global $blog_id, $table_prefix;
$parameters = array( 'host' => defined( 'WPHB_REDIS_HOST' ) ? WPHB_REDIS_HOST : '127.0.0.1', 'port' => defined( 'WPHB_REDIS_PORT' ) ? WPHB_REDIS_PORT : 6379, 'timeout' => 5, 'read_timeout' => 5, 'retry_interval' => 0, );
$client = 'predis'; if ( class_exists( 'Redis' ) && 0 !== strcasecmp( 'predis', $client ) ) { $client = defined( 'HHVM_VERSION' ) ? 'hhvm' : 'pecl'; }
try { if ( 'hhvm' === $client ) { $this->redis_client = sprintf( 'HHVM Extension (v%s)', constant( 'HHVM_VERSION' ) );
$this->redis = new Redis(); $this->redis->connect( $parameters['host'], $parameters['port'], $parameters['timeout'], null, $parameters['retry_interval'] );
if ( $parameters['read_timeout'] ) { $this->redis->setOption( Redis::OPT_READ_TIMEOUT, $parameters['read_timeout'] ); } }
if ( 'pecl' === $client ) { $phpredis_version = phpversion( 'redis' ); $this->redis_client = sprintf( 'PECL Extension (v%s)', $phpredis_version );
$this->redis = new Redis(); if ( version_compare( $phpredis_version, '3.1.3', '>=' ) ) { $this->redis->connect( $parameters['host'], $parameters['port'], $parameters['timeout'], null, $parameters['retry_interval'], $parameters['read_timeout'] ); } else { $this->redis->connect( $parameters['host'], $parameters['port'], $parameters['timeout'], null, $parameters['retry_interval'] ); } }
if ( ( 'hhvm' === $client || 'pecl' === $client ) && defined( 'WPHB_REDIS_PASSWORD' ) ) { $this->redis->auth( WPHB_REDIS_PASSWORD ); }
if ( 'predis' === $client ) { $this->redis_client = 'Predis';
// Load bundled Predis library. if ( ! class_exists( 'Predis\Client' ) ) { $predis_pro = sprintf( '%s/wp-hummingbird/vendor/predis/predis/autoload.php', defined( 'WP_PLUGIN_DIR' ) ? WP_PLUGIN_DIR : WP_CONTENT_DIR . '/plugins' );
$predis_free = sprintf( '%s/hummingbird-performance/vendor/predis/predis/autoload.php', defined( 'WP_PLUGIN_DIR' ) ? WP_PLUGIN_DIR : WP_CONTENT_DIR . '/plugins' );
if ( file_exists( $predis_pro ) ) { /* @noinspection PhpIncludeInspection */ include_once $predis_pro; } elseif ( file_exists( $predis_free ) ) { /* @noinspection PhpIncludeInspection */ include_once $predis_free; } else { throw new Exception( 'Predis library not found. Re-install Hummingbird plugin or delete object-cache.php.' ); } }
$options = array();
if ( $parameters['read_timeout'] ) { $parameters['read_write_timeout'] = $parameters['read_timeout']; }
if ( defined( 'WPHB_REDIS_PASSWORD' ) ) { $options['parameters']['password'] = WPHB_REDIS_PASSWORD; }
$this->redis = new Predis\Client( $parameters, $options ); $this->redis->connect();
$this->redis_client .= sprintf( ' (v%s)', Predis\Client::VERSION ); }
$this->redis->ping();
$server_info = $this->redis->info( 'SERVER' );
if ( isset( $server_info['redis_version'] ) ) { $this->redis_version = $server_info['redis_version']; } elseif ( isset( $server_info['Server']['redis_version'] ) ) { $this->redis_version = $server_info['Server']['redis_version']; }
$this->redis_connected = true; } catch ( Exception $exception ) { $this->redis_connected = false;
$this->redis_error = $exception->getMessage();
// When Redis is unavailable, fall back to the internal cache by forcing all groups to be "no redis" groups. $this->ignored_groups = array_unique( array_merge( $this->ignored_groups, $this->global_groups ) ); }
// 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 ); } }
/** * Is Redis available? * * @return bool */ public function redis_status() { return $this->redis_connected; }
/** * Returns the Redis instance. * * @return mixed */ public function redis_instance() { return $this->redis; }
/** * Returns the Redis server version. * * @return null|string */ public function redis_version() { return $this->redis_version; }
/** * 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. */ public function add( $key, $value, $group = 'default', $expiration = 0 ) { return $this->add_or_replace( true, $key, $value, $group, (int) $expiration ); }
/** * Replace a value in the cache. * * If the specified key doesn't exist, 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. */ public function replace( $key, $value, $group = 'default', $expiration = 0 ) { return $this->add_or_replace( false, $key, $value, $group, (int) $expiration ); }
/** * Add or replace a value in the cache. * * Add does not set the value if the key exists; replace does not replace if the value doesn't exist. * * @param bool $add True if should only add if value doesn't exist, false to only add when value already exists. * @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. */ protected function add_or_replace( $add, $key, $value, $group = 'default', $expiration = 0 ) { $addition_suspended = function_exists( 'wp_suspend_cache_addition' ) ? wp_suspend_cache_addition() : false;
if ( $add && $addition_suspended ) { return false; }
$result = true; $derived_key = $this->build_key( $key, $group );
// Save if group not excluded and redis is up. if ( ! in_array( $group, $this->ignored_groups ) && $this->redis_status() ) { $exists = $this->redis->exists( $derived_key );
if ( $add == $exists ) { return false; }
$expiration = $this->validate_expiration( $expiration );
if ( $expiration ) { $result = $this->parse_redis_response( $this->redis->setex( $derived_key, $expiration, $this->maybe_serialize( $value ) ) ); } else { $result = $this->parse_redis_response( $this->redis->set( $derived_key, $this->maybe_serialize( $value ) ) ); } }
$exists = isset( $this->cache[ $derived_key ] );
if ( $add == $exists ) { return false; }
if ( $result ) { $this->add_to_internal_cache( $derived_key, $value ); }
return $result; }
/** * Remove the item from the cache. * * @param string $key The key under which to store the value. * @param string $group The group value appended to the $key. * @return bool Returns TRUE on success or FALSE on failure. */ public function delete( $key, $group = 'default' ) { $result = false; $derived_key = $this->build_key( $key, $group );
if ( isset( $this->cache[ $derived_key ] ) ) { unset( $this->cache[ $derived_key ] ); $result = true; }
if ( $this->redis_status() && ! in_array( $group, $this->ignored_groups ) ) { $result = $this->parse_redis_response( $this->redis->del( $derived_key ) ); }
return $result; }
/** * Invalidate all items in the cache. If `WP_REDIS_SELECTIVE_FLUSH` is `true`, * only keys prefixed with the `WP_CACHE_KEY_SALT` are flushed. * * @return bool Returns TRUE on success or FALSE on failure. */ public function flush() { $results = array(); $this->cache = array();
if ( $this->redis_status() ) { $results[] = $this->parse_redis_response( $this->redis->flushdb() ); }
if ( empty( $results ) ) { return false; }
foreach ( $results as $result ) { if ( ! $result ) { return false; } }
return true; }
/** * Returns a closure to flush selectively. * * @param string $salt The salt to be used to differentiate. * @return callable Generated callable executing the lua script. */ protected function get_flush_closure( $salt ) { if ( $this->unflushable_groups ) { return $this->lua_flush_extended_closure( $salt ); } else { return $this->lua_flush_closure( $salt ); } }
/** * Returns a closure ready to be called to flush selectively ignoring unflushable groups. * * @param string $salt The salt to be used to differentiate. * @return callable Generated callable executing the lua script. */ protected function lua_flush_closure( $salt ) { return function () use ( $salt ) { $script = <<<LUA local cur = 0 local i = 0 local tmp repeat tmp = redis.call('SCAN', cur, 'MATCH', '{$salt}*') cur = tonumber(tmp[1]) if tmp[2] then for _, v in pairs(tmp[2]) do redis.call('del', v) i = i + 1 end end until 0 == cur return i LUA;
if ( version_compare( $this->redis_version(), '5', '<' ) && version_compare( $this->redis_version(), '3.2', '>=' ) ) { $script = 'redis.replicate_commands()' . "\n" . $script; }
$args = ( $this->redis instanceof Predis\Client ) ? array( $script, 0 ) : array( $script );
return call_user_func_array( array( $this->redis, 'eval' ), $args ); }; }
/** * Returns a closure ready to be called to flush selectively. * * @param string $salt The salt to be used to differentiate. * @return callable Generated callable executing the lua script. */ protected function lua_flush_extended_closure( $salt ) { return function () use ( $salt ) { $salt_length = strlen( $salt );
$unflushable = array_map( function ( $group ) { return ":{$group}:"; }, $this->unflushable_groups );
$script = <<<LUA local cur = 0 local i = 0 local d, tmp repeat tmp = redis.call('SCAN', cur, 'MATCH', '{$salt}*') cur = tonumber(tmp[1]) if tmp[2] then for _, v in pairs(tmp[2]) do d = true for _, s in pairs(KEYS) do d = d and not v:find(s, {$salt_length}) if not d then break end end if d then redis.call('del', v) i = i + 1 end end end until 0 == cur return i LUA; if ( version_compare( $this->redis_version(), '5', '<' ) && version_compare( $this->redis_version(), '3.2', '>=' ) ) { $script = 'redis.replicate_commands()' . "\n" . $script; }
$args = ( $this->redis instanceof Predis\Client ) ? array_merge( array( $script, count( $unflushable ) ), $unflushable ) : array( $script, $unflushable, count( $unflushable ) );
return call_user_func_array( array( $this->redis, 'eval' ), $args ); }; }
/** * 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 bool $force Optional. Whether to force a refetch rather than relying on the local * cache. Default false. * @param bool|null $found Optional. Whether the key was found in the cache. Disambiguates a return of * false, a storable value. Passed by reference. Default null. * @return bool|mixed Cached object value. */ public function get( $key, $group = 'default', $force = false, &$found = null ) { $derived_key = $this->build_key( $key, $group );
if ( isset( $this->cache[ $derived_key ] ) && ! $force ) { $found = true; $this->cache_hits++; return is_object( $this->cache[ $derived_key ] ) ? clone $this->cache[ $derived_key ] : $this->cache[ $derived_key ]; } elseif ( in_array( $group, $this->ignored_groups ) || ! $this->redis_status() ) { $found = false; $this->cache_misses++; return false; }
$result = $this->redis->get( $derived_key ); if ( null === $result || false === $result ) { $found = false; $this->cache_misses++; return false; } else { $found = true; $this->cache_hits++; // All non-numeric values are serialized. $result = $this->maybe_unserialize( $result ); }
$this->add_to_internal_cache( $derived_key, $result );
return $result; }
/** * Retrieve multiple values from cache. * * Gets multiple values from cache, including across multiple groups. Mirrors the Memcached Object Cache plugin's * argument and return-value formats. * Usage: array( 'group0' => array( 'key0', 'key1', 'key2', ), 'group1' => array( 'key0' ) ). * * @param array $groups Array of groups and keys to retrieve. * @return bool|mixed Array of cached values, keys in the format $group:$key. Non-existent keys null. */ public function get_multi( $groups ) { if ( empty( $groups ) || ! is_array( $groups ) ) { return false; }
// Retrieve requested caches and reformat results to mimic Memcached Object Cache's output. $cache = array();
foreach ( $groups as $group => $keys ) { if ( in_array( $group, $this->ignored_groups ) || ! $this->redis_status() ) { foreach ( $keys as $key ) { $cache[ $this->build_key( $key, $group ) ] = $this->get( $key, $group ); } } else { // Reformat arguments as expected by Redis. $derived_keys = array();
foreach ( $keys as $key ) { $derived_keys[] = $this->build_key( $key, $group ); }
// Retrieve from cache in a single request. $group_cache = $this->redis->mget( $derived_keys );
// Build an array of values looked up, keyed by the derived cache key. $group_cache = array_combine( $derived_keys, $group_cache );
// Restores cached data to its original data type. $group_cache = array_map( array( $this, 'maybe_unserialize' ), $group_cache );
// Redis returns null for values not found in cache, but expected return value is false in this instance. $group_cache = array_map( array( $this, 'filter_redis_get_multi' ), $group_cache );
$cache = array_merge( $cache, $group_cache ); } }
// Add to the internal cache the found values from Redis. foreach ( $cache as $key => $value ) { if ( $value ) { $this->cache_hits++; $this->add_to_internal_cache( $key, $value ); } else { $this->cache_misses++; } }
return $cache; }
/** * Sets a value in cache. * * The value is set whether or not this key already exists in Redis. * * @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. */ public function set( $key, $value, $group = 'default', $expiration = 0 ) { $result = true; $derived_key = $this->build_key( $key, $group );
// Save if group not excluded from redis and redis is up. if ( ! in_array( $group, $this->ignored_groups ) && $this->redis_status() ) { $expiration = $this->validate_expiration( $expiration );
if ( $expiration ) { $result = $this->parse_redis_response( $this->redis->setex( $derived_key, $expiration, $this->maybe_serialize( $value ) ) ); } else { $result = $this->parse_redis_response( $this->redis->set( $derived_key, $this->maybe_serialize( $value ) ) ); } }
// If the set was successful, or we didn't go to redis. if ( $result ) { $this->add_to_internal_cache( $derived_key, $value ); }
return $result; }
/** * Increment a Redis counter by the amount specified. * * @param int|string $key The cache key to increment. * @param int $offset Optional. The amount by which to increment the item's value. Default 1. * @param string $group Optional. The group the key is in. Default 'default'. * @return false|int False on failure, the item's new value on success. */ public function incr( $key, $offset = 1, $group = 'default' ) { $derived_key = $this->build_key( $key, $group ); $offset = (int) $offset;
// If group is a non-Redis group, save to internal cache, not Redis. if ( in_array( $group, $this->ignored_groups ) || ! $this->redis_status() ) { $value = $this->get_from_internal_cache( $derived_key, $group ); $value += $offset; $this->add_to_internal_cache( $derived_key, $value );
return $value; }
// Save to Redis. $result = $this->parse_redis_response( $this->redis->incrBy( $derived_key, $offset ) ); $this->add_to_internal_cache( $derived_key, (int) $this->redis->get( $derived_key ) );
return $result; }
/** * Decrement a Redis counter by the amount specified. * * @param int|string $key The cache key to decrement. * @param int $offset Optional. The amount by which to decrement the item's value. Default 1. * @param string $group Optional. The group the key is in. Default 'default'. * @return false|int False on failure, the item's new value on success. */ public function decr( $key, $offset = 1, $group = 'default' ) { $derived_key = $this->build_key( $key, $group ); $offset = (int) $offset;
// If group is a non-Redis group, save to internal cache, not Redis. if ( in_array( $group, $this->ignored_groups ) || ! $this->redis_status() ) { $value = $this->get_from_internal_cache( $derived_key, $group ); $value -= $offset; $this->add_to_internal_cache( $derived_key, $value );
return $value; }
// Save to Redis. $result = $this->parse_redis_response( $this->redis->decrBy( $derived_key, $offset ) ); $this->add_to_internal_cache( $derived_key, (int) $this->redis->get( $derived_key ) );
return $result; }
/** * Builds a key for the cached object using the prefix, group and key. * * @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 build_key( $key, $group = 'default' ) { if ( empty( $group ) ) { $group = 'default'; }
$salt = defined( 'WP_CACHE_KEY_SALT' ) ? trim( WP_CACHE_KEY_SALT ) : ''; $prefix = in_array( $group, $this->global_groups ) ? $this->global_prefix : $this->blog_prefix;
$key = str_replace( ':', '-', $key ); $group = str_replace( ':', '-', $group );
$prefix = trim( $prefix, '_-:$' );
return "{$salt}{$prefix}:{$group}:{$key}"; }
/** * Convert data types when using Redis MGET * * When requesting multiple keys, those not found in cache are assigned the value null upon return. * Expected value in this case is false, so we convert * * @param string $value Value to possibly convert. * @return string Converted value */ protected function filter_redis_get_multi( $value ) { if ( is_null( $value ) ) { $value = false; }
return $value; }
/** * Convert Redis responses into something meaningful * * @param mixed $response Redis response. * @return mixed */ protected function parse_redis_response( $response ) { if ( is_bool( $response ) ) { return $response; }
if ( is_numeric( $response ) ) { return $response; }
if ( is_object( $response ) && method_exists( $response, 'getPayload' ) ) { return $response->getPayload() === 'OK'; }
return false; }
/** * 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_internal_cache( $key, $group ) { $derived_key = $this->build_key( $key, $group );
if ( isset( $this->cache[ $derived_key ] ) ) { return $this->cache[ $derived_key ]; }
return false; }
/** * In multisite, switch blog prefix when switching blogs * * @param int $_blog_id Blog ID. * @return bool */ public function switch_to_blog( $_blog_id ) { if ( ! function_exists( 'is_multisite' ) || ! is_multisite() ) { return false; }
$this->blog_prefix = $_blog_id; return true; }
/** * Sets the list of global groups. * * @param array $groups List of groups that are global. */ public function add_global_groups( $groups ) { $groups = (array) $groups;
if ( $this->redis_status() ) { $this->global_groups = array_unique( array_merge( $this->global_groups, $groups ) ); } else { $this->ignored_groups = array_unique( array_merge( $this->ignored_groups, $groups ) ); } }
/** * Sets the list of groups not to be cached by Redis. * * @param array $groups List of groups that are to be ignored. */ public function add_non_persistent_groups( $groups ) { $groups = (array) $groups;
$this->ignored_groups = array_unique( array_merge( $this->ignored_groups, $groups ) ); }
/** * Render data about current cache requests. */ public function stats() { ?> <p> <strong>Redis Status:</strong> <?php echo $this->redis_status() ? 'Connected' : 'Not Connected'; ?><br /> <strong>Redis Client:</strong> <?php echo $this->redis_client; ?><br /> <strong>Cache Hits:</strong> <?php echo $this->cache_hits; ?><br /> <strong>Cache Misses:</strong> <?php echo $this->cache_misses; ?> </p>
<ul> <?php foreach ( $this->cache as $group => $cache ) : ?> <li> <?php printf( '%s - %sk', strip_tags( $group ), number_format( strlen( serialize( $cache ) ) / 1024, 2 ) ); ?> </li> <?php endforeach; ?> </ul> <?php }
/** * Serialize data, if needed. * * @param string|array|object $data Data that might be serialized. * * @return mixed A scalar data */ private function maybe_serialize( $data ) { if ( is_array( $data ) || is_object( $data ) ) { return serialize( $data ); }
/* * Double serialization is required for backward compatibility. * See https://core.trac.wordpress.org/ticket/12930 */ if ( $this->is_serialized( $data, false ) ) { return serialize( $data ); }
return $data; }
/** * Unserialize value only if it was serialized. * * @param string $original Maybe unserialized original, if is needed. * * @return mixed Unserialized data can be any type. */ function maybe_unserialize( $original ) { if ( $this->is_serialized( $original ) ) { // Don't attempt to unserialize data that wasn't serialized going in. return @unserialize( $original ); }
return $original; }
/** * Check value to find if it was serialized. * * If $data is not an string, then returned value will always be false. * Serialized data is always a string. * * @param string $data Value to check to see if was serialized. * @param bool $strict Optional. Whether to be strict about the end of the string. Default true. * * @return bool False if not serialized and true if it was. */ private function is_serialized( $data, $strict = true ) { // If it isn't a string, it isn't serialized. if ( ! is_string( $data ) ) { return false; }
$data = trim( $data ); if ( 'N;' == $data ) { return true; }
if ( strlen( $data ) < 4 ) { return false; }
if ( ':' !== $data[1] ) { return false; }
if ( $strict ) { $lastc = substr( $data, -1 ); if ( ';' !== $lastc && '}' !== $lastc ) { return false; } } else { $semicolon = strpos( $data, ';' ); $brace = strpos( $data, '}' ); // Either ; or } must exist. if ( false === $semicolon && false === $brace ) { return false; } // But neither must be in the first X characters. if ( false !== $semicolon && $semicolon < 3 ) { return false; } if ( false !== $brace && $brace < 4 ) { return false; } }
$token = $data[0]; switch ( $token ) { case 's': if ( $strict ) { if ( '"' !== substr( $data, -2, 1 ) ) { return false; } } elseif ( false === strpos( $data, '"' ) ) { return false; } // Or else fall through. case 'a': case 'O': return (bool) preg_match( "/^{$token}:[0-9]+:/s", $data ); case 'b': case 'i': case 'd': $end = $strict ? '$' : ''; return (bool) preg_match( "/^{$token}:[0-9.E+-]+;$end/", $data ); }
return false; }
/** * Wrapper to validate the cache keys expiration value. * * @param mixed $expiration Incoming expiration value (whatever it is). * * @return int */ protected function validate_expiration( $expiration ) { $expiration = is_int( $expiration ) || ctype_digit( $expiration ) ? (int) $expiration : 0; return $expiration; }
}
|