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-site-query.php

    r59009 r60697  
    358358        $last_changed = wp_cache_get_last_changed( 'sites' );
    359359
    360         $cache_key   = "get_sites:$key:$last_changed";
    361         $cache_value = wp_cache_get( $cache_key, 'site-queries' );
     360        $cache_key   = "get_sites:$key";
     361        $cache_value = wp_cache_get_salted( $cache_key, 'site-queries', $last_changed );
    362362
    363363        if ( false === $cache_value ) {
     
    371371                'found_sites' => $this->found_sites,
    372372            );
    373             wp_cache_add( $cache_key, $cache_value, 'site-queries' );
     373            wp_cache_set_salted( $cache_key, $cache_value, 'site-queries', $last_changed );
    374374        } else {
    375375            $site_ids          = $cache_value['site_ids'];
Note: See TracChangeset for help on using the changeset viewer.