Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/wp-includes/class-wp-comment-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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'];
Expand Down
7 changes: 4 additions & 3 deletions src/wp-includes/class-wp-network-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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'];
Expand Down
69 changes: 37 additions & 32 deletions src/wp-includes/class-wp-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand All @@ -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' );
Expand Down Expand Up @@ -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' );
Expand Down Expand Up @@ -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' );
Expand Down Expand Up @@ -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";
}

/**
Expand Down
9 changes: 5 additions & 4 deletions src/wp-includes/class-wp-site-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down
64 changes: 28 additions & 36 deletions src/wp-includes/class-wp-user-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 );
}
}
}
Expand Down Expand Up @@ -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";
}

/**
Expand Down