From 8bcdffc214e5bd03b36faea014acfc695440c64b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Till=20Kru=CC=88ss?= Date: Mon, 18 Jul 2022 17:54:14 -0700 Subject: [PATCH 01/10] remove tracing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit this was contributed by a user a while back, is rather incomplete at this point and I don’t want to maintain it --- CHANGELOG.md | 4 + includes/object-cache.php | 298 +------------------------------------- 2 files changed, 8 insertions(+), 294 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 96e40d5a..39c32688 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 2.1.2 + +- Removed tracing + ## 2.1.1 - Bumped PHP requirement to 7.2 diff --git a/includes/object-cache.php b/includes/object-cache.php index 1f96e13f..d2e6a984 100644 --- a/includes/object-cache.php +++ b/includes/object-cache.php @@ -319,49 +319,6 @@ function wp_cache_add_non_persistent_groups( $groups ) { * Object cache class definition */ class WP_Object_Cache { - /** - * Operation pertains to internal cache, not Redis. - * - * @since 2.0.18 - * @var int - */ - const TRACE_FLAG_INTERNAL = 1 << 0; - /** - * Operation resulted in a cache hit. - * - * @since 2.0.18 - * @var int - */ - const TRACE_FLAG_HIT = 1 << 1; - /** - * Read operation. - * - * @since 2.0.18 - * @var int - */ - const TRACE_FLAG_READ = 1 << 2; - /** - * Write operation. - * - * @since 2.0.18 - * @var int - */ - const TRACE_FLAG_WRITE = 1 << 3; - /** - * Delete operation. - * - * @since 2.0.18 - * @var int - */ - const TRACE_FLAG_DEL = 1 << 4; - /** - * Operation bypassed internal cache. - * - * @since 2.0.18 - * @var int - */ - const TRACE_FLAG_REFRESH = 1 << 5; - /** * The Redis client. * @@ -404,13 +361,6 @@ class WP_Object_Cache { */ public $diagnostics = null; - /** - * Holds the error messages. - * - * @var array - */ - public $trace_enabled = false; - /** * Holds the error messages. * @@ -536,8 +486,8 @@ public function __construct( $fail_gracefully = true ) { $this->cache_group_types(); - if ( defined( 'WP_REDIS_TRACE' ) && WP_REDIS_TRACE ) { - $this->trace_enabled = true; + if ( defined( 'WP_REDIS_TRACE' ) && WP_REDIS_TRACE && function_exists( '_doing_it_wrong' ) ) { + _doing_it_wrong( __FUNCTION__ , 'Tracing feature was removed.' , '2.1.2' ); } $client = $this->determine_client(); @@ -1215,7 +1165,6 @@ protected function add_multiple_at_once( array $data, $group = 'default', $expir $orig_exp = $expire; $expire = $this->validate_expiration( $expire ); $tx = $this->redis->pipeline(); - $traceKV = []; foreach ( $data as $key => $value ) { /** This action is documented in includes/object-cache.php */ @@ -1243,11 +1192,6 @@ protected function add_multiple_at_once( array $data, $group = 'default', $expir } } - $traceKV[ $key ] = [ - 'value' => $value, - 'status' => self::TRACE_FLAG_WRITE, - ]; - $tx->set( ...$args ); } @@ -1264,10 +1208,6 @@ protected function add_multiple_at_once( array $data, $group = 'default', $expir $execute_time = microtime( true ) - $start_time; - if ( $this->trace_enabled ) { - $this->trace_command( 'set', $group, $traceKV, microtime( true ) - $start_time ); - } - $this->cache_calls++; $this->cache_time += $execute_time; } catch ( Exception $exception ) { @@ -1382,15 +1322,6 @@ protected function add_or_replace( $add, $key, $value, $group = 'default', $expi $execute_time = microtime( true ) - $start_time; - if ( $this->trace_enabled ) { - $this->trace_command( 'set', $group, [ - $key => [ - 'value' => $value, - 'status' => self::TRACE_FLAG_WRITE, - ], - ], microtime( true ) - $start_time ); - } - $this->cache_calls++; $this->cache_time += $execute_time; } catch ( Exception $exception ) { @@ -1447,15 +1378,6 @@ public function delete( $key, $group = 'default' ) { $execute_time = microtime( true ) - $start_time; - if ( $this->trace_enabled ) { - $this->trace_command( 'del', $group, [ - $key => [ - 'value' => null, - 'status' => self::TRACE_FLAG_DEL, - ], - ], $execute_time ); - } - $this->cache_calls++; $this->cache_time += $execute_time; @@ -1506,7 +1428,6 @@ public function delete_multiple( array $keys, $group = 'default' ) { */ protected function delete_multiple_at_once( array $keys, $group = 'default' ) { $start_time = microtime( true ); - $traceKV = []; if ( $this->is_ignored_group( $group ) ) { $results = []; @@ -1516,34 +1437,20 @@ protected function delete_multiple_at_once( array $keys, $group = 'default' ) { $results[ $key ] = isset( $this->cache[ $derived_key ] ); - $traceKV[ $key ] = [ - 'value' => $this->cache[ $derived_key ], - 'status' => self::TRACE_FLAG_DEL, - ]; - unset( $this->cache[ $derived_key ] ); } $execute_time = microtime( true ) - $start_time; - if ( $this->trace_enabled ) { - $this->trace_command( 'set', $group, $traceKV, $execute_time ); - } - return $results; } try { $tx = $this->redis->pipeline(); - foreach ($keys as $key) { + foreach ( $keys as $key ) { $derived_key = $this->build_key( (string) $key, $group ); - $traceKV[ $key ] = [ - 'value' => $this->cache[ $derived_key ], - 'status' => self::TRACE_FLAG_DEL, - ]; - $tx->del( $derived_key ); unset( $this->cache[ $derived_key ] ); @@ -1557,10 +1464,6 @@ protected function delete_multiple_at_once( array $keys, $group = 'default' ) { $execute_time = microtime( true ) - $start_time; - if ( $this->trace_enabled ) { - $this->trace_command( 'set', $group, $traceKV, $execute_time ); - } - return array_combine( $keys, $results ); } catch ( Exception $exception ) { $this->handle_exception( $exception ); @@ -1822,12 +1725,6 @@ function ( $group ) { * @return bool|mixed Cached object value. */ public function get( $key, $group = 'default', $force = false, &$found = null ) { - $trace_flags = self::TRACE_FLAG_READ; - - if ( $force ) { - $trace_flags |= self::TRACE_FLAG_REFRESH; - } - $start_time = microtime( true ); $san_key = $this->sanitize_key_part( $key ); @@ -1839,29 +1736,11 @@ public function get( $key, $group = 'default', $force = false, &$found = null ) $this->cache_hits++; $value = $this->get_from_internal_cache( $derived_key ); - if ( $this->trace_enabled ) { - $this->trace_command( 'get', $group, [ - $key => [ - 'value' => $value, - 'status' => $trace_flags | self::TRACE_FLAG_HIT | self::TRACE_FLAG_INTERNAL, - ], - ], microtime( true ) - $start_time); - } - return $value; } elseif ( $this->is_ignored_group( $san_group ) || ! $this->redis_status() ) { $found = false; $this->cache_misses++; - if ( $this->trace_enabled ) { - $this->trace_command( 'get', $group, [ - $key => [ - 'value' => null, - 'status' => $trace_flags | self::TRACE_FLAG_INTERNAL, - ], - ], microtime( true ) - $start_time ); - } - return false; } @@ -1883,15 +1762,6 @@ public function get( $key, $group = 'default', $force = false, &$found = null ) $found = false; $this->cache_misses++; - if ( $this->trace_enabled ) { - $this->trace_command( 'get', $group, [ - $key => [ - 'value' => null, - 'status' => $trace_flags, - ], - ], microtime( true ) - $start_time ); - } - return false; } else { $found = true; @@ -1901,15 +1771,6 @@ public function get( $key, $group = 'default', $force = false, &$found = null ) $this->add_to_internal_cache( $derived_key, $value ); - if ( $this->trace_enabled ) { - $this->trace_command( 'get', $group, [ - $key => [ - 'value' => $value, - 'status' => $trace_flags | self::TRACE_FLAG_HIT, - ], - ], microtime( true ) - $start_time ); - } - if ( function_exists( 'do_action' ) ) { /** * Fires on every cache get request @@ -1958,12 +1819,6 @@ public function get_multiple( $keys, $group = 'default', $force = false ) { return false; } - $trace_flags = self::TRACE_FLAG_READ; - - if ( $force ) { - $trace_flags |= self::TRACE_FLAG_REFRESH; - } - $cache = []; $derived_keys = []; $start_time = microtime( true ); @@ -1976,40 +1831,20 @@ public function get_multiple( $keys, $group = 'default', $force = false ) { } if ( $this->is_ignored_group( $san_group ) || ! $this->redis_status() ) { - $traceKV = []; - foreach ( $keys as $key ) { $value = $this->get_from_internal_cache( $derived_keys[ $key ] ); $cache[ $key ] = $value; if ($value === false) { $this->cache_misses++; - - if ( $this->trace_enabled ) { - $traceKV[ $key ] = [ - 'value' => null, - 'status' => $trace_flags | self::TRACE_FLAG_INTERNAL, - ]; - } } else { $this->cache_hits++; - - if ( $this->trace_enabled ) { - $traceKV[ $key ] = [ - 'value' => $value, - 'status' => $trace_flags | self::TRACE_FLAG_HIT | self::TRACE_FLAG_INTERNAL, - ]; - } } } - $this->trace_command( 'mget', $group, $traceKV, microtime( true ) - $start_time ); - return $cache; } - $traceKV = []; - if ( ! $force ) { foreach ( $keys as $key ) { $value = $this->get_from_internal_cache( $derived_keys[ $key ] ); @@ -2017,22 +1852,9 @@ public function get_multiple( $keys, $group = 'default', $force = false ) { if ( $value === false ) { $this->cache_misses++; - if ( $this->trace_enabled ) { - $traceKV[ $key ] = [ - 'value' => null, - 'status' => $trace_flags | self::TRACE_FLAG_INTERNAL, - ]; - } } else { $cache[ $key ] = $value; $this->cache_hits++; - - if ( $this->trace_enabled ) { - $traceKV[ $key ] = [ - 'value' => $value, - 'status' => $trace_flags | self::TRACE_FLAG_HIT | self::TRACE_FLAG_INTERNAL, - ]; - } } } } @@ -2045,9 +1867,6 @@ function ( $key ) use ( $cache ) { ); if ( empty( $remaining_keys ) ) { - $this->trace_enabled - && $this->trace_command( 'mget', $group, $traceKV, microtime( true ) - $start_time ); - return $cache; } @@ -2081,30 +1900,13 @@ function ( $key ) use ( $derived_keys ) { if ( $value === null || $value === false ) { $cache[ $key ] = false; $this->cache_misses++; - - if ( $this->trace_enabled ) { - $traceKV[ $key ] = [ - 'value' => null, - 'status' => $trace_flags, - ]; - } } else { $cache[ $key ] = $this->maybe_unserialize( $value ); $this->add_to_internal_cache( $derived_keys[ $key ], $cache[ $key ] ); $this->cache_hits++; - - if ( $this->trace_enabled ) { - $traceKV[ $key ] = [ - 'value' => $value, - 'status' => $trace_flags | self::TRACE_FLAG_HIT, - ]; - } } } - $this->trace_enabled - && $this->trace_command( 'mget', $group, $traceKV, $execute_time ); - if ( function_exists( 'do_action' ) ) { /** * Fires on every cache get multiple request @@ -2190,15 +1992,6 @@ public function set( $key, $value, $group = 'default', $expiration = 0 ) { $execute_time = microtime( true ) - $start_time; $this->cache_calls++; $this->cache_time += $execute_time; - - if ( $this->trace_enabled ) { - $this->trace_command( 'set', $group, [ - $key => [ - 'value' => null, - 'status' => self::TRACE_FLAG_WRITE, - ], - ], $execute_time ); - } } // If the set was successful, or we didn't go to redis. @@ -2269,7 +2062,6 @@ protected function set_multiple_at_once( array $data, $group = 'default', $expir $expiration = $this->validate_expiration( $expiration ); $tx = $this->redis->pipeline(); $keys = array_keys( $data ); - $traceKV = []; foreach ( $data as $key => $value ) { $key = $this->sanitize_key_part( $key ); @@ -2295,11 +2087,6 @@ protected function set_multiple_at_once( array $data, $group = 'default', $expir } } - $traceKV[ $key ] = [ - 'value' => $value, - 'status' => self::TRACE_FLAG_WRITE, - ]; - $tx->set( ...$args ); } @@ -2320,12 +2107,9 @@ protected function set_multiple_at_once( array $data, $group = 'default', $expir } $execute_time = microtime( true ) - $start_time; + $this->cache_calls++; $this->cache_time += $execute_time; - - if ( $this->trace_enabled ) { - $this->trace_command( 'set', $group, $traceKV, $execute_time ); - } } foreach ($data as $key => $value) { @@ -2356,7 +2140,6 @@ public function increment( $key, $offset = 1, $group = 'default' ) { $san_group = $this->sanitize_key_part( $group ); $derived_key = $this->fast_build_key( $san_key, $san_group ); - $trace_flags = self::TRACE_FLAG_READ | self::TRACE_FLAG_WRITE; // If group is a non-Redis group, save to internal cache, not Redis. if ( $this->is_ignored_group( $san_group ) || ! $this->redis_status() ) { @@ -2364,15 +2147,6 @@ public function increment( $key, $offset = 1, $group = 'default' ) { $value += $offset; $this->add_to_internal_cache( $derived_key, $value ); - if ( $this->trace_enabled ) { - $this->trace_command( 'incr', $group, [ - $key => [ - 'value' => $value, - 'status' => $trace_flags | self::TRACE_FLAG_INTERNAL, - ], - ], microtime( true ) - $start_time ); - } - return $value; } @@ -2388,15 +2162,6 @@ public function increment( $key, $offset = 1, $group = 'default' ) { $execute_time = microtime( true ) - $start_time; - if ( $this->trace_enabled ) { - $this->trace_command( 'incr', $group, [ - $key => [ - 'value' => $result, - 'status' => $trace_flags, - ], - ], $execute_time ); - } - $this->cache_calls += 2; $this->cache_time += $execute_time; @@ -2432,7 +2197,6 @@ public function decrement( $key, $offset = 1, $group = 'default' ) { $san_group = $this->sanitize_key_part( $group ); $derived_key = $this->fast_build_key( $san_key, $san_group ); - $trace_flags = self::TRACE_FLAG_READ | self::TRACE_FLAG_WRITE; // If group is a non-Redis group, save to internal cache, not Redis. if ( $this->is_ignored_group( $san_group ) || ! $this->redis_status() ) { @@ -2440,21 +2204,10 @@ public function decrement( $key, $offset = 1, $group = 'default' ) { $value -= $offset; $this->add_to_internal_cache( $derived_key, $value ); - if ( $this->trace_enabled ) { - $this->trace_command( 'decr', $group, [ - $key => [ - 'value' => $value, - 'status' => $trace_flags | self::TRACE_FLAG_INTERNAL, - ], - ], microtime( true ) - $start_time ); - } - return $value; } - try { - // 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 ) ); @@ -2466,15 +2219,6 @@ public function decrement( $key, $offset = 1, $group = 'default' ) { $execute_time = microtime( true ) - $start_time; - if ( $this->trace_enabled ) { - $this->trace_command( 'decr', $group, [ - $key => [ - 'value' => $result, - 'status' => $trace_flags, - ], - ], $execute_time ); - } - $this->cache_calls += 2; $this->cache_time += $execute_time; @@ -2921,40 +2665,6 @@ protected function is_serialized( $data, $strict = true ) { return false; } - /** - * Invoke the `redis_object_cache_trace` hook. - * - * @param string $command - * @param string $group - * @param array[string]array $keyValues - * @param float $duration - * @return void - */ - private function trace_command ( $command, $group, $keyValues, $duration ) { - if ( ! $this->trace_enabled || ! function_exists( 'do_action' ) ) { - return; - } - - /** - * Fires on every cache call. - * - * This hook is called on every cache request. - * It reports statistics per key involved. @see WP_Object_Cache::TRACE_FLAG_READ and friends. - * - * @param string $command The command that was executed. - * @param string $group Key group. - * @param array[string]array $keyValues Maps keys to the returned values (if any) and the resulting status. - * $keyValues = [ - * "foo" => ["value" => "bar", "status" => TRACE_FLAG_READ | TRACE_FLAG_HIT], // hit on redis (implies internal miss) - * "baz" => ["value" => "quo", "status" => TRACE_FLAG_READ | TRACE_FLAG_HIT | TRACE_FLAG_INTERNAL], // hit on internal cache - * "eta" => ["value" => null, "status" => TRACE_FLAG_READ], // miss - * ]; - * @param float $duration Duration of the request in microseconds. - * @return void - */ - do_action( 'redis_object_cache_trace', $command, $group, $keyValues, $duration ); - } - /** * Handle the redis failure gracefully or throw an exception. * From fbe535007c0a8fbb9283f7dfb1ee3e2b59c066e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Till=20Kru=CC=88ss?= Date: Mon, 18 Jul 2022 18:18:24 -0700 Subject: [PATCH 02/10] document filters --- includes/object-cache.php | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/includes/object-cache.php b/includes/object-cache.php index d2e6a984..d8afbc40 100644 --- a/includes/object-cache.php +++ b/includes/object-cache.php @@ -1167,7 +1167,14 @@ protected function add_multiple_at_once( array $data, $group = 'default', $expir $tx = $this->redis->pipeline(); foreach ( $data as $key => $value ) { - /** This action is documented in includes/object-cache.php */ + /** + * Filters the cache expiration time + * + * @param int $expiration The time in seconds the entry expires. 0 for no expiry. + * @param string $key The cache key. + * @param string $group The cache group. + * @param mixed $orig_exp The original expiration value before validation. + */ $expire = apply_filters( 'redis_cache_expiration', $expire, $key, $group, $orig_exp ); $key = $this->sanitize_key_part( $key ); @@ -2067,7 +2074,14 @@ protected function set_multiple_at_once( array $data, $group = 'default', $expir $key = $this->sanitize_key_part( $key ); $derived_key = $this->fast_build_key( $key, $group ); - /** This action is documented in includes/object-cache.php */ + /** + * Filters the cache expiration time + * + * @param int $expiration The time in seconds the entry expires. 0 for no expiry. + * @param string $key The cache key. + * @param string $group The cache group. + * @param mixed $orig_exp The original expiration value before validation. + */ $expiration = apply_filters( 'redis_cache_expiration', $expiration, $key, $group, $orig_exp ); $args = [ $derived_key, $this->maybe_serialize( $value ) ]; From 095aeda059a8e053801f03d9974356999a47ccef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Till=20Kru=CC=88ss?= Date: Mon, 18 Jul 2022 18:18:45 -0700 Subject: [PATCH 03/10] remove lines --- includes/object-cache.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/includes/object-cache.php b/includes/object-cache.php index d8afbc40..8492c9fd 100644 --- a/includes/object-cache.php +++ b/includes/object-cache.php @@ -1751,7 +1751,6 @@ public function get( $key, $group = 'default', $force = false, &$found = null ) return false; } - try { $result = $this->redis->get( $derived_key ); } catch ( Exception $exception ) { @@ -2350,8 +2349,6 @@ public function fast_build_key( $key, $group = 'default' ) { $salt = defined( 'WP_REDIS_PREFIX' ) ? trim( WP_REDIS_PREFIX ) : ''; $prefix = $this->is_global_group( $group ) ? $this->global_prefix : $this->blog_prefix; - - $prefix = trim( $prefix, '_-:$' ); return "{$salt}{$prefix}:{$group}:{$key}"; From 60ace6beaf3c80a3bf52aee65a7b4857022b67a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Till=20Kru=CC=88ss?= Date: Mon, 18 Jul 2022 18:19:00 -0700 Subject: [PATCH 04/10] improve proptery descriptions --- includes/object-cache.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/includes/object-cache.php b/includes/object-cache.php index 8492c9fd..a8058f30 100644 --- a/includes/object-cache.php +++ b/includes/object-cache.php @@ -447,18 +447,18 @@ class WP_Object_Cache { public $cache_misses = 0; /** - * Track how long request took. + * The amount of Redis commands made. * - * @var float + * @var int */ - public $cache_time = 0; + public $cache_calls = 0; /** - * Track how may calls were made. + * The amount of microseconds (μs) waited for Redis commands. * - * @var int + * @var float */ - public $cache_calls = 0; + public $cache_time = 0; /** * Instantiate the Redis class. From 6c08a2e2b2e54219f134a4a9b1ebd64062ae2ea3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Till=20Kru=CC=88ss?= Date: Mon, 18 Jul 2022 18:31:41 -0700 Subject: [PATCH 05/10] check raw group name --- CHANGELOG.md | 1 + includes/object-cache.php | 10 +++++----- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 39c32688..1ec4461a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## 2.1.2 - Removed tracing +- Check if raw group name is ignored, not sanitized name ## 2.1.1 diff --git a/includes/object-cache.php b/includes/object-cache.php index a8058f30..a899ecdb 100644 --- a/includes/object-cache.php +++ b/includes/object-cache.php @@ -1744,7 +1744,7 @@ public function get( $key, $group = 'default', $force = false, &$found = null ) $value = $this->get_from_internal_cache( $derived_key ); return $value; - } elseif ( $this->is_ignored_group( $san_group ) || ! $this->redis_status() ) { + } elseif ( $this->is_ignored_group( $group ) || ! $this->redis_status() ) { $found = false; $this->cache_misses++; @@ -1836,7 +1836,7 @@ public function get_multiple( $keys, $group = 'default', $force = false ) { $derived_keys[ $key ] = $this->fast_build_key( $san_key, $san_group ); } - if ( $this->is_ignored_group( $san_group ) || ! $this->redis_status() ) { + if ( $this->is_ignored_group( $group ) || ! $this->redis_status() ) { foreach ( $keys as $key ) { $value = $this->get_from_internal_cache( $derived_keys[ $key ] ); $cache[ $key ] = $value; @@ -1968,7 +1968,7 @@ public function set( $key, $value, $group = 'default', $expiration = 0 ) { $derived_key = $this->fast_build_key( $san_key, $san_group ); // Save if group not excluded from redis and redis is up. - if ( ! $this->is_ignored_group( $san_group ) && $this->redis_status() ) { + if ( ! $this->is_ignored_group( $group ) && $this->redis_status() ) { $orig_exp = $expiration; $expiration = $this->validate_expiration( $expiration ); @@ -2155,7 +2155,7 @@ public function increment( $key, $offset = 1, $group = 'default' ) { $derived_key = $this->fast_build_key( $san_key, $san_group ); // If group is a non-Redis group, save to internal cache, not Redis. - if ( $this->is_ignored_group( $san_group ) || ! $this->redis_status() ) { + if ( $this->is_ignored_group( $group ) || ! $this->redis_status() ) { $value = $this->get_from_internal_cache( $derived_key ); $value += $offset; $this->add_to_internal_cache( $derived_key, $value ); @@ -2212,7 +2212,7 @@ public function decrement( $key, $offset = 1, $group = 'default' ) { $derived_key = $this->fast_build_key( $san_key, $san_group ); // If group is a non-Redis group, save to internal cache, not Redis. - if ( $this->is_ignored_group( $san_group ) || ! $this->redis_status() ) { + if ( $this->is_ignored_group( $group ) || ! $this->redis_status() ) { $value = $this->get_from_internal_cache( $derived_key ); $value -= $offset; $this->add_to_internal_cache( $derived_key, $value ); From 2e35dd04e0c226334e735ea6a0bd8cef3db0f3e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Till=20Kru=CC=88ss?= Date: Mon, 18 Jul 2022 18:33:28 -0700 Subject: [PATCH 06/10] add missing `redis_object_cache_delete` call --- CHANGELOG.md | 1 + includes/object-cache.php | 40 ++++++++++++++++++++------------------- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ec4461a..62bb029a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Removed tracing - Check if raw group name is ignored, not sanitized name +- Call `redis_object_cache_delete` action in `wp_cache_delete_multiple()` ## 2.1.1 diff --git a/includes/object-cache.php b/includes/object-cache.php index a899ecdb..5acb74be 100644 --- a/includes/object-cache.php +++ b/includes/object-cache.php @@ -1412,7 +1412,11 @@ public function delete( $key, $group = 'default' ) { * true on success, or false if the contents were not deleted. */ public function delete_multiple( array $keys, $group = 'default' ) { - if ( $this->redis_status() && method_exists( $this->redis, 'pipeline' ) ) { + if ( + $this->redis_status() && + method_exists( $this->redis, 'pipeline' ) && + ! $this->is_ignored_group( $group ) + ) { return $this->delete_multiple_at_once( $keys, $group ); } @@ -1436,22 +1440,6 @@ public function delete_multiple( array $keys, $group = 'default' ) { protected function delete_multiple_at_once( array $keys, $group = 'default' ) { $start_time = microtime( true ); - if ( $this->is_ignored_group( $group ) ) { - $results = []; - - foreach ( $keys as $key ) { - $derived_key = $this->build_key( $key, $group ); - - $results[ $key ] = isset( $this->cache[ $derived_key ] ); - - unset( $this->cache[ $derived_key ] ); - } - - $execute_time = microtime( true ) - $start_time; - - return $results; - } - try { $tx = $this->redis->pipeline(); @@ -1470,13 +1458,27 @@ protected function delete_multiple_at_once( array $keys, $group = 'default' ) { }, $tx->{$method}() ); $execute_time = microtime( true ) - $start_time; - - return array_combine( $keys, $results ); } catch ( Exception $exception ) { $this->handle_exception( $exception ); return array_combine( $keys, array_fill( 0, count( $keys ), false ) ); } + + if ( function_exists( 'do_action' ) ) { + foreach ( $keys as $key ) { + /** + * Fires on every cache key deletion + * + * @since 1.3.3 + * @param string $key The cache key. + * @param string $group The group value appended to the $key. + * @param float $execute_time Execution time for the request in seconds. + */ + do_action( 'redis_object_cache_delete', $key, $group, $execute_time ); + } + } + + return array_combine( $keys, $results ); } /** From 96f2dae8617c99a0118ae0354daf41b572000008 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Till=20Kru=CC=88ss?= Date: Mon, 18 Jul 2022 19:46:11 -0700 Subject: [PATCH 07/10] Call `redis_object_cache_set` action in `wp_cache_set_multiple()` --- CHANGELOG.md | 1 + includes/object-cache.php | 121 +++++++++++++++++++------------------- 2 files changed, 61 insertions(+), 61 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 62bb029a..916f91a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Removed tracing - Check if raw group name is ignored, not sanitized name +- Call `redis_object_cache_set` action in `wp_cache_set_multiple()` - Call `redis_object_cache_delete` action in `wp_cache_delete_multiple()` ## 2.1.1 diff --git a/includes/object-cache.php b/includes/object-cache.php index 5acb74be..26856a73 100644 --- a/includes/object-cache.php +++ b/includes/object-cache.php @@ -2008,8 +2008,6 @@ public function set( $key, $value, $group = 'default', $expiration = 0 ) { } if ( function_exists( 'do_action' ) ) { - $execute_time = microtime( true ) - $start_time; - /** * Fires on every cache set * @@ -2036,7 +2034,11 @@ public function set( $key, $value, $group = 'default', $expiration = 0 ) { * @return bool[] Array of return values, grouped by key. Each value is always true. */ public function set_multiple( array $data, $group = 'default', $expire = 0 ) { - if ( $this->redis_status() && method_exists( $this->redis, 'pipeline' ) ) { + if ( + $this->redis_status() && + method_exists( $this->redis, 'pipeline' ) && + ! $this->is_ignored_group( $group ) + ) { return $this->set_multiple_at_once( $data, $group, $expire ); } @@ -2060,83 +2062,80 @@ public function set_multiple( array $data, $group = 'default', $expire = 0 ) { */ protected function set_multiple_at_once( array $data, $group = 'default', $expiration = 0 ) { - $values = []; - $group = $this->sanitize_key_part( $group ); - - if ( ! $this->is_ignored_group( $group ) ) { - $start_time = microtime( true ); + $start_time = microtime( true ); - $orig_exp = $expiration; - $expiration = $this->validate_expiration( $expiration ); - $tx = $this->redis->pipeline(); - $keys = array_keys( $data ); + $san_group = $this->sanitize_key_part( $group ); + $derived_keys = []; - foreach ( $data as $key => $value ) { - $key = $this->sanitize_key_part( $key ); - $derived_key = $this->fast_build_key( $key, $group ); + $orig_exp = $expiration; + $expiration = $this->validate_expiration( $expiration ); + $expirations = []; - /** - * Filters the cache expiration time - * - * @param int $expiration The time in seconds the entry expires. 0 for no expiry. - * @param string $key The cache key. - * @param string $group The cache group. - * @param mixed $orig_exp The original expiration value before validation. - */ - $expiration = apply_filters( 'redis_cache_expiration', $expiration, $key, $group, $orig_exp ); - - $args = [ $derived_key, $this->maybe_serialize( $value ) ]; + $tx = $this->redis->pipeline(); + $keys = array_keys( $data ); - if ( $this->redis instanceof Predis\Client ) { - $args[] = 'nx'; + foreach ( $data as $key => $value ) { + $san_key = $this->sanitize_key_part( $key ); + $derived_key = $derived_keys[ $key ] = $this->fast_build_key( $san_key, $san_group ); - if ( $expiration ) { - $args[] = 'ex'; - $args[] = $expiration; - } - } else { - if ( $expiration ) { - $args[] = [ 'nx', 'ex' => $expiration ]; - } else { - $args[] = [ 'nx' ]; - } - } + /** + * Filters the cache expiration time + * + * @param int $expiration The time in seconds the entry expires. 0 for no expiry. + * @param string $key The cache key. + * @param string $group The cache group. + * @param mixed $orig_exp The original expiration value before validation. + */ + $expiration = $expirations[ $key ] = apply_filters( 'redis_cache_expiration', $expiration, $key, $group, $orig_exp ); - $tx->set( ...$args ); + if ( $expiration ) { + $tx->setex( $derived_key, $expiration, $this->maybe_serialize( $value ) ); + } else { + $tx->set( $derived_key, $this->maybe_serialize( $value ) ); } + } - try { - $method = ( $this->redis instanceof Predis\Client ) ? 'execute' : 'exec'; + try { + $method = ( $this->redis instanceof Predis\Client ) ? 'execute' : 'exec'; - $values = array_map( function ( $response ) { - return (bool) $this->parse_redis_response( $response ); - }, $tx->{$method}() ); + $results = array_map( function ( $response ) { + return (bool) $this->parse_redis_response( $response ); + }, $tx->{$method}() ); - if ( $values ) { - $values = array_combine( $keys, $values ); - } - } catch ( Exception $exception ) { - $this->handle_exception( $exception ); + $results = array_combine( $keys, $results ); - return array_combine( $keys, array_fill( 0, count( $keys ), false ) ); + foreach ( $results as $key => $result ) { + if ( $result ) { + $this->add_to_internal_cache( $derived_keys[ $key ], $value ); + } } + } catch ( Exception $exception ) { + $this->handle_exception( $exception ); - $execute_time = microtime( true ) - $start_time; - - $this->cache_calls++; - $this->cache_time += $execute_time; + return array_combine( $keys, array_fill( 0, count( $keys ), false ) ); } - foreach ($data as $key => $value) { - $key = $this->sanitize_key_part( $key ); - $derived_key = $this->fast_build_key( $key, $group ); + $execute_time = microtime( true ) - $start_time; - if ( isset( $values[ $key ] ) && $values[ $key ] ) { - $this->add_to_internal_cache( $derived_key, $value ); + $this->cache_calls++; + $this->cache_time += $execute_time; + + if ( function_exists( 'do_action' ) ) { + foreach ( $data as $key => $value ) { + /** + * Fires on every cache set + * + * @param string $key The cache key. + * @param mixed $value Value of the cache entry. + * @param string $group The group value appended to the $key. + * @param int $expiration The time in seconds the entry expires. 0 for no expiry. + * @param float $execute_time Execution time for the request in seconds. + */ + do_action( 'redis_object_cache_set', $key, $value, $group, $expirations[ $key ], $execute_time ); } } - return $values; + return $results; } /** From 2d6df03ea6418045ad7a6bf3c7e248bf026743a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Till=20Kru=CC=88ss?= Date: Mon, 18 Jul 2022 20:15:02 -0700 Subject: [PATCH 08/10] restore line --- includes/object-cache.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/includes/object-cache.php b/includes/object-cache.php index 26856a73..4371db2f 100644 --- a/includes/object-cache.php +++ b/includes/object-cache.php @@ -2008,6 +2008,8 @@ public function set( $key, $value, $group = 'default', $expiration = 0 ) { } if ( function_exists( 'do_action' ) ) { + $execute_time = microtime( true ) - $start_time; + /** * Fires on every cache set * From 12a3257d65d3639287fbad29770e5bf83bd6e1f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Till=20Kru=CC=88ss?= Date: Mon, 18 Jul 2022 20:17:10 -0700 Subject: [PATCH 09/10] overhaul `add_multiple_at_once()` logic --- CHANGELOG.md | 1 + includes/object-cache.php | 109 +++++++++++++++++++------------------- 2 files changed, 55 insertions(+), 55 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 916f91a8..67f0166a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Removed tracing - Check if raw group name is ignored, not sanitized name +- Fixed and improved `wp_cache_*_multiple()` logic - Call `redis_object_cache_set` action in `wp_cache_set_multiple()` - Call `redis_object_cache_delete` action in `wp_cache_delete_multiple()` diff --git a/includes/object-cache.php b/includes/object-cache.php index 4371db2f..e9016bd4 100644 --- a/includes/object-cache.php +++ b/includes/object-cache.php @@ -1130,7 +1130,11 @@ public function add_multiple( array $data, $group = 'default', $expire = 0 ) { return array_combine( array_keys( $data ), array_fill( 0, count( $data ), false ) ); } - if ( $this->redis_status() && method_exists( $this->redis, 'pipeline' ) ) { + if ( + $this->redis_status() && + method_exists( $this->redis, 'pipeline' ) && + ! $this->is_ignored_group( $group ) + ) { return $this->add_multiple_at_once( $data, $group, $expire ); } @@ -1156,81 +1160,76 @@ public function add_multiple( array $data, $group = 'default', $expire = 0 ) { protected function add_multiple_at_once( array $data, $group = 'default', $expire = 0 ) { $keys = array_keys( $data ); - $values = array_combine( $keys, array_fill( 0, count( $keys ), true ) ); - $group = $this->sanitize_key_part( $group ); - if ( ! $this->is_ignored_group( $group ) ) { - $start_time = microtime( true ); + $san_group = $this->sanitize_key_part( $group ); - $orig_exp = $expire; - $expire = $this->validate_expiration( $expire ); - $tx = $this->redis->pipeline(); + $tx = $this->redis->pipeline(); - foreach ( $data as $key => $value ) { - /** - * Filters the cache expiration time - * - * @param int $expiration The time in seconds the entry expires. 0 for no expiry. - * @param string $key The cache key. - * @param string $group The cache group. - * @param mixed $orig_exp The original expiration value before validation. - */ - $expire = apply_filters( 'redis_cache_expiration', $expire, $key, $group, $orig_exp ); + $orig_exp = $expire; + $expire = $this->validate_expiration( $expire ); - $key = $this->sanitize_key_part( $key ); - $derived_key = $this->fast_build_key( $key, $group ); + foreach ( $data as $key => $value ) { + /** + * Filters the cache expiration time + * + * @param int $expiration The time in seconds the entry expires. 0 for no expiry. + * @param string $key The cache key. + * @param string $group The cache group. + * @param mixed $orig_exp The original expiration value before validation. + */ + $expire = apply_filters( 'redis_cache_expiration', $expire, $key, $group, $orig_exp ); - $values[ $key ] = $derived_key || ! isset( $this->cache[ $derived_key ] ); + $san_key = $this->sanitize_key_part( $key ); + $derived_key = $derived_keys[ $key ] = $this->fast_build_key( $san_key, $san_group ); - $args = [ $derived_key, $this->maybe_serialize( $value ) ]; + $args = [ $derived_key, $this->maybe_serialize( $value ) ]; - if ( $this->redis instanceof Predis\Client ) { - $args[] = 'nx'; + if ( $this->redis instanceof Predis\Client ) { + $args[] = 'nx'; - if ( $expire ) { - $args[] = 'ex'; - $args[] = $expire; - } + if ( $expire ) { + $args[] = 'ex'; + $args[] = $expire; + } + } else { + if ( $expire ) { + $args[] = [ 'nx', 'ex' => $expire ]; } else { - if ( $expire ) { - $args[] = [ 'nx', 'ex' => $expire ]; - } else { - $args[] = [ 'nx' ]; - } + $args[] = [ 'nx' ]; } - - $tx->set( ...$args ); } - try { - $method = ( $this->redis instanceof Predis\Client ) ? 'execute' : 'exec'; + $tx->set( ...$args ); + } - $values = array_map( function ( $response ) { - return (bool) $this->parse_redis_response( $response ); - }, $tx->{$method}() ); + try { + $start_time = microtime( true ); - if ( $values ) { - $values = array_combine( $keys, $values ); - } + $method = ( $this->redis instanceof Predis\Client ) ? 'execute' : 'exec'; - $execute_time = microtime( true ) - $start_time; + $results = array_map( function ( $response ) { + return (bool) $this->parse_redis_response( $response ); + }, $tx->{$method}() ); - $this->cache_calls++; - $this->cache_time += $execute_time; - } catch ( Exception $exception ) { - $this->handle_exception( $exception ); + $results = array_combine( $keys, $results ); - return array_combine( $keys, array_fill( 0, count( $keys ), false ) ); + foreach ( $results as $key => $result ) { + if ( $result ) { + $this->add_to_internal_cache( $derived_keys[ $key ], $value ); + } } - } - foreach ( $values as $key => $value ) { - if ( $value ) { - $this->add_to_internal_cache( $this->fast_build_key( $key, $group ) , $value ); - } + $execute_time = microtime( true ) - $start_time; + + $this->cache_calls++; + $this->cache_time += $execute_time; + } catch ( Exception $exception ) { + $this->handle_exception( $exception ); + + return array_combine( $keys, array_fill( 0, count( $keys ), false ) ); } - return $values; + return $results; } /** From aad13ba7d7a4d9a3bdd1dafc09e097a75ba5392d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Till=20Kru=CC=88ss?= Date: Mon, 18 Jul 2022 20:20:52 -0700 Subject: [PATCH 10/10] tag v2.1.2 --- CHANGELOG.md | 4 ++-- includes/object-cache.php | 2 +- languages/redis-cache.pot | 4 ++-- readme.txt | 12 ++++++++++-- redis-cache.php | 2 +- 5 files changed, 16 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 67f0166a..92b92589 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,11 +2,11 @@ ## 2.1.2 -- Removed tracing -- Check if raw group name is ignored, not sanitized name - Fixed and improved `wp_cache_*_multiple()` logic - Call `redis_object_cache_set` action in `wp_cache_set_multiple()` - Call `redis_object_cache_delete` action in `wp_cache_delete_multiple()` +- Check if raw group name is ignored, not sanitized name +- Removed tracing ## 2.1.1 diff --git a/includes/object-cache.php b/includes/object-cache.php index e9016bd4..ad8507aa 100644 --- a/includes/object-cache.php +++ b/includes/object-cache.php @@ -3,7 +3,7 @@ * Plugin Name: Redis Object Cache Drop-In * Plugin URI: https://wordpress.org/plugins/redis-cache/ * Description: A persistent object cache backend powered by Redis. Supports Predis, PhpRedis, Relay, replication, sentinels, clustering and WP-CLI. - * Version: 2.1.1 + * Version: 2.1.2 * Author: Till Krüss * Author URI: https://objectcache.pro * License: GPLv3 diff --git a/languages/redis-cache.pot b/languages/redis-cache.pot index cf3def5d..01337730 100644 --- a/languages/redis-cache.pot +++ b/languages/redis-cache.pot @@ -2,14 +2,14 @@ # This file is distributed under the GPLv3. msgid "" msgstr "" -"Project-Id-Version: Redis Object Cache 2.1.1\n" +"Project-Id-Version: Redis Object Cache 2.1.2\n" "Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/redis-cache\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" -"POT-Creation-Date: 2022-07-18T16:28:07+00:00\n" +"POT-Creation-Date: 2022-07-19T03:20:42+00:00\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "X-Generator: WP-CLI 2.6.0\n" "X-Domain: redis-cache\n" diff --git a/readme.txt b/readme.txt index 8f4da93b..87df2d0c 100644 --- a/readme.txt +++ b/readme.txt @@ -5,7 +5,7 @@ Tags: redis, predis, phpredis, credis, relay, caching, cache, object cache, perf Requires at least: 3.3 Tested up to: 6.0 Requires PHP: 7.2 -Stable tag: 2.1.1 +Stable tag: 2.1.2 License: GPLv3 License URI: http://www.gnu.org/licenses/gpl-3.0.html @@ -83,6 +83,14 @@ To see a list of all available WP-CLI commands, please see the [WP CLI commands == Changelog == += 2.1.2 = + +- Fixed and improved `wp_cache_*_multiple()` logic +- Call `redis_object_cache_set` action in `wp_cache_set_multiple()` +- Call `redis_object_cache_delete` action in `wp_cache_delete_multiple()` +- Check if raw group name is ignored, not sanitized name +- Removed tracing + = 2.1.1 = - Bumped PHP requirement to 7.2 @@ -572,6 +580,6 @@ Since Predis isn't maintained any longer, it's highly recommended to switch over == Upgrade Notice == -= 2.1.1 = += 2.1.2 = Bumped PHP requirement to 7.2, updated Predis to v2.0 and deprecated Credis and HHVM clients. diff --git a/redis-cache.php b/redis-cache.php index 657e8204..c0336275 100644 --- a/redis-cache.php +++ b/redis-cache.php @@ -3,7 +3,7 @@ * Plugin Name: Redis Object Cache * Plugin URI: https://wordpress.org/plugins/redis-cache/ * Description: A persistent object cache backend powered by Redis. Supports Predis, PhpRedis, Relay, replication, sentinels, clustering and WP-CLI. - * Version: 2.1.1 + * Version: 2.1.2 * Text Domain: redis-cache * Domain Path: /languages * Network: true