From dfcfe0b21d499159bb932fe7a45eed86049a72c3 Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Tue, 21 Jun 2022 13:32:48 +0000 Subject: [PATCH] Posts, Post Types: Add caching to `_find_post_by_old_slug` and `_find_post_by_old_date` functions. Cache result of database queries in `_find_post_by_old_slug` and `_find_post_by_old_date` functions. This means that repeated requests to a url where the page is not found, will result in hitting a cache for sites running persistent object caching. Props Spacedmonkey, dd32, mukesh27, pbearne, flixos90. Fixes #36723. git-svn-id: https://develop.svn.wordpress.org/trunk@53549 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/query.php | 30 ++++++-- .../phpunit/tests/rewrite/oldDateRedirect.php | 68 +++++++++++++++++++ .../phpunit/tests/rewrite/oldSlugRedirect.php | 58 ++++++++++++++++ 3 files changed, 150 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/query.php b/src/wp-includes/query.php index d4ac780212..c980c73286 100644 --- a/src/wp-includes/query.php +++ b/src/wp-includes/query.php @@ -1150,7 +1150,16 @@ function _find_post_by_old_slug( $post_type ) { $query .= $wpdb->prepare( ' AND DAYOFMONTH(post_date) = %d', get_query_var( 'day' ) ); } - $id = (int) $wpdb->get_var( $query ); + $key = md5( $query ); + $last_changed = wp_cache_get_last_changed( 'posts' ); + $cache_key = "find_post_by_old_slug:$key:$last_changed"; + $cache = wp_cache_get( $cache_key, 'posts' ); + if ( false !== $cache ) { + $id = $cache; + } else { + $id = (int) $wpdb->get_var( $query ); + wp_cache_set( $cache_key, $id, 'posts' ); + } return $id; } @@ -1183,11 +1192,20 @@ function _find_post_by_old_date( $post_type ) { $id = 0; if ( $date_query ) { - $id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta AS pm_date, $wpdb->posts WHERE ID = post_id AND post_type = %s AND meta_key = '_wp_old_date' AND post_name = %s" . $date_query, $post_type, get_query_var( 'name' ) ) ); - - if ( ! $id ) { - // Check to see if an old slug matches the old date. - $id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts, $wpdb->postmeta AS pm_slug, $wpdb->postmeta AS pm_date WHERE ID = pm_slug.post_id AND ID = pm_date.post_id AND post_type = %s AND pm_slug.meta_key = '_wp_old_slug' AND pm_slug.meta_value = %s AND pm_date.meta_key = '_wp_old_date'" . $date_query, $post_type, get_query_var( 'name' ) ) ); + $query = $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta AS pm_date, $wpdb->posts WHERE ID = post_id AND post_type = %s AND meta_key = '_wp_old_date' AND post_name = %s" . $date_query, $post_type, get_query_var( 'name' ) ); + $key = md5( $query ); + $last_changed = wp_cache_get_last_changed( 'posts' ); + $cache_key = "find_post_by_old_date:$key:$last_changed"; + $cache = wp_cache_get( $cache_key, 'posts' ); + if ( false !== $cache ) { + $id = $cache; + } else { + $id = (int) $wpdb->get_var( $query ); + if ( ! $id ) { + // Check to see if an old slug matches the old date. + $id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts, $wpdb->postmeta AS pm_slug, $wpdb->postmeta AS pm_date WHERE ID = pm_slug.post_id AND ID = pm_date.post_id AND post_type = %s AND pm_slug.meta_key = '_wp_old_slug' AND pm_slug.meta_value = %s AND pm_date.meta_key = '_wp_old_date'" . $date_query, $post_type, get_query_var( 'name' ) ) ); + } + wp_cache_set( $cache_key, $id, 'posts' ); } } diff --git a/tests/phpunit/tests/rewrite/oldDateRedirect.php b/tests/phpunit/tests/rewrite/oldDateRedirect.php index 7f4519875d..cdbab81c51 100644 --- a/tests/phpunit/tests/rewrite/oldDateRedirect.php +++ b/tests/phpunit/tests/rewrite/oldDateRedirect.php @@ -2,6 +2,7 @@ /** * @group rewrite + * @covers wp_old_slug_redirect */ class Tests_Rewrite_OldDateRedirect extends WP_UnitTestCase { protected $old_date_redirect_url; @@ -86,6 +87,73 @@ class Tests_Rewrite_OldDateRedirect extends WP_UnitTestCase { $this->assertSame( $permalink, $this->old_date_redirect_url ); } + /** + * @ticket 36723 + */ + public function test_old_date_slug_redirect_cache() { + $old_permalink = user_trailingslashit( get_permalink( self::$post_id ) ); + + $time = '2004-01-03 00:00:00'; + wp_update_post( + array( + 'ID' => self::$post_id, + 'post_date' => $time, + 'post_date_gmt' => get_gmt_from_date( $time ), + 'post_name' => 'bar-baz', + ) + ); + + $permalink = user_trailingslashit( get_permalink( self::$post_id ) ); + + $this->go_to( $old_permalink ); + wp_old_slug_redirect(); + $num_queries = get_num_queries(); + $this->assertSame( $permalink, $this->old_date_redirect_url ); + wp_old_slug_redirect(); + $this->assertSame( $permalink, $this->old_date_redirect_url ); + $this->assertSame( $num_queries, get_num_queries() ); + } + + /** + * @ticket 36723 + */ + public function test_old_date_redirect_cache_invalidation() { + global $wpdb; + $old_permalink = user_trailingslashit( get_permalink( self::$post_id ) ); + + $time = '2004-01-03 00:00:00'; + wp_update_post( + array( + 'ID' => self::$post_id, + 'post_date' => $time, + 'post_date_gmt' => get_gmt_from_date( $time ), + 'post_name' => 'bar-baz', + ) + ); + + $permalink = user_trailingslashit( get_permalink( self::$post_id ) ); + + $this->go_to( $old_permalink ); + wp_old_slug_redirect(); + $this->assertSame( $permalink, $this->old_date_redirect_url ); + + $time = '2014-02-01 00:00:00'; + wp_update_post( + array( + 'ID' => $this->post_id, + 'post_date' => $time, + 'post_date_gmt' => get_gmt_from_date( $time ), + 'post_name' => 'bar-baz', + ) + ); + + $num_queries = get_num_queries(); + $this->go_to( $permalink ); + wp_old_slug_redirect(); + $this->assertSame( $permalink, $this->old_date_redirect_url ); + $this->assertGreaterThan( $num_queries, get_num_queries() ); + } + public function test_old_date_redirect_attachment() { $old_permalink = get_attachment_link( self::$attachment_id ); diff --git a/tests/phpunit/tests/rewrite/oldSlugRedirect.php b/tests/phpunit/tests/rewrite/oldSlugRedirect.php index 2083f9bcce..f455235948 100644 --- a/tests/phpunit/tests/rewrite/oldSlugRedirect.php +++ b/tests/phpunit/tests/rewrite/oldSlugRedirect.php @@ -3,6 +3,7 @@ /** * @group rewrite * @ticket 33920 + * @covers wp_old_slug_redirect */ class Tests_Rewrite_OldSlugRedirect extends WP_UnitTestCase { protected $old_slug_redirect_url; @@ -52,6 +53,63 @@ class Tests_Rewrite_OldSlugRedirect extends WP_UnitTestCase { $this->assertSame( $permalink, $this->old_slug_redirect_url ); } + /** + * @ticket 36723 + */ + public function test_old_slug_redirect_cache() { + $old_permalink = user_trailingslashit( get_permalink( $this->post_id ) ); + + wp_update_post( + array( + 'ID' => $this->post_id, + 'post_name' => 'bar-baz', + ) + ); + + $permalink = user_trailingslashit( get_permalink( $this->post_id ) ); + + $this->go_to( $old_permalink ); + + wp_old_slug_redirect(); + $num_queries = get_num_queries(); + $this->assertSame( $permalink, $this->old_slug_redirect_url ); + wp_old_slug_redirect(); + $this->assertSame( $permalink, $this->old_slug_redirect_url ); + $this->assertSame( $num_queries, get_num_queries() ); + } + + /** + * @ticket 36723 + */ + public function test_old_slug_redirect_cache_invalidation() { + $old_permalink = user_trailingslashit( get_permalink( $this->post_id ) ); + + wp_update_post( + array( + 'ID' => $this->post_id, + 'post_name' => 'bar-baz', + ) + ); + + $permalink = user_trailingslashit( get_permalink( $this->post_id ) ); + + $this->go_to( $old_permalink ); + + wp_old_slug_redirect(); + $this->assertSame( $permalink, $this->old_slug_redirect_url ); + + wp_update_post( + array( + 'ID' => $this->post_id, + 'post_name' => 'foo-bar', + ) + ); + $num_queries = get_num_queries(); + wp_old_slug_redirect(); + $this->assertSame( $permalink, $this->old_slug_redirect_url ); + $this->assertSame( $num_queries + 1, get_num_queries() ); + } + public function test_old_slug_redirect_attachment() { $file = DIR_TESTDATA . '/images/canola.jpg'; $attachment_id = self::factory()->attachment->create_object(