Skip to content

Commit

Permalink
Posts, Post Types: Use persistent caching in get_adjacent_post func…
Browse files Browse the repository at this point in the history
…tion.

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.
Built from https://develop.svn.wordpress.org/trunk@55085


git-svn-id: https://core.svn.wordpress.org/trunk@54618 1a063a9b-81f0-0310-95a4-ce76da25c4cd
  • Loading branch information
spacedmonkey committed Jan 18, 2023
1 parent d897684 commit 17d6dd9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
14 changes: 10 additions & 4 deletions wp-includes/link-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -1976,9 +1976,15 @@ function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previo
*/
$sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1", $post, $order );

$query = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort";
$query_key = 'adjacent_post_' . md5( $query );
$result = wp_cache_get( $query_key, 'counts' );
$query = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort";
$key = md5( $query );
$last_changed = wp_cache_get_last_changed( 'posts' );
if ( $in_same_term || ! empty( $excluded_terms ) ) {
$last_changed .= wp_cache_get_last_changed( 'terms' );
}
$cache_key = "adjacent_post:$key:$last_changed";

$result = wp_cache_get( $cache_key, 'posts' );
if ( false !== $result ) {
if ( $result ) {
$result = get_post( $result );
Expand All @@ -1991,7 +1997,7 @@ function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previo
$result = '';
}

wp_cache_set( $query_key, $result, 'counts' );
wp_cache_set( $cache_key, $result, 'posts' );

if ( $result ) {
$result = get_post( $result );
Expand Down
2 changes: 1 addition & 1 deletion wp-includes/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.2-alpha-55084';
$wp_version = '6.2-alpha-55085';

/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
Expand Down

0 comments on commit 17d6dd9

Please sign in to comment.