From a327759a7c13ad56f201e34a24c9d09c653054a9 Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Wed, 11 Oct 2023 11:58:31 +0100 Subject: [PATCH] Store last changed last_changed in cache value. --- src/wp-includes/class-wp-comment-query.php | 7 ++- src/wp-includes/class-wp-network-query.php | 7 ++- src/wp-includes/class-wp-query.php | 69 ++++++++++++---------- src/wp-includes/class-wp-site-query.php | 9 +-- src/wp-includes/class-wp-user-query.php | 64 +++++++++----------- 5 files changed, 78 insertions(+), 78 deletions(-) diff --git a/src/wp-includes/class-wp-comment-query.php b/src/wp-includes/class-wp-comment-query.php index 9ebddd1c74e15..4421e0e286435 100644 --- a/src/wp-includes/class-wp-comment-query.php +++ b/src/wp-includes/class-wp-comment-query.php @@ -451,9 +451,9 @@ public function get_comments() { $key = md5( serialize( $_args ) ); $last_changed = wp_cache_get_last_changed( 'comment' ); - $cache_key = "get_comments:$key:$last_changed"; + $cache_key = "get_comments:$key"; $cache_value = wp_cache_get( $cache_key, 'comment-queries' ); - if ( false === $cache_value ) { + if ( ! is_array( $cache_value ) || $cache_value['last_changed'] !== $last_changed ) { $comment_ids = $this->get_comment_ids(); if ( $comment_ids ) { $this->set_found_comments(); @@ -462,8 +462,9 @@ public function get_comments() { $cache_value = array( 'comment_ids' => $comment_ids, 'found_comments' => $this->found_comments, + 'last_changed' => $last_changed, ); - wp_cache_add( $cache_key, $cache_value, 'comment-queries' ); + wp_cache_set( $cache_key, $cache_value, 'comment-queries' ); } else { $comment_ids = $cache_value['comment_ids']; $this->found_comments = $cache_value['found_comments']; diff --git a/src/wp-includes/class-wp-network-query.php b/src/wp-includes/class-wp-network-query.php index 7199ec03a1a15..552af0280b72f 100644 --- a/src/wp-includes/class-wp-network-query.php +++ b/src/wp-includes/class-wp-network-query.php @@ -249,10 +249,10 @@ public function get_networks() { $key = md5( serialize( $_args ) ); $last_changed = wp_cache_get_last_changed( 'networks' ); - $cache_key = "get_network_ids:$key:$last_changed"; + $cache_key = "get_network_ids:$key"; $cache_value = wp_cache_get( $cache_key, 'network-queries' ); - if ( false === $cache_value ) { + if ( ! is_array( $cache_value ) || $cache_value['last_changed'] !== $last_changed ) { $network_ids = $this->get_network_ids(); if ( $network_ids ) { $this->set_found_networks(); @@ -261,8 +261,9 @@ public function get_networks() { $cache_value = array( 'network_ids' => $network_ids, 'found_networks' => $this->found_networks, + 'last_changed' => $last_changed, ); - wp_cache_add( $cache_key, $cache_value, 'network-queries' ); + wp_cache_set( $cache_key, $cache_value, 'network-queries' ); } else { $network_ids = $cache_value['network_ids']; $this->found_networks = $cache_value['found_networks']; diff --git a/src/wp-includes/class-wp-query.php b/src/wp-includes/class-wp-query.php index e79f460fc56cd..5ad0a0603ad4c 100644 --- a/src/wp-includes/class-wp-query.php +++ b/src/wp-includes/class-wp-query.php @@ -3170,45 +3170,52 @@ public function get_posts() { } if ( $q['cache_results'] && $id_query_is_cacheable ) { - $new_request = str_replace( $fields, "{$wpdb->posts}.*", $this->request ); - $cache_key = $this->generate_cache_key( $q, $new_request ); + $new_request = str_replace( $fields, "{$wpdb->posts}.*", $this->request ); + $cache_key = $this->generate_cache_key( $q, $new_request ); + $last_changed = wp_cache_get_last_changed( 'posts' ); + if ( ! empty( $this->tax_query->queries ) ) { + $last_changed .= wp_cache_get_last_changed( 'terms' ); + } $cache_found = false; if ( null === $this->posts ) { $cached_results = wp_cache_get( $cache_key, 'post-queries', false, $cache_found ); - if ( $cached_results ) { - /** @var int[] */ - $post_ids = array_map( 'intval', $cached_results['posts'] ); + if ( $cached_results['last_changed'] === $last_changed ) { + /** @var int[] */ + $post_ids = array_map( 'intval', $cached_results['posts'] ); - $this->post_count = count( $post_ids ); - $this->found_posts = $cached_results['found_posts']; - $this->max_num_pages = $cached_results['max_num_pages']; + $this->post_count = count( $post_ids ); + $this->found_posts = $cached_results['found_posts']; + $this->max_num_pages = $cached_results['max_num_pages']; - if ( 'ids' === $q['fields'] ) { - $this->posts = $post_ids; + if ( 'ids' === $q['fields'] ) { + $this->posts = $post_ids; - return $this->posts; - } elseif ( 'id=>parent' === $q['fields'] ) { - _prime_post_parent_id_caches( $post_ids ); + return $this->posts; + } elseif ( 'id=>parent' === $q['fields'] ) { + _prime_post_parent_id_caches( $post_ids ); - /** @var int[] */ - $post_parents = wp_cache_get_multiple( $post_ids, 'post_parent' ); + /** @var int[] */ + $post_parents = wp_cache_get_multiple( $post_ids, 'post_parent' ); - foreach ( $post_parents as $id => $post_parent ) { - $obj = new stdClass(); - $obj->ID = (int) $id; - $obj->post_parent = (int) $post_parent; + foreach ( $post_parents as $id => $post_parent ) { + $obj = new stdClass(); + $obj->ID = (int) $id; + $obj->post_parent = (int) $post_parent; - $this->posts[] = $obj; - } + $this->posts[] = $obj; + } - return $post_parents; - } else { - _prime_post_caches( $post_ids, $q['update_post_term_cache'], $q['update_post_meta_cache'] ); - /** @var WP_Post[] */ - $this->posts = array_map( 'get_post', $post_ids ); + return $post_parents; + } else { + _prime_post_caches( $post_ids, $q['update_post_term_cache'], $q['update_post_meta_cache'] ); + /** @var WP_Post[] */ + $this->posts = array_map( 'get_post', $post_ids ); + } } + } else { + $cache_found = false; } } } @@ -3228,6 +3235,7 @@ public function get_posts() { 'posts' => $this->posts, 'found_posts' => $this->found_posts, 'max_num_pages' => $this->max_num_pages, + 'last_changed' => $last_changed, ); wp_cache_set( $cache_key, $cache_value, 'post-queries' ); @@ -3263,6 +3271,7 @@ public function get_posts() { 'posts' => $post_ids, 'found_posts' => $this->found_posts, 'max_num_pages' => $this->max_num_pages, + 'last_changed' => $last_changed, ); wp_cache_set( $cache_key, $cache_value, 'post-queries' ); @@ -3348,6 +3357,7 @@ public function get_posts() { 'posts' => $post_ids, 'found_posts' => $this->found_posts, 'max_num_pages' => $this->max_num_pages, + 'last_changed' => $last_changed, ); wp_cache_set( $cache_key, $cache_value, 'post-queries' ); @@ -4870,12 +4880,7 @@ static function ( &$value ) use ( $wpdb, $placeholder ) { $sql = $wpdb->remove_placeholder_escape( $sql ); $key = md5( serialize( $args ) . $sql ); - $last_changed = wp_cache_get_last_changed( 'posts' ); - if ( ! empty( $this->tax_query->queries ) ) { - $last_changed .= wp_cache_get_last_changed( 'terms' ); - } - - return "wp_query:$key:$last_changed"; + return "wp_query:$key"; } /** diff --git a/src/wp-includes/class-wp-site-query.php b/src/wp-includes/class-wp-site-query.php index 6598866162445..b71d45903767b 100644 --- a/src/wp-includes/class-wp-site-query.php +++ b/src/wp-includes/class-wp-site-query.php @@ -360,17 +360,18 @@ public function get_sites() { $cache_key = "get_sites:$key:$last_changed"; $cache_value = wp_cache_get( $cache_key, 'site-queries' ); - if ( false === $cache_value ) { + if ( ! is_array( $cache_value ) || $cache_value['last_changed'] !== $last_changed ) { $site_ids = $this->get_site_ids(); if ( $site_ids ) { $this->set_found_sites(); } $cache_value = array( - 'site_ids' => $site_ids, - 'found_sites' => $this->found_sites, + 'site_ids' => $site_ids, + 'found_sites' => $this->found_sites, + 'last_changed' => $last_changed, ); - wp_cache_add( $cache_key, $cache_value, 'site-queries' ); + wp_cache_set( $cache_key, $cache_value, 'site-queries' ); } else { $site_ids = $cache_value['site_ids']; $this->found_sites = $cache_value['found_sites']; diff --git a/src/wp-includes/class-wp-user-query.php b/src/wp-includes/class-wp-user-query.php index b8b4733cf09f6..5a2f6a0f83315 100644 --- a/src/wp-includes/class-wp-user-query.php +++ b/src/wp-includes/class-wp-user-query.php @@ -828,14 +828,34 @@ public function query() { $cache_value = false; $cache_key = $this->generate_cache_key( $qv, $this->request ); $cache_group = 'user-queries'; + + $last_changed = wp_cache_get_last_changed( 'users' ); + + $blog_id = 0; + if ( isset( $args['blog_id'] ) ) { + $blog_id = absint( $args['blog_id'] ); + } + + if ( $args['has_published_posts'] || in_array( 'post_count', $ordersby, true ) ) { + $switch = $blog_id && get_current_blog_id() !== $blog_id; + if ( $switch ) { + switch_to_blog( $blog_id ); + } + + $last_changed .= wp_cache_get_last_changed( 'posts' ); + + if ( $switch ) { + restore_current_blog(); + } + } + if ( $qv['cache_results'] ) { $cache_value = wp_cache_get( $cache_key, $cache_group ); } - if ( false !== $cache_value ) { + if ( is_array( $cache_value ) && $cache_value['last_changed'] === $last_changed ) { $this->results = $cache_value['user_data']; $this->total_users = $cache_value['total_users']; } else { - if ( is_array( $qv['fields'] ) ) { $this->results = $wpdb->get_results( $this->request ); } else { @@ -861,10 +881,11 @@ public function query() { if ( $qv['cache_results'] ) { $cache_value = array( - 'user_data' => $this->results, - 'total_users' => $this->total_users, + 'user_data' => $this->results, + 'total_users' => $this->total_users, + 'last_changed' => $last_changed, ); - wp_cache_add( $cache_key, $cache_value, $cache_group ); + wp_cache_set( $cache_key, $cache_value, $cache_group ); } } } @@ -1054,38 +1075,9 @@ protected function generate_cache_key( array $args, $sql ) { // Replace wpdb placeholder in the SQL statement used by the cache key. $sql = $wpdb->remove_placeholder_escape( $sql ); - $key = md5( $sql ); - $last_changed = wp_cache_get_last_changed( 'users' ); - - if ( empty( $args['orderby'] ) ) { - // Default order is by 'user_login'. - $ordersby = array( 'user_login' => '' ); - } elseif ( is_array( $args['orderby'] ) ) { - $ordersby = $args['orderby']; - } else { - // 'orderby' values may be a comma- or space-separated list. - $ordersby = preg_split( '/[,\s]+/', $args['orderby'] ); - } - - $blog_id = 0; - if ( isset( $args['blog_id'] ) ) { - $blog_id = absint( $args['blog_id'] ); - } - - if ( $args['has_published_posts'] || in_array( 'post_count', $ordersby, true ) ) { - $switch = $blog_id && get_current_blog_id() !== $blog_id; - if ( $switch ) { - switch_to_blog( $blog_id ); - } - - $last_changed .= wp_cache_get_last_changed( 'posts' ); - - if ( $switch ) { - restore_current_blog(); - } - } + $key = md5( $sql ); - return "get_users:$key:$last_changed"; + return "get_users:$key"; } /**