From 9ceaba4013f408aa74006e0308ff50eeab00aa6e Mon Sep 17 00:00:00 2001 From: Debarghya Banerjee Date: Wed, 25 Sep 2024 23:55:56 +0530 Subject: [PATCH 1/5] Add wp_cache_replace_multiple function --- src/wp-includes/cache.php | 51 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/src/wp-includes/cache.php b/src/wp-includes/cache.php index e7fe7b87a85cd..6798fded5d045 100644 --- a/src/wp-includes/cache.php +++ b/src/wp-includes/cache.php @@ -87,6 +87,57 @@ function wp_cache_replace( $key, $data, $group = '', $expire = 0 ) { return $wp_object_cache->replace( $key, $data, $group, (int) $expire ); } +/** + * Replace multiple cache values only if they already exist. + * + * @param array $data Associative array of cache keys and their new values. + * @param string $group Optional. The cache group. Defaults to 'default'. + * @return array List of keys that were successfully replaced. + */ +function wp_cache_replace_multiple( $key_value_pairs, $group = 'default' ) { + global $wp_object_cache; + + $keys = array_keys( $key_value_pairs ); + $existing_keys = array(); + $missing_keys = array(); + $successfully_replaced = array(); + + // Check existence of keys using _exists() and separate them. + foreach ( $keys as $key ) { + if ( $wp_object_cache->_exists( $key, $group ) ) { + $existing_keys[] = $key; // Track keys that exist. + } else { + $missing_keys[] = $key; // Track keys that are missing. + } + } + + // Retrieve values for the missing keys. + $existing_values = $wp_object_cache->get_multiple( $missing_keys, $group ); + + // Merge existing keys with those retrieved from get_multiple. + $merged_keys = array_merge( $existing_keys, array_keys( array_filter( $existing_values ) ) ); + + // Prepare new values for replacement. + $values_to_set = array(); + + foreach ( $merged_keys as $key ) { + if ( isset( $existing_values[ $key ] ) || in_array( $key, $existing_keys, true ) ) { + // Only replace if it exists. + $values_to_set[ $key ] = $key_value_pairs[ $key ]; + } + } + + // Set new values in the cache and track successfully replaced keys. + if ( ! empty( $values_to_set ) ) { + $results = $wp_object_cache->set_multiple( $values_to_set, $group ); + + // Track successfully replaced keys using array_keys. + $successfully_replaced = array_keys( array_filter( $results ) ); + } + + return $successfully_replaced; // Return list of keys that were successfully replaced. +} + /** * Saves the data to the cache. * From dc25a846b38f2bb15c10153ff597e96354322da7 Mon Sep 17 00:00:00 2001 From: Debarghya Banerjee Date: Thu, 26 Sep 2024 02:25:29 +0530 Subject: [PATCH 2/5] Add empty check --- src/wp-includes/cache.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/wp-includes/cache.php b/src/wp-includes/cache.php index 6798fded5d045..6c6df7fbe53f6 100644 --- a/src/wp-includes/cache.php +++ b/src/wp-includes/cache.php @@ -97,6 +97,10 @@ function wp_cache_replace( $key, $data, $group = '', $expire = 0 ) { function wp_cache_replace_multiple( $key_value_pairs, $group = 'default' ) { global $wp_object_cache; + if ( empty( $group ) ) { + $group = 'default'; + } + $keys = array_keys( $key_value_pairs ); $existing_keys = array(); $missing_keys = array(); From 55064350b14e58749cb0ab646ab4c57dea9d2a4e Mon Sep 17 00:00:00 2001 From: Debarghya Banerjee Date: Thu, 26 Sep 2024 02:48:35 +0530 Subject: [PATCH 3/5] Add expiry and fix unit tests --- src/wp-includes/cache.php | 11 +++++++---- tests/phpunit/tests/pluggable/signatures.php | 5 +++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/cache.php b/src/wp-includes/cache.php index 6c6df7fbe53f6..946d4d6947836 100644 --- a/src/wp-includes/cache.php +++ b/src/wp-includes/cache.php @@ -90,11 +90,14 @@ function wp_cache_replace( $key, $data, $group = '', $expire = 0 ) { /** * Replace multiple cache values only if they already exist. * - * @param array $data Associative array of cache keys and their new values. - * @param string $group Optional. The cache group. Defaults to 'default'. + * @param array $data Associative array of cache keys and their new values. + * @param string $group Optional. The cache group. Defaults to 'default'. + * @param int $expire Optional. When to expire the cache contents, in seconds. + * Default 0 (no expiration). + * * @return array List of keys that were successfully replaced. */ -function wp_cache_replace_multiple( $key_value_pairs, $group = 'default' ) { +function wp_cache_replace_multiple( $key_value_pairs, $group = 'default', $expire = 0 ) { global $wp_object_cache; if ( empty( $group ) ) { @@ -133,7 +136,7 @@ function wp_cache_replace_multiple( $key_value_pairs, $group = 'default' ) { // Set new values in the cache and track successfully replaced keys. if ( ! empty( $values_to_set ) ) { - $results = $wp_object_cache->set_multiple( $values_to_set, $group ); + $results = $wp_object_cache->set_multiple( $values_to_set, $group, (int) $expire ); // Track successfully replaced keys using array_keys. $successfully_replaced = array_keys( array_filter( $results ) ); diff --git a/tests/phpunit/tests/pluggable/signatures.php b/tests/phpunit/tests/pluggable/signatures.php index 81fd079621916..e1c00b706b38e 100644 --- a/tests/phpunit/tests/pluggable/signatures.php +++ b/tests/phpunit/tests/pluggable/signatures.php @@ -282,6 +282,11 @@ public function get_pluggable_function_signatures() { 'group' => '', 'expire' => 0, ), + 'wp_cache_replace_multiple' => array( + 'data', + 'group' => 'default', + 'expire' => 0, + ), 'wp_cache_set' => array( 'key', 'data', From 58c8541cdbc24fc2c0a672b0386839abbd661aa1 Mon Sep 17 00:00:00 2001 From: Debarghya Banerjee Date: Thu, 26 Sep 2024 03:03:05 +0530 Subject: [PATCH 4/5] Replace key_value_pairs with data --- src/wp-includes/cache.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/cache.php b/src/wp-includes/cache.php index 946d4d6947836..a049334112888 100644 --- a/src/wp-includes/cache.php +++ b/src/wp-includes/cache.php @@ -97,14 +97,14 @@ function wp_cache_replace( $key, $data, $group = '', $expire = 0 ) { * * @return array List of keys that were successfully replaced. */ -function wp_cache_replace_multiple( $key_value_pairs, $group = 'default', $expire = 0 ) { +function wp_cache_replace_multiple( $data, $group = 'default', $expire = 0 ) { global $wp_object_cache; if ( empty( $group ) ) { $group = 'default'; } - $keys = array_keys( $key_value_pairs ); + $keys = array_keys( $data ); $existing_keys = array(); $missing_keys = array(); $successfully_replaced = array(); @@ -130,7 +130,7 @@ function wp_cache_replace_multiple( $key_value_pairs, $group = 'default', $expir foreach ( $merged_keys as $key ) { if ( isset( $existing_values[ $key ] ) || in_array( $key, $existing_keys, true ) ) { // Only replace if it exists. - $values_to_set[ $key ] = $key_value_pairs[ $key ]; + $values_to_set[ $key ] = $data[ $key ]; } } From 54132d52aad3647d5838d341965adcedee931b6e Mon Sep 17 00:00:00 2001 From: Debarghya Banerjee Date: Fri, 1 Nov 2024 18:59:50 +0530 Subject: [PATCH 5/5] Fix the doc --- src/wp-includes/cache.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/cache.php b/src/wp-includes/cache.php index a049334112888..d532f47538d10 100644 --- a/src/wp-includes/cache.php +++ b/src/wp-includes/cache.php @@ -88,7 +88,7 @@ function wp_cache_replace( $key, $data, $group = '', $expire = 0 ) { } /** - * Replace multiple cache values only if they already exist. + * Replaces multiple cache values only if they already exist. * * @param array $data Associative array of cache keys and their new values. * @param string $group Optional. The cache group. Defaults to 'default'.