33 * Plugin Name: Redis Object Cache Drop-In
44 * Plugin URI: https://wordpress.org/plugins/redis-cache/
55 * Description: A persistent object cache backend powered by Redis. Supports Predis, PhpRedis, Relay, replication, sentinels, clustering and WP-CLI.
6- * Version: 2.6.5
6+ * Version: 2.7.0
77 * Author: Till Krüss
88 * Author URI: https://objectcache.pro
99 * License: GPLv3
@@ -50,17 +50,17 @@ function wp_cache_supports( $feature ) {
5050 * If the specified key already exists, the value is not stored and the function
5151 * returns false.
5252 *
53- * @param string $key The key under which to store the value.
54- * @param mixed $value The value to store.
55- * @param string $group The group value appended to the $key.
56- * @param int $expiration The expiration time, defaults to 0.
53+ * @param string $key The key under which to store the value.
54+ * @param mixed $data The value to store.
55+ * @param string $group The group value appended to the $key.
56+ * @param int $expire The expiration time, defaults to 0.
5757 *
58- * @return bool Returns TRUE on success or FALSE on failure.
58+ * @return bool Returns TRUE on success or FALSE on failure.
5959 */
60- function wp_cache_add ( $ key , $ value , $ group = '' , $ expiration = 0 ) {
60+ function wp_cache_add ( $ key , $ data , $ group = '' , $ expire = 0 ) {
6161 global $ wp_object_cache ;
6262
63- return $ wp_object_cache ->add ( $ key , $ value , $ group , $ expiration );
63+ return $ wp_object_cache ->add ( $ key , $ data , $ group , $ expire );
6464}
6565
6666/**
@@ -263,35 +263,35 @@ function wp_cache_init() {
263263 * This method is similar to "add"; however, is does not successfully set a value if
264264 * the object's key is not already set in cache.
265265 *
266- * @param string $key The key under which to store the value.
267- * @param mixed $value The value to store.
268- * @param string $group The group value appended to the $key.
269- * @param int $expiration The expiration time, defaults to 0.
266+ * @param string $key The key under which to store the value.
267+ * @param mixed $data The value to store.
268+ * @param string $group The group value appended to the $key.
269+ * @param int $expire The expiration time, defaults to 0.
270270 *
271- * @return bool Returns TRUE on success or FALSE on failure.
271+ * @return bool Returns TRUE on success or FALSE on failure.
272272 */
273- function wp_cache_replace ( $ key , $ value , $ group = '' , $ expiration = 0 ) {
273+ function wp_cache_replace ( $ key , $ data , $ group = '' , $ expire = 0 ) {
274274 global $ wp_object_cache ;
275275
276- return $ wp_object_cache ->replace ( $ key , $ value , $ group , $ expiration );
276+ return $ wp_object_cache ->replace ( $ key , $ data , $ group , $ expire );
277277}
278278
279279/**
280280 * Sets a value in cache.
281281 *
282282 * The value is set whether or not this key already exists in Redis.
283283 *
284- * @param string $key The key under which to store the value.
285- * @param mixed $value The value to store.
286- * @param string $group The group value appended to the $key.
287- * @param int $expiration The expiration time, defaults to 0.
284+ * @param string $key The key under which to store the value.
285+ * @param mixed $data The value to store.
286+ * @param string $group The group value appended to the $key.
287+ * @param int $expire The expiration time, defaults to 0.
288288 *
289- * @return bool Returns TRUE on success or FALSE on failure.
289+ * @return bool Returns TRUE on success or FALSE on failure.
290290 */
291- function wp_cache_set ( $ key , $ value , $ group = '' , $ expiration = 0 ) {
291+ function wp_cache_set ( $ key , $ data , $ group = '' , $ expire = 0 ) {
292292 global $ wp_object_cache ;
293293
294- return $ wp_object_cache ->set ( $ key , $ value , $ group , $ expiration );
294+ return $ wp_object_cache ->set ( $ key , $ data , $ group , $ expire );
295295}
296296
297297/**
@@ -315,14 +315,14 @@ function wp_cache_set_multiple( array $data, $group = '', $expire = 0 ) {
315315 *
316316 * This changes the blog id used to create keys in blog specific groups.
317317 *
318- * @param int $_blog_id The blog ID.
318+ * @param int $blog_id The blog ID.
319319 *
320320 * @return bool
321321 */
322- function wp_cache_switch_to_blog ( $ _blog_id ) {
322+ function wp_cache_switch_to_blog ( $ blog_id ) {
323323 global $ wp_object_cache ;
324324
325- return $ wp_object_cache ->switch_to_blog ( $ _blog_id );
325+ return $ wp_object_cache ->switch_to_blog ( $ blog_id );
326326}
327327
328328/**
@@ -384,6 +384,13 @@ class WP_Object_Cache {
384384 */
385385 private $ fail_gracefully = true ;
386386
387+ /**
388+ * Whether to use igbinary serialization.
389+ *
390+ * @var bool
391+ */
392+ private $ use_igbinary = false ;
393+
387394 /**
388395 * Holds the non-Redis objects.
389396 *
@@ -519,18 +526,13 @@ public function __construct( $fail_gracefully = false ) {
519526
520527 $ this ->cache_group_types ();
521528
522- if ( defined ( 'WP_REDIS_TRACE ' ) && WP_REDIS_TRACE ) {
523- trigger_error ('Tracing feature was removed ' , E_USER_DEPRECATED );
524- }
529+ $ this ->use_igbinary = defined ( 'WP_REDIS_IGBINARY ' ) && WP_REDIS_IGBINARY && extension_loaded ( 'igbinary ' );
525530
526531 $ client = $ this ->determine_client ();
527532 $ parameters = $ this ->build_parameters ();
528533
529534 try {
530535 switch ( $ client ) {
531- case 'hhvm ' :
532- $ this ->connect_using_hhvm ( $ parameters );
533- break ;
534536 case 'phpredis ' :
535537 $ this ->connect_using_phpredis ( $ parameters );
536538 break ;
@@ -600,7 +602,7 @@ protected function determine_client() {
600602 $ client = 'predis ' ;
601603
602604 if ( class_exists ( 'Redis ' ) ) {
603- $ client = defined ( ' HHVM_VERSION ' ) ? ' hhvm ' : 'phpredis ' ;
605+ $ client = 'phpredis ' ;
604606 }
605607
606608 if ( defined ( 'WP_REDIS_CLIENT ' ) ) {
@@ -753,12 +755,6 @@ protected function connect_using_phpredis( $parameters ) {
753755
754756 $ this ->diagnostics += $ args ;
755757 }
756-
757- if ( defined ( 'WP_REDIS_SERIALIZER ' ) && ! empty ( WP_REDIS_SERIALIZER ) ) {
758- $ this ->redis ->setOption ( Redis::OPT_SERIALIZER , WP_REDIS_SERIALIZER );
759-
760- trigger_error ('The `WP_REDIS_SERIALIZER` configuration constant has been deprecated in favor of `WP_REDIS_IGBINARY` ' , E_USER_DEPRECATED );
761- }
762758 }
763759
764760 /**
@@ -827,12 +823,6 @@ protected function connect_using_relay( $parameters ) {
827823
828824 $ this ->diagnostics += $ args ;
829825 }
830-
831- if ( defined ( 'WP_REDIS_SERIALIZER ' ) && ! empty ( WP_REDIS_SERIALIZER ) ) {
832- $ this ->redis ->setOption ( Relay \Relay::OPT_SERIALIZER , WP_REDIS_SERIALIZER );
833-
834- trigger_error ('The `WP_REDIS_SERIALIZER` configuration constant has been deprecated in favor of `WP_REDIS_IGBINARY` ' , E_USER_DEPRECATED );
835- }
836826 }
837827
838828 /**
@@ -1079,55 +1069,6 @@ protected function connect_using_credis( $parameters ) {
10791069 );
10801070 }
10811071
1082- /**
1083- * Connect to Redis using HHVM's Redis extension.
1084- *
1085- * @param array $parameters Connection parameters built by the `build_parameters` method.
1086- * @return void
1087- */
1088- protected function connect_using_hhvm ( $ parameters ) {
1089- trigger_error ('HHVM support is deprecated and will be removed in the future ' , E_USER_DEPRECATED );
1090-
1091- $ this ->redis = new Redis ();
1092-
1093- // Adjust host and port if the scheme is `unix`.
1094- if ( strcasecmp ( 'unix ' , $ parameters ['scheme ' ] ) === 0 ) {
1095- $ parameters ['host ' ] = 'unix:// ' . $ parameters ['path ' ];
1096- $ parameters ['port ' ] = 0 ;
1097- }
1098-
1099- $ this ->redis ->connect (
1100- $ parameters ['host ' ],
1101- $ parameters ['port ' ],
1102- $ parameters ['timeout ' ],
1103- null ,
1104- $ parameters ['retry_interval ' ]
1105- );
1106-
1107- if ( $ parameters ['read_timeout ' ] ) {
1108- $ this ->redis ->setOption ( Redis::OPT_READ_TIMEOUT , $ parameters ['read_timeout ' ] );
1109- }
1110-
1111- if ( isset ( $ parameters ['password ' ] ) ) {
1112- $ this ->redis ->auth ( $ parameters ['password ' ] );
1113- }
1114-
1115- if ( isset ( $ parameters ['database ' ] ) ) {
1116- if ( ctype_digit ( (string ) $ parameters ['database ' ] ) ) {
1117- $ parameters ['database ' ] = (int ) $ parameters ['database ' ];
1118- }
1119-
1120- if ( $ parameters ['database ' ] ) {
1121- $ this ->redis ->select ( $ parameters ['database ' ] );
1122- }
1123- }
1124-
1125- $ this ->diagnostics = array_merge (
1126- [ 'client ' => sprintf ( 'HHVM Extension (v%s) ' , HHVM_VERSION ) ],
1127- $ parameters
1128- );
1129- }
1130-
11311072 /**
11321073 * Fetches Redis `INFO` mostly for server version.
11331074 *
@@ -2412,13 +2353,28 @@ public function increment( $key, $offset = 1, $group = 'default' ) {
24122353 }
24132354
24142355 try {
2415- $ value = (int ) $ this ->parse_redis_response ( $ this ->maybe_unserialize ( $ this ->redis ->get ( $ derived_key ) ) );
2416- $ value += $ offset ;
2417- $ result = $ this ->parse_redis_response ( $ this ->redis ->set ( $ derived_key , $ this ->maybe_serialize ( $ value ) ) );
2356+ if ( $ this ->use_igbinary ) {
2357+ $ value = (int ) $ this ->parse_redis_response ( $ this ->maybe_unserialize ( $ this ->redis ->get ( $ derived_key ) ) );
2358+ $ value += $ offset ;
2359+ $ serialized = $ this ->maybe_serialize ( $ value );
24182360
2419- if ( $ result ) {
2420- $ this ->add_to_internal_cache ( $ derived_key , $ value );
2421- $ result = $ value ;
2361+ if ( ($ pttl = $ this ->redis ->pttl ( $ derived_key )) > 0 ) {
2362+ if ( $ this ->is_predis () ) {
2363+ $ result = $ this ->parse_redis_response ( $ this ->redis ->set ( $ derived_key , $ serialized , 'px ' , $ pttl ) );
2364+ } else {
2365+ $ result = $ this ->parse_redis_response ( $ this ->redis ->set ( $ derived_key , $ serialized , [ 'px ' => $ pttl ] ) );
2366+ }
2367+ } else {
2368+ $ result = $ this ->parse_redis_response ( $ this ->redis ->set ( $ derived_key , $ serialized ) );
2369+ }
2370+
2371+ if ( $ result ) {
2372+ $ this ->add_to_internal_cache ( $ derived_key , $ value );
2373+ $ result = $ value ;
2374+ }
2375+ } else {
2376+ $ result = $ this ->parse_redis_response ( $ this ->redis ->incrBy ( $ derived_key , $ offset ) );
2377+ $ this ->add_to_internal_cache ( $ derived_key , (int ) $ this ->redis ->get ( $ derived_key ) );
24222378 }
24232379 } catch ( Exception $ exception ) {
24242380 $ this ->handle_exception ( $ exception );
@@ -2474,13 +2430,28 @@ public function decrement( $key, $offset = 1, $group = 'default' ) {
24742430 }
24752431
24762432 try {
2477- $ value = (int ) $ this ->parse_redis_response ( $ this ->maybe_unserialize ( $ this ->redis ->get ( $ derived_key ) ) );
2478- $ value -= $ offset ;
2479- $ result = $ this ->parse_redis_response ( $ this ->redis ->set ( $ derived_key , $ this ->maybe_serialize ( $ value ) ) );
2433+ if ( $ this ->use_igbinary ) {
2434+ $ value = (int ) $ this ->parse_redis_response ( $ this ->maybe_unserialize ( $ this ->redis ->get ( $ derived_key ) ) );
2435+ $ value -= $ offset ;
2436+ $ serialized = $ this ->maybe_serialize ( $ value );
24802437
2481- if ( $ result ) {
2482- $ this ->add_to_internal_cache ( $ derived_key , $ value );
2483- $ result = $ value ;
2438+ if ( ($ pttl = $ this ->redis ->pttl ( $ derived_key )) > 0 ) {
2439+ if ( $ this ->is_predis () ) {
2440+ $ result = $ this ->parse_redis_response ( $ this ->redis ->set ( $ derived_key , $ serialized , 'px ' , $ pttl ) );
2441+ } else {
2442+ $ result = $ this ->parse_redis_response ( $ this ->redis ->set ( $ derived_key , $ serialized , [ 'px ' => $ pttl ] ) );
2443+ }
2444+ } else {
2445+ $ result = $ this ->parse_redis_response ( $ this ->redis ->set ( $ derived_key , $ serialized ) );
2446+ }
2447+
2448+ if ( $ result ) {
2449+ $ this ->add_to_internal_cache ( $ derived_key , $ value );
2450+ $ result = $ value ;
2451+ }
2452+ } else {
2453+ $ result = $ this ->parse_redis_response ( $ this ->redis ->decrBy ( $ derived_key , $ offset ) );
2454+ $ this ->add_to_internal_cache ( $ derived_key , (int ) $ this ->redis ->get ( $ derived_key ) );
24842455 }
24852456 } catch ( Exception $ exception ) {
24862457 $ this ->handle_exception ( $ exception );
@@ -2808,11 +2779,7 @@ protected function validate_expiration( $expiration ) {
28082779 * @return mixed Unserialized data can be any type.
28092780 */
28102781 protected function maybe_unserialize ( $ original ) {
2811- if ( defined ( 'WP_REDIS_SERIALIZER ' ) && ! empty ( WP_REDIS_SERIALIZER ) ) {
2812- return $ original ;
2813- }
2814-
2815- if ( defined ( 'WP_REDIS_IGBINARY ' ) && WP_REDIS_IGBINARY && function_exists ( 'igbinary_unserialize ' ) ) {
2782+ if ( $ this ->use_igbinary ) {
28162783 return igbinary_unserialize ( $ original );
28172784 }
28182785
@@ -2838,11 +2805,7 @@ protected function maybe_serialize( $data ) {
28382805 $ data = clone $ data ;
28392806 }
28402807
2841- if ( defined ( 'WP_REDIS_SERIALIZER ' ) && ! empty ( WP_REDIS_SERIALIZER ) ) {
2842- return $ data ;
2843- }
2844-
2845- if ( defined ( 'WP_REDIS_IGBINARY ' ) && WP_REDIS_IGBINARY && function_exists ( 'igbinary_serialize ' ) ) {
2808+ if ( $ this ->use_igbinary ) {
28462809 return igbinary_serialize ( $ data );
28472810 }
28482811
0 commit comments