Make WordPress Core


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

    r60681 r60697  
    20702070        $query   = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date $order $limit";
    20712071        $key     = md5( $query );
    2072         $key     = "wp_get_archives:$key:$last_changed";
    2073         $results = wp_cache_get( $key, 'post-queries' );
     2072        $key     = "wp_get_archives:$key";
     2073        $results = wp_cache_get_salted( $key, 'post-queries', $last_changed );
    20742074        if ( ! $results ) {
    20752075            $results = $wpdb->get_results( $query );
    2076             wp_cache_set( $key, $results, 'post-queries' );
     2076            wp_cache_set_salted( $key, $results, 'post-queries', $last_changed );
    20772077        }
    20782078        if ( $results ) {
     
    20952095        $query   = "SELECT YEAR(post_date) AS `year`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date) ORDER BY post_date $order $limit";
    20962096        $key     = md5( $query );
    2097         $key     = "wp_get_archives:$key:$last_changed";
    2098         $results = wp_cache_get( $key, 'post-queries' );
     2097        $key     = "wp_get_archives:$key";
     2098        $results = wp_cache_get_salted( $key, 'post-queries', $last_changed );
    20992099        if ( ! $results ) {
    21002100            $results = $wpdb->get_results( $query );
    2101             wp_cache_set( $key, $results, 'post-queries' );
     2101            wp_cache_set_salted( $key, $results, 'post-queries', $last_changed );
    21022102        }
    21032103        if ( $results ) {
     
    21192119        $query   = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, DAYOFMONTH(post_date) AS `dayofmonth`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date), DAYOFMONTH(post_date) ORDER BY post_date $order $limit";
    21202120        $key     = md5( $query );
    2121         $key     = "wp_get_archives:$key:$last_changed";
    2122         $results = wp_cache_get( $key, 'post-queries' );
     2121        $key     = "wp_get_archives:$key";
     2122        $results = wp_cache_get_salted( $key, 'post-queries', $last_changed );
    21232123        if ( ! $results ) {
    21242124            $results = $wpdb->get_results( $query );
    2125             wp_cache_set( $key, $results, 'post-queries' );
     2125            wp_cache_set_salted( $key, $results, 'post-queries', $last_changed );
    21262126        }
    21272127        if ( $results ) {
     
    21452145        $query   = "SELECT DISTINCT $week AS `week`, YEAR( `post_date` ) AS `yr`, DATE_FORMAT( `post_date`, '%Y-%m-%d' ) AS `yyyymmdd`, count( `ID` ) AS `posts` FROM `$wpdb->posts` $join $where GROUP BY $week, YEAR( `post_date` ) ORDER BY `post_date` $order $limit";
    21462146        $key     = md5( $query );
    2147         $key     = "wp_get_archives:$key:$last_changed";
    2148         $results = wp_cache_get( $key, 'post-queries' );
     2147        $key     = "wp_get_archives:$key";
     2148        $results = wp_cache_get_salted( $key, 'post-queries', $last_changed );
    21492149        if ( ! $results ) {
    21502150            $results = $wpdb->get_results( $query );
    2151             wp_cache_set( $key, $results, 'post-queries' );
     2151            wp_cache_set_salted( $key, $results, 'post-queries', $last_changed );
    21522152        }
    21532153        $arc_w_last = '';
     
    21842184        $query   = "SELECT * FROM $wpdb->posts $join $where ORDER BY $orderby $limit";
    21852185        $key     = md5( $query );
    2186         $key     = "wp_get_archives:$key:$last_changed";
    2187         $results = wp_cache_get( $key, 'post-queries' );
     2186        $key     = "wp_get_archives:$key";
     2187        $results = wp_cache_get_salted( $key, 'post-queries', $last_changed );
    21882188        if ( ! $results ) {
    21892189            $results = $wpdb->get_results( $query );
    2190             wp_cache_set( $key, $results, 'post-queries' );
     2190            wp_cache_set_salted( $key, $results, 'post-queries', $last_changed );
    21912191        }
    21922192        if ( $results ) {
Note: See TracChangeset for help on using the changeset viewer.