Make WordPress Core

Changeset 55085


Ignore:
Timestamp:
01/18/2023 11:18:36 AM (2 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.

Location:
trunk
Files:
2 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 ) {
  • trunk/tests/phpunit/tests/link/getAdjacentPost.php

    r52010 r55085  
    351351        return $excluded_terms;
    352352    }
     353
     354    /**
     355     * @ticket 41131
     356     */
     357    public function test_get_adjacent_post_cache() {
     358        // Need some sample posts to test adjacency.
     359        $post_one = self::factory()->post->create_and_get(
     360            array(
     361                'post_title' => 'First',
     362                'post_date'  => '2012-01-01 12:00:00',
     363            )
     364        );
     365
     366        $post_two = self::factory()->post->create_and_get(
     367            array(
     368                'post_title' => 'Second',
     369                'post_date'  => '2012-02-01 12:00:00',
     370            )
     371        );
     372
     373        $post_three = self::factory()->post->create_and_get(
     374            array(
     375                'post_title' => 'Third',
     376                'post_date'  => '2012-03-01 12:00:00',
     377            )
     378        );
     379
     380        $post_four = self::factory()->post->create_and_get(
     381            array(
     382                'post_title' => 'Fourth',
     383                'post_date'  => '2012-04-01 12:00:00',
     384            )
     385        );
     386
     387        // Assign some terms.
     388        wp_set_object_terms( $post_one->ID, 'WordPress', 'category', false );
     389        wp_set_object_terms( $post_three->ID, 'WordPress', 'category', false );
     390
     391        wp_set_object_terms( $post_two->ID, 'plugins', 'post_tag', false );
     392        wp_set_object_terms( $post_four->ID, 'plugins', 'post_tag', false );
     393
     394        // Test normal post adjacency.
     395        $this->go_to( get_permalink( $post_two->ID ) );
     396
     397        // Test getting the right result.
     398        $first_run = get_adjacent_post( false, '', true );
     399        $this->assertEquals( $post_one, $first_run, 'Did not get first post when on second post' );
     400        $this->assertNotEquals( $post_two, $first_run, 'Got second post when on second post' );
     401
     402        // Query count to test caching.
     403        $num_queries = get_num_queries();
     404        $second_run  = get_adjacent_post( false, '', true );
     405        $this->assertNotEquals( $post_two, $second_run, 'Got second post when on second post on second run' );
     406        $this->assertEquals( $post_one, $second_run, 'Did not get first post when on second post on second run' );
     407        $this->assertSame( $num_queries, get_num_queries() );
     408
     409        // Test creating new post busts cache.
     410        $post_five   = self::factory()->post->create_and_get(
     411            array(
     412                'post_title' => 'Five',
     413                'post_date'  => '2012-04-01 12:00:00',
     414            )
     415        );
     416        $num_queries = get_num_queries();
     417
     418        $this->assertEquals( $post_one, get_adjacent_post( false, '', true ), 'Did not get first post after new post is added' );
     419        $this->assertSame( get_num_queries() - $num_queries, 1, 'Number of queries run was not one after new post is added' );
     420
     421        $this->assertEquals( $post_four, get_adjacent_post( true, '', false ), 'Did not get forth post after new post is added' );
     422        $num_queries = get_num_queries();
     423        $this->assertEquals( $post_four, get_adjacent_post( true, '', false ), 'Did not get forth post after new post is added' );
     424        $this->assertSame( $num_queries, get_num_queries() );
     425        wp_set_object_terms( $post_four->ID, 'themes', 'post_tag', false );
     426
     427        $num_queries = get_num_queries();
     428        $this->assertEquals( $post_four, get_adjacent_post( true, '', false ), 'Result of function call is wrong after after adding new term' );
     429        $this->assertSame( get_num_queries() - $num_queries, 2, 'Number of queries run was not two after adding new term' );
     430    }
    353431}
Note: See TracChangeset for help on using the changeset viewer.