Skip to content

Commit e440510

Browse files
committed
Replace direct cache operations with query-specific functions
Introduced `wp_cache_get_query_data` and `wp_cache_set_query_data` to handle cached query data with freshness validation. Refactored various classes, functions, and queries to use these standardized methods for improved readability and maintainability while ensuring cache consistency.
1 parent 97ed384 commit e440510

13 files changed

+187
-101
lines changed

src/wp-includes/class-wp-comment-query.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -451,8 +451,8 @@ public function get_comments() {
451451
$key = md5( serialize( $_args ) );
452452
$last_changed = wp_cache_get_last_changed( 'comment' );
453453

454-
$cache_key = "get_comments:$key:$last_changed";
455-
$cache_value = wp_cache_get( $cache_key, 'comment-queries' );
454+
$cache_key = "get_comments:$key";
455+
$cache_value = wp_cache_get_query_data( $cache_key, 'comment-queries', $last_changed );
456456
if ( false === $cache_value ) {
457457
$comment_ids = $this->get_comment_ids();
458458
if ( $comment_ids ) {
@@ -463,7 +463,7 @@ public function get_comments() {
463463
'comment_ids' => $comment_ids,
464464
'found_comments' => $this->found_comments,
465465
);
466-
wp_cache_add( $cache_key, $cache_value, 'comment-queries' );
466+
wp_cache_set_query_data( $cache_key, $cache_value, 'comment-queries', $last_changed );
467467
} else {
468468
$comment_ids = $cache_value['comment_ids'];
469469
$this->found_comments = $cache_value['found_comments'];
@@ -1046,9 +1046,9 @@ protected function fill_descendants( $comments ) {
10461046
if ( $_parent_ids ) {
10471047
$cache_keys = array();
10481048
foreach ( $_parent_ids as $parent_id ) {
1049-
$cache_keys[ $parent_id ] = "get_comment_child_ids:$parent_id:$key:$last_changed";
1049+
$cache_keys[ $parent_id ] = "get_comment_child_ids:$parent_id:$key";
10501050
}
1051-
$cache_data = wp_cache_get_multiple( array_values( $cache_keys ), 'comment-queries' );
1051+
$cache_data = wp_cache_get_multiple_query_data( array_values( $cache_keys ), 'comment-queries', $last_changed );
10521052
foreach ( $_parent_ids as $parent_id ) {
10531053
$parent_child_ids = $cache_data[ $cache_keys[ $parent_id ] ];
10541054
if ( false !== $parent_child_ids ) {
@@ -1082,10 +1082,10 @@ protected function fill_descendants( $comments ) {
10821082

10831083
$data = array();
10841084
foreach ( $parent_map as $parent_id => $children ) {
1085-
$cache_key = "get_comment_child_ids:$parent_id:$key:$last_changed";
1085+
$cache_key = "get_comment_child_ids:$parent_id:$key";
10861086
$data[ $cache_key ] = $children;
10871087
}
1088-
wp_cache_set_multiple( $data, 'comment-queries' );
1088+
wp_cache_set_multiple_query_data( $data, 'comment-queries', $last_changed );
10891089
}
10901090

10911091
++$level;

src/wp-includes/class-wp-network-query.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,8 @@ public function get_networks() {
249249
$key = md5( serialize( $_args ) );
250250
$last_changed = wp_cache_get_last_changed( 'networks' );
251251

252-
$cache_key = "get_network_ids:$key:$last_changed";
253-
$cache_value = wp_cache_get( $cache_key, 'network-queries' );
252+
$cache_key = "get_network_ids:$key";
253+
$cache_value = wp_cache_get_query_data( $cache_key, 'network-queries', $last_changed );
254254

255255
if ( false === $cache_value ) {
256256
$network_ids = $this->get_network_ids();
@@ -262,7 +262,7 @@ public function get_networks() {
262262
'network_ids' => $network_ids,
263263
'found_networks' => $this->found_networks,
264264
);
265-
wp_cache_add( $cache_key, $cache_value, 'network-queries' );
265+
wp_cache_set_query_data( $cache_key, $cache_value, 'network-queries', $last_changed );
266266
} else {
267267
$network_ids = $cache_value['network_ids'];
268268
$this->found_networks = $cache_value['found_networks'];

src/wp-includes/class-wp-query.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2884,11 +2884,11 @@ public function get_posts() {
28842884
$key = md5( $comments_request );
28852885
$last_changed = wp_cache_get_last_changed( 'comment' ) . ':' . wp_cache_get_last_changed( 'posts' );
28862886

2887-
$cache_key = "comment_feed:$key:$last_changed";
2888-
$comment_ids = wp_cache_get( $cache_key, 'comment-queries' );
2887+
$cache_key = "comment_feed:$key";
2888+
$comment_ids = wp_cache_get_query_data( $cache_key, 'comment-queries', $last_changed );
28892889
if ( false === $comment_ids ) {
28902890
$comment_ids = $wpdb->get_col( $comments_request );
2891-
wp_cache_add( $cache_key, $comment_ids, 'comment-queries' );
2891+
wp_cache_set_query_data( $cache_key, $comment_ids, 'comment-queries', $last_changed );
28922892
}
28932893
_prime_comment_caches( $comment_ids );
28942894

@@ -3246,15 +3246,21 @@ public function get_posts() {
32463246
$id_query_is_cacheable = false;
32473247
}
32483248

3249+
$last_changed = wp_cache_get_last_changed( 'posts' );
3250+
if ( ! empty( $this->tax_query->queries ) ) {
3251+
$last_changed .= wp_cache_get_last_changed( 'terms' );
3252+
}
3253+
32493254
if ( $q['cache_results'] && $id_query_is_cacheable ) {
32503255
$new_request = str_replace( $fields, "{$wpdb->posts}.*", $this->request );
32513256
$cache_key = $this->generate_cache_key( $q, $new_request );
32523257

32533258
$cache_found = false;
32543259
if ( null === $this->posts ) {
3255-
$cached_results = wp_cache_get( $cache_key, 'post-queries', false, $cache_found );
3260+
$cached_results = wp_cache_get_query_data( $cache_key, 'post-queries', $last_changed );
32563261

32573262
if ( $cached_results ) {
3263+
$cache_found = true;
32583264
/** @var int[] */
32593265
$post_ids = array_map( 'intval', $cached_results['posts'] );
32603266

@@ -3312,7 +3318,7 @@ public function get_posts() {
33123318
'max_num_pages' => $this->max_num_pages,
33133319
);
33143320

3315-
wp_cache_set( $cache_key, $cache_value, 'post-queries' );
3321+
wp_cache_set_query_data( $cache_key, $cache_value, 'post-queries', $last_changed );
33163322
}
33173323

33183324
return $this->posts;
@@ -3486,11 +3492,11 @@ public function get_posts() {
34863492
$comment_key = md5( $comments_request );
34873493
$comment_last_changed = wp_cache_get_last_changed( 'comment' );
34883494

3489-
$comment_cache_key = "comment_feed:$comment_key:$comment_last_changed";
3490-
$comment_ids = wp_cache_get( $comment_cache_key, 'comment-queries' );
3495+
$comment_cache_key = "comment_feed:$comment_key";
3496+
$comment_ids = wp_cache_get_query_data( $comment_cache_key, 'comment-queries', $comment_last_changed );
34913497
if ( false === $comment_ids ) {
34923498
$comment_ids = $wpdb->get_col( $comments_request );
3493-
wp_cache_add( $comment_cache_key, $comment_ids, 'comment-queries' );
3499+
wp_cache_set_query_data( $comment_cache_key, $comment_ids, 'comment-queries', $comment_last_changed );
34943500
}
34953501
_prime_comment_caches( $comment_ids );
34963502

@@ -5062,12 +5068,7 @@ static function ( &$value ) use ( $wpdb, $placeholder ) {
50625068
$sql = $wpdb->remove_placeholder_escape( $sql );
50635069
$key = md5( serialize( $args ) . $sql );
50645070

5065-
$last_changed = wp_cache_get_last_changed( 'posts' );
5066-
if ( ! empty( $this->tax_query->queries ) ) {
5067-
$last_changed .= wp_cache_get_last_changed( 'terms' );
5068-
}
5069-
5070-
$this->query_cache_key = "wp_query:$key:$last_changed";
5071+
$this->query_cache_key = "wp_query:$key";
50715072
return $this->query_cache_key;
50725073
}
50735074

src/wp-includes/class-wp-site-query.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,8 @@ public function get_sites() {
357357
$key = md5( serialize( $_args ) );
358358
$last_changed = wp_cache_get_last_changed( 'sites' );
359359

360-
$cache_key = "get_sites:$key:$last_changed";
361-
$cache_value = wp_cache_get( $cache_key, 'site-queries' );
360+
$cache_key = "get_sites:$key";
361+
$cache_value = wp_cache_get_query_data( $cache_key, 'site-queries', $last_changed );
362362

363363
if ( false === $cache_value ) {
364364
$site_ids = $this->get_site_ids();
@@ -370,7 +370,7 @@ public function get_sites() {
370370
'site_ids' => $site_ids,
371371
'found_sites' => $this->found_sites,
372372
);
373-
wp_cache_add( $cache_key, $cache_value, 'site-queries' );
373+
wp_cache_set_query_data( $cache_key, $cache_value, 'site-queries', $last_changed );
374374
} else {
375375
$site_ids = $cache_value['site_ids'];
376376
$this->found_sites = $cache_value['found_sites'];

src/wp-includes/class-wp-term-query.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -777,8 +777,9 @@ public function get_terms() {
777777
}
778778

779779
if ( $args['cache_results'] ) {
780-
$cache_key = $this->generate_cache_key( $args, $this->request );
781-
$cache = wp_cache_get( $cache_key, 'term-queries' );
780+
$cache_key = $this->generate_cache_key( $args, $this->request );
781+
$last_changed = wp_cache_get_last_changed( 'terms' );
782+
$cache = wp_cache_get_query_data( $cache_key, 'term-queries', $last_changed );
782783

783784
if ( false !== $cache ) {
784785
if ( 'ids' === $_fields ) {
@@ -806,7 +807,7 @@ public function get_terms() {
806807
if ( 'count' === $_fields ) {
807808
$count = $wpdb->get_var( $this->request ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
808809
if ( $args['cache_results'] ) {
809-
wp_cache_set( $cache_key, $count, 'term-queries' );
810+
wp_cache_set_query_data( $cache_key, $count, 'term-queries', $last_changed );
810811
}
811812
return $count;
812813
}
@@ -815,7 +816,7 @@ public function get_terms() {
815816

816817
if ( empty( $terms ) ) {
817818
if ( $args['cache_results'] ) {
818-
wp_cache_add( $cache_key, array(), 'term-queries' );
819+
wp_cache_set_query_data( $cache_key, array(), 'term-queries', $last_changed );
819820
}
820821
return array();
821822
}
@@ -899,7 +900,7 @@ public function get_terms() {
899900
}
900901

901902
if ( $args['cache_results'] ) {
902-
wp_cache_add( $cache_key, $term_cache, 'term-queries' );
903+
wp_cache_set_query_data( $cache_key, $term_cache, 'term-queries', $last_changed );
903904
}
904905

905906
$this->terms = $this->format_terms( $term_objects, $_fields );
@@ -1171,8 +1172,8 @@ protected function generate_cache_key( array $args, $sql ) {
11711172
// Replace wpdb placeholder in the SQL statement used by the cache key.
11721173
$sql = $wpdb->remove_placeholder_escape( $sql );
11731174

1174-
$key = md5( serialize( $cache_args ) . $sql );
1175-
$last_changed = wp_cache_get_last_changed( 'terms' );
1176-
return "get_terms:$key:$last_changed";
1175+
$key = md5( serialize( $cache_args ) . $sql );
1176+
1177+
return "get_terms:$key";
11771178
}
11781179
}

src/wp-includes/class-wp-user-query.php

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -830,8 +830,38 @@ public function query() {
830830
$cache_value = false;
831831
$cache_key = $this->generate_cache_key( $qv, $this->request );
832832
$cache_group = 'user-queries';
833+
$last_changed = wp_cache_get_last_changed( 'users' );
834+
835+
if ( empty( $qv['orderby'] ) ) {
836+
// Default order is by 'user_login'.
837+
$ordersby = array( 'user_login' => '' );
838+
} elseif ( is_array( $qv['orderby'] ) ) {
839+
$ordersby = $qv['orderby'];
840+
} else {
841+
// 'orderby' values may be a comma- or space-separated list.
842+
$ordersby = preg_split( '/[,\s]+/', $qv['orderby'] );
843+
}
844+
845+
$blog_id = 0;
846+
if ( isset( $qv['blog_id'] ) ) {
847+
$blog_id = absint( $qv['blog_id'] );
848+
}
849+
850+
if ( $qv['has_published_posts'] || in_array( 'post_count', $ordersby, true ) ) {
851+
$switch = $blog_id && get_current_blog_id() !== $blog_id;
852+
if ( $switch ) {
853+
switch_to_blog( $blog_id );
854+
}
855+
856+
$last_changed .= wp_cache_get_last_changed( 'posts' );
857+
858+
if ( $switch ) {
859+
restore_current_blog();
860+
}
861+
}
862+
833863
if ( $qv['cache_results'] ) {
834-
$cache_value = wp_cache_get( $cache_key, $cache_group );
864+
$cache_value = wp_cache_get_query_data( $cache_key, $cache_group, $last_changed );
835865
}
836866
if ( false !== $cache_value ) {
837867
$this->results = $cache_value['user_data'];
@@ -866,7 +896,7 @@ public function query() {
866896
'user_data' => $this->results,
867897
'total_users' => $this->total_users,
868898
);
869-
wp_cache_add( $cache_key, $cache_value, $cache_group );
899+
wp_cache_set_query_data( $cache_key, $cache_value, $cache_group, $last_changed );
870900
}
871901
}
872902
}
@@ -1056,38 +1086,9 @@ protected function generate_cache_key( array $args, $sql ) {
10561086
// Replace wpdb placeholder in the SQL statement used by the cache key.
10571087
$sql = $wpdb->remove_placeholder_escape( $sql );
10581088

1059-
$key = md5( $sql );
1060-
$last_changed = wp_cache_get_last_changed( 'users' );
1061-
1062-
if ( empty( $args['orderby'] ) ) {
1063-
// Default order is by 'user_login'.
1064-
$ordersby = array( 'user_login' => '' );
1065-
} elseif ( is_array( $args['orderby'] ) ) {
1066-
$ordersby = $args['orderby'];
1067-
} else {
1068-
// 'orderby' values may be a comma- or space-separated list.
1069-
$ordersby = preg_split( '/[,\s]+/', $args['orderby'] );
1070-
}
1071-
1072-
$blog_id = 0;
1073-
if ( isset( $args['blog_id'] ) ) {
1074-
$blog_id = absint( $args['blog_id'] );
1075-
}
1076-
1077-
if ( $args['has_published_posts'] || in_array( 'post_count', $ordersby, true ) ) {
1078-
$switch = $blog_id && get_current_blog_id() !== $blog_id;
1079-
if ( $switch ) {
1080-
switch_to_blog( $blog_id );
1081-
}
1082-
1083-
$last_changed .= wp_cache_get_last_changed( 'posts' );
1084-
1085-
if ( $switch ) {
1086-
restore_current_blog();
1087-
}
1088-
}
1089+
$key = md5( $sql );
10891090

1090-
return "get_users:$key:$last_changed";
1091+
return "get_users:$key";
10911092
}
10921093

10931094
/**

src/wp-includes/functions.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8061,6 +8061,89 @@ function wp_unique_id_from_values( array $data, string $prefix = '' ): string {
80618061
return $prefix . $hash;
80628062
}
80638063

8064+
/**
8065+
* Retrieves cached query data if valid and unchanged.
8066+
*
8067+
* @param string $cache_key The cache key used for storage and retrieval.
8068+
* @param string $group The cache group used for organizing data.
8069+
* @param string $last_changed The timestamp of the last modification to the cache group.
8070+
* @return mixed|false The cached data if valid, or false if the cache does not exist or is outdated.
8071+
*/
8072+
function wp_cache_get_query_data( $cache_key, $group, $last_changed ) {
8073+
$cache = wp_cache_get( $cache_key, $group );
8074+
8075+
if ( false === $cache ) {
8076+
return false;
8077+
}
8078+
8079+
if ( $last_changed !== $cache['last_changed'] ) {
8080+
return false;
8081+
}
8082+
8083+
return $cache['data'];
8084+
}
8085+
8086+
/**
8087+
* Stores query-related data in the cache.
8088+
*
8089+
* @param string $cache_key The cache key under which to store the data.
8090+
* @param mixed $data The data to be cached.
8091+
* @param string $group The cache group to which the data belongs.
8092+
* @param string $last_changed The timestamp or identifier indicating the last change to the cached data.
8093+
*/
8094+
function wp_cache_set_query_data( $cache_key, $data, $group, $last_changed ) {
8095+
wp_cache_set(
8096+
$cache_key,
8097+
array(
8098+
'data' => $data,
8099+
'last_chagned' => $last_changed,
8100+
),
8101+
$group
8102+
);
8103+
}
8104+
8105+
/**
8106+
* Retrieves multiple items from the cache and validates their freshness.
8107+
*
8108+
* @param array $cache_keys Array of cache keys to retrieve.
8109+
* @param string $group The group of the cache to check.
8110+
* @param string $last_changed The timestamp of the last cache modification for validation.
8111+
* @return array An associative array containing cache values. Values are `false` if they are not found or outdated.
8112+
*/
8113+
function wp_cache_get_multiple_query_data( $cache_keys, $group, $last_changed ) {
8114+
$cache = wp_cache_get_multiple( $cache_keys, $group );
8115+
8116+
foreach ( $cache as $key => $value ) {
8117+
if ( false === $value ) {
8118+
continue;
8119+
}
8120+
if ( $last_changed !== $value['last_changed'] ) {
8121+
$cache[ $key ] = false;
8122+
}
8123+
}
8124+
8125+
return $cache;
8126+
}
8127+
8128+
/**
8129+
* Stores multiple pieces of query data in the cache.
8130+
*
8131+
* @param array $cache_keys Array of cache keys to define the items to be stored.
8132+
* @param mixed $data Data to be stored in the cache for all keys.
8133+
* @param string $group Group to which the cached data belongs.
8134+
* @param string $last_changed Timestamp indicating the last modification time for the data.
8135+
*/
8136+
function wp_cache_set_multiple_query_data( $cache_keys, $data, $group, $last_changed ) {
8137+
$new_cache = array();
8138+
foreach ( $cache_keys as $key ) {
8139+
$new_cache[ $key ] = array(
8140+
'data' => $data,
8141+
'last_changed' => $last_changed,
8142+
);
8143+
}
8144+
wp_cache_set_multiple( $new_cache, $group );
8145+
}
8146+
80648147
/**
80658148
* Gets last changed date for the specified cache group.
80668149
*

0 commit comments

Comments
 (0)