Make WordPress Core


Ignore:
Timestamp:
08/31/2025 09:41:54 PM (5 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-term-query.php

    r60661 r60697  
    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_salted( $cache_key, 'term-queries', $last_changed );
    782783
    783784            if ( false !== $cache ) {
     
    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_salted( $cache_key, $count, 'term-queries', $last_changed );
    810811            }
    811812            return $count;
     
    816817        if ( empty( $terms ) ) {
    817818            if ( $args['cache_results'] ) {
    818                 wp_cache_add( $cache_key, array(), 'term-queries' );
     819                wp_cache_set_salted( $cache_key, array(), 'term-queries', $last_changed );
    819820            }
    820821            return array();
     
    901902
    902903        if ( $args['cache_results'] ) {
    903             wp_cache_add( $cache_key, $term_cache, 'term-queries' );
     904            wp_cache_set_salted( $cache_key, $term_cache, 'term-queries', $last_changed );
    904905        }
    905906
     
    11731174        $sql = $wpdb->remove_placeholder_escape( $sql );
    11741175
    1175         $key          = md5( serialize( $cache_args ) . $sql );
    1176         $last_changed = wp_cache_get_last_changed( 'terms' );
    1177         return "get_terms:$key:$last_changed";
     1176        $key = md5( serialize( $cache_args ) . $sql );
     1177
     1178        return "get_terms:$key";
    11781179    }
    11791180}
Note: See TracChangeset for help on using the changeset viewer.