Make WordPress Core


Ignore:
Timestamp:
06/21/2022 01:32:48 PM (3 years ago)
Author:
spacedmonkey
Message:

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.

File:
1 edited

Legend:

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

    r53429 r53549  
    11511151    }
    11521152
    1153     $id = (int) $wpdb->get_var( $query );
     1153    $key          = md5( $query );
     1154    $last_changed = wp_cache_get_last_changed( 'posts' );
     1155    $cache_key    = "find_post_by_old_slug:$key:$last_changed";
     1156    $cache        = wp_cache_get( $cache_key, 'posts' );
     1157    if ( false !== $cache ) {
     1158        $id = $cache;
     1159    } else {
     1160        $id = (int) $wpdb->get_var( $query );
     1161        wp_cache_set( $cache_key, $id, 'posts' );
     1162    }
    11541163
    11551164    return $id;
     
    11841193    $id = 0;
    11851194    if ( $date_query ) {
    1186         $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' ) ) );
    1187 
    1188         if ( ! $id ) {
    1189             // Check to see if an old slug matches the old date.
    1190             $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' ) ) );
     1195        $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' ) );
     1196        $key          = md5( $query );
     1197        $last_changed = wp_cache_get_last_changed( 'posts' );
     1198        $cache_key    = "find_post_by_old_date:$key:$last_changed";
     1199        $cache        = wp_cache_get( $cache_key, 'posts' );
     1200        if ( false !== $cache ) {
     1201            $id = $cache;
     1202        } else {
     1203            $id = (int) $wpdb->get_var( $query );
     1204            if ( ! $id ) {
     1205                // Check to see if an old slug matches the old date.
     1206                $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' ) ) );
     1207            }
     1208            wp_cache_set( $cache_key, $id, 'posts' );
    11911209        }
    11921210    }
Note: See TracChangeset for help on using the changeset viewer.