Make WordPress Core

Changeset 64043


Ignore:
Timestamp:
10/01/2026 11:19:51 PM (7 weeks ago)
Author:
peterwilsoncc
Message:

Canonical: Cache redirect_guess_404_permalink().

Introduce caching of both hits and misses to the database query within redirect_guess_404_permalink().

Props altf4falt, audrasjb, im3dabasia1, spacedmonkey, westonruter, wildworks.
Fixes #64250.

Location:
trunk
Files:
3 edited

Legend:

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

    r63347 r64043  
    10081008                }
    10091009
    1010                 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
    1011                 $post_id = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE $where AND post_status IN ('" . implode( "', '", esc_sql( $publicly_viewable_statuses ) ) . "')" );
     1010                $query = "SELECT ID FROM $wpdb->posts WHERE $where AND post_status IN ('" . implode( "', '", esc_sql( $publicly_viewable_statuses ) ) . "')";
     1011
     1012                $key          = md5( $query );
     1013                $last_changed = wp_cache_get_last_changed( 'posts' );
     1014                $cache_key    = "redirect_guess_404_permalink:$key";
     1015                $cache        = wp_cache_get_salted( $cache_key, 'post-queries', $last_changed );
     1016
     1017                if ( false !== $cache ) {
     1018                        $post_id = $cache;
     1019                } else {
     1020                        // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
     1021                        $post_id = (int) $wpdb->get_var( $query );
     1022
     1023                        // Cache misses as well as hits.
     1024                        wp_cache_set_salted( $cache_key, $post_id, 'post-queries', $last_changed );
     1025                }
    10121026
    10131027                if ( ! $post_id ) {
  • trunk/tests/phpstan/baselines/argument.type.neon

    r64012 r64043  
    690690                        path: ../../../src/wp-includes/blocks.php
    691691                -
    692                         message: '#^Parameter \#1 \$post of function get_permalink expects int\|WP_Post, string given\.$#'
    693                         identifier: argument.type
    694                         count: 2
    695                         path: ../../../src/wp-includes/canonical.php
    696                 -
    697                         message: '#^Parameter \#1 \$post_id of function get_post_comments_feed_link expects int, string given\.$#'
    698                         identifier: argument.type
    699                         count: 1
    700                         path: ../../../src/wp-includes/canonical.php
    701                 -
    702692                        message: '#^Parameter \#2 \$callback of function preg_replace_callback expects callable\(array\<string\>\)\: string, ''lowercase_octets'' given\.$#'
    703693                        identifier: argument.type
  • trunk/tests/phpunit/tests/canonical.php

    r59017 r64043  
    418418
    419419        /**
     420         * @ticket 64250
     421         *
     422         * @covers ::redirect_guess_404_permalink
     423         */
     424        public function test_redirect_guess_404_permalink_cache() {
     425                $post = self::factory()->post->create(
     426                        array(
     427                                'post_title' => 'redirect-guess-404-permalink-cache',
     428                        )
     429                );
     430
     431                $this->go_to( 'redirect-guess-404-permalink-cach' );
     432
     433                $first_run = redirect_guess_404_permalink();
     434                $this->assertSame( get_permalink( $post ), $first_run, 'Did not guess the correct permalink on first run.' );
     435
     436                $start_num_queries = get_num_queries();
     437                $second_run        = redirect_guess_404_permalink();
     438                $this->assertSame( $first_run, $second_run, 'Result changed between cached and uncached run.' );
     439                $this->assertSame( 0, get_num_queries() - $start_num_queries, 'A cached lookup performed an additional database query.' );
     440        }
     441
     442        /**
     443         * @ticket 64250
     444         *
     445         * @covers ::redirect_guess_404_permalink
     446         */
     447        public function test_redirect_guess_404_permalink_cache_misses_are_cached() {
     448                $this->go_to( 'redirect-guess-404-permalink-no-such-post' );
     449
     450                $this->assertFalse( redirect_guess_404_permalink(), 'Expected no match for a nonexistent slug.' );
     451
     452                $start_num_queries = get_num_queries();
     453                $this->assertFalse( redirect_guess_404_permalink(), 'Expected no match for a nonexistent slug on second run.' );
     454                $this->assertSame( 0, get_num_queries() - $start_num_queries, 'A cached "not found" result performed an additional database query.' );
     455        }
     456
     457        /**
     458         * @ticket 64250
     459         *
     460         * @covers ::redirect_guess_404_permalink
     461         */
     462        public function test_redirect_guess_404_permalink_cache_invalidated_on_new_matching_post() {
     463                $this->go_to( 'redirect-guess-404-permalink-new-post' );
     464
     465                // Prime a "not found" cache entry.
     466                $this->assertFalse( redirect_guess_404_permalink(), 'Expected no match before the matching post exists.' );
     467
     468                $post = self::factory()->post->create(
     469                        array(
     470                                'post_title' => 'redirect-guess-404-permalink-new-post',
     471                        )
     472                );
     473
     474                $start_num_queries = get_num_queries();
     475                $this->assertSame( get_permalink( $post ), redirect_guess_404_permalink(), 'Newly created matching post was not found after cache invalidation.' );
     476                $this->assertSame( 1, get_num_queries() - $start_num_queries, 'Expected exactly one new query after the posts cache was invalidated.' );
     477        }
     478
     479        /**
     480         * @ticket 64250
     481         *
     482         * @covers ::redirect_guess_404_permalink
     483         */
     484        public function test_redirect_guess_404_permalink_cache_invalidated_on_post_delete() {
     485                $post = self::factory()->post->create(
     486                        array(
     487                                'post_title' => 'redirect-guess-404-permalink-delete-me',
     488                        )
     489                );
     490
     491                $this->go_to( 'redirect-guess-404-permalink-delete-m' );
     492
     493                $this->assertSame( get_permalink( $post ), redirect_guess_404_permalink(), 'Did not guess the correct permalink before deletion.' );
     494
     495                wp_delete_post( $post, true );
     496
     497                $start_num_queries = get_num_queries();
     498                $this->assertFalse( redirect_guess_404_permalink(), 'Deleted post should no longer be guessed after cache invalidation.' );
     499                $this->assertSame( 1, get_num_queries() - $start_num_queries, 'Expected exactly one new query after the posts cache was invalidated by deletion.' );
     500        }
     501
     502        /**
     503         * @ticket 64250
     504         *
     505         * @covers ::redirect_guess_404_permalink
     506         */
     507        public function test_redirect_guess_404_permalink_cache_keys_do_not_collide() {
     508                $post_post = self::factory()->post->create(
     509                        array(
     510                                'post_title' => 'redirect-guess-collision',
     511                                'post_type'  => 'post',
     512                        )
     513                );
     514                $page_post = self::factory()->post->create(
     515                        array(
     516                                'post_title' => 'redirect-guess-collision',
     517                                'post_type'  => 'page',
     518                        )
     519                );
     520
     521                $this->go_to( '/?name=redirect-guess-collisio&post_type=post' );
     522                $this->assertSame( get_permalink( $post_post ), redirect_guess_404_permalink(), 'Setting post type to post did not return the post permalink.' );
     523
     524                // Re-run without navigating away (go_to() flushes the object cache) to confirm the cached result is reused.
     525                $start_num_queries = get_num_queries();
     526                $this->assertSame( get_permalink( $post_post ), redirect_guess_404_permalink(), 'Result changed between cached and uncached run.' );
     527                $this->assertSame( 0, get_num_queries() - $start_num_queries, 'A cached lookup performed an additional database query.' );
     528
     529                $this->go_to( '/?name=redirect-guess-collisio&post_type=page' );
     530                $this->assertSame( get_permalink( $page_post ), redirect_guess_404_permalink(), 'Setting post type to page did not return the page permalink.' );
     531        }
     532
     533        /**
    420534         * @ticket 43745
    421535         */
Note: See TracChangeset for help on using the changeset viewer.