Make WordPress Core


Ignore:
Timestamp:
01/18/2023 11:18:36 AM (3 years ago)
Author:
spacedmonkey
Message:

Posts, Post Types: Use persistent caching in get_adjacent_post function.

The function get_adjacent_post cached the results of database query in the cache group counts. This is a none persistent group and meant cache would not persist on the next request. Change cache to save to the posts cache group. Cache invalidation is done by using get last changed value of the posts and terms group as a salt for the cache key.

Props spacedmonkey, peterwilsoncc, johnbillion, boonebgorges, mukesh27, dd32.
Fixes #41131.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/link-template.php

    r54943 r55085  
    19771977    $sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1", $post, $order );
    19781978
    1979     $query     = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort";
    1980     $query_key = 'adjacent_post_' . md5( $query );
    1981     $result    = wp_cache_get( $query_key, 'counts' );
     1979    $query        = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort";
     1980    $key          = md5( $query );
     1981    $last_changed = wp_cache_get_last_changed( 'posts' );
     1982    if ( $in_same_term || ! empty( $excluded_terms ) ) {
     1983        $last_changed .= wp_cache_get_last_changed( 'terms' );
     1984    }
     1985    $cache_key = "adjacent_post:$key:$last_changed";
     1986
     1987    $result = wp_cache_get( $cache_key, 'posts' );
    19821988    if ( false !== $result ) {
    19831989        if ( $result ) {
     
    19921998    }
    19931999
    1994     wp_cache_set( $query_key, $result, 'counts' );
     2000    wp_cache_set( $cache_key, $result, 'posts' );
    19952001
    19962002    if ( $result ) {
Note: See TracChangeset for help on using the changeset viewer.