From ac6ce3e0262bfe446b6009e3495ea0dbe6b4e4df Mon Sep 17 00:00:00 2001 From: spacedmonkey Date: Tue, 21 Jun 2022 13:34:13 +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. Built from https://develop.svn.wordpress.org/trunk@53549 git-svn-id: http://core.svn.wordpress.org/trunk@53138 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/query.php | 30 ++++++++++++++++++++++++------ wp-includes/version.php | 2 +- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/wp-includes/query.php b/wp-includes/query.php index d4ac780212..c980c73286 100644 --- a/wp-includes/query.php +++ b/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/wp-includes/version.php b/wp-includes/version.php index 8d7324d316..649d376886 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.1-alpha-53548'; +$wp_version = '6.1-alpha-53549'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.