Make WordPress Core


Ignore:
Timestamp:
08/31/2025 09:41:54 PM (13 months ago)
Author:
spacedmonkey
Message:

Caching API: Use consistent cache keys for query groups.

Query-based caches are now improved by reusing cache keys. Previously, cache keys for query caches were generated using the last_changed value as part of the key. This meant that whenever last_changed was updated, all the previously cached values for the group became unreachable.

The new approach allows WordPress to replace previously cached results that are known to be stale. The previous approach relied on the object cache backend evicting stale keys which is done at various levels of efficiency.

To address this, the following new helper functions have been introduced:

  • wp_cache_get_salted
  • wp_cache_set_salted
  • wp_cache_get_multiple_salted
  • wp_cache_set_multiple_salted

These functions provide a consistent way to get/set query caches. Instead of using the last_changed value as part of the cache key, it is now stored inside the cache value as a "salt". This allows cache keys to be reused, with values updated in place rather than relying on eviction of outdated entries.

Props spacedmonkey, peterwilsoncc, flixos90, sanchothefat, tillkruess, rmccue, mukesh27, adamsilverstein, owi, nickchomey.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-query.php

    r60444 r60697  
    28832883
    28842884                        $key          = md5( $comments_request );
    2885                         $last_changed = wp_cache_get_last_changed( 'comment' ) . ':' . wp_cache_get_last_changed( 'posts' );
    2886 
    2887                         $cache_key   = "comment_feed:$key:$last_changed";
    2888                         $comment_ids = wp_cache_get( $cache_key, 'comment-queries' );
     2885                        $last_changed = array(
     2886                                wp_cache_get_last_changed( 'comment' ),
     2887                                wp_cache_get_last_changed( 'posts' ),
     2888                        );
     2889
     2890                        $cache_key   = "comment_feed:$key";
     2891                        $comment_ids = wp_cache_get_salted( $cache_key, 'comment-queries', $last_changed );
    28892892                        if ( false === $comment_ids ) {
    28902893                                $comment_ids = $wpdb->get_col( $comments_request );
    2891                                 wp_cache_add( $cache_key, $comment_ids, 'comment-queries' );
     2894                                wp_cache_set_salted( $cache_key, $comment_ids, 'comment-queries', $last_changed );
    28922895                        }
    28932896                        _prime_comment_caches( $comment_ids );
    … …  
    32473250                }
    32483251
     3252                $last_changed = (array) wp_cache_get_last_changed( 'posts' );
     3253                if ( ! empty( $this->tax_query->queries ) ) {
     3254                        $last_changed[] = wp_cache_get_last_changed( 'terms' );
     3255                }
     3256
    32493257                if ( $q['cache_results'] && $id_query_is_cacheable ) {
    32503258                        $new_request = str_replace( $fields, "{$wpdb->posts}.*", $this->request );
    … …  
    32533261                        $cache_found = false;
    32543262                        if ( null === $this->posts ) {
    3255                                 $cached_results = wp_cache_get( $cache_key, 'post-queries', false, $cache_found );
     3263                                $cached_results = wp_cache_get_salted( $cache_key, 'post-queries', $last_changed );
    32563264
    32573265                                if ( $cached_results ) {
     3266                                        $cache_found = true;
    32583267                                        /** @var int[] */
    32593268                                        $post_ids = array_map( 'intval', $cached_results['posts'] );
    … …  
    33133322                                );
    33143323
    3315                                 wp_cache_set( $cache_key, $cache_value, 'post-queries' );
     3324                                wp_cache_set_salted( $cache_key, $cache_value, 'post-queries', $last_changed );
    33163325                        }
    33173326
    … …  
    33513360                                );
    33523361
    3353                                 wp_cache_set( $cache_key, $cache_value, 'post-queries' );
     3362                                wp_cache_set_salted( $cache_key, $cache_value, 'post-queries', $last_changed );
    33543363                        }
    33553364
    … …  
    34493458                        );
    34503459
    3451                         wp_cache_set( $cache_key, $cache_value, 'post-queries' );
     3460                        wp_cache_set_salted( $cache_key, $cache_value, 'post-queries', $last_changed );
    34523461                }
    34533462
    … …  
    34873496                        $comment_last_changed = wp_cache_get_last_changed( 'comment' );
    34883497
    3489                         $comment_cache_key = "comment_feed:$comment_key:$comment_last_changed";
    3490                         $comment_ids       = wp_cache_get( $comment_cache_key, 'comment-queries' );
     3498                        $comment_cache_key = "comment_feed:$comment_key";
     3499                        $comment_ids       = wp_cache_get_salted( $comment_cache_key, 'comment-queries', $comment_last_changed );
    34913500                        if ( false === $comment_ids ) {
    34923501                                $comment_ids = $wpdb->get_col( $comments_request );
    3493                                 wp_cache_add( $comment_cache_key, $comment_ids, 'comment-queries' );
     3502                                wp_cache_set_salted( $comment_cache_key, $comment_ids, 'comment-queries', $comment_last_changed );
    34943503                        }
    34953504                        _prime_comment_caches( $comment_ids );
    … …  
    50635072                $key = md5( serialize( $args ) . $sql );
    50645073
    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";
     5074                $this->query_cache_key = "wp_query:$key";
    50715075                return $this->query_cache_key;
    50725076        }
Note: See TracChangeset for help on using the changeset viewer.