Make WordPress Core


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

    r56434 r60697  
    11561156    $key          = md5( $query );
    11571157    $last_changed = wp_cache_get_last_changed( 'posts' );
    1158     $cache_key    = "find_post_by_old_slug:$key:$last_changed";
    1159     $cache        = wp_cache_get( $cache_key, 'post-queries' );
     1158    $cache_key    = "find_post_by_old_slug:$key";
     1159    $cache        = wp_cache_get_salted( $cache_key, 'post-queries', $last_changed );
    11601160    if ( false !== $cache ) {
    11611161        $id = $cache;
    11621162    } else {
    11631163        $id = (int) $wpdb->get_var( $query );
    1164         wp_cache_set( $cache_key, $id, 'post-queries' );
     1164        wp_cache_set_salted( $cache_key, $id, 'post-queries', $last_changed );
    11651165    }
    11661166
     
    11991199        $key          = md5( $query );
    12001200        $last_changed = wp_cache_get_last_changed( 'posts' );
    1201         $cache_key    = "find_post_by_old_date:$key:$last_changed";
    1202         $cache        = wp_cache_get( $cache_key, 'post-queries' );
     1201        $cache_key    = "find_post_by_old_date:$key";
     1202        $cache        = wp_cache_get_salted( $cache_key, 'post-queries', $last_changed );
    12031203        if ( false !== $cache ) {
    12041204            $id = $cache;
     
    12091209                $id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts, $wpdb->postmeta AS pm_slug, $wpdb->postmeta AS pm_date WHERE ID = pm_slug.post_id AND ID = pm_date.post_id AND post_type = %s AND pm_slug.meta_key = '_wp_old_slug' AND pm_slug.meta_value = %s AND pm_date.meta_key = '_wp_old_date'" . $date_query, $post_type, get_query_var( 'name' ) ) );
    12101210            }
    1211             wp_cache_set( $cache_key, $id, 'post-queries' );
     1211            wp_cache_set_salted( $cache_key, $id, 'post-queries', $last_changed );
    12121212        }
    12131213    }
Note: See TracChangeset for help on using the changeset viewer.