Make WordPress Core


Ignore:
Timestamp:
05/21/2016 05:26:55 PM (6 years ago)
Author:
boonebgorges
Message:

Cache queries in get_page_by_path().

Props spacedmonkey.
Fixes #36711.

File:
1 edited

Legend:

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

    r37403 r37479  
    42324232    global $wpdb;
    42334233
     4234    $last_changed = wp_cache_get( 'last_changed', 'posts' );
     4235    if ( false === $last_changed ) {
     4236        $last_changed = microtime();
     4237        wp_cache_set( 'last_changed', $last_changed, 'posts' );
     4238    }
     4239
     4240    $hash = md5( $page_path . serialize( $post_type ) );
     4241    $cache_key = "get_page_by_path:$hash:$last_changed";
     4242    $cached = wp_cache_get( $cache_key, 'posts' );
     4243    if ( false !== $cached ) {
     4244        // Special case: '0' is a bad `$page_path`.
     4245        if ( '0' === $cached || 0 === $cached ) {
     4246            return;
     4247        } else {
     4248            return get_post( $cached );
     4249        }
     4250    }
     4251
    42344252    $page_path = rawurlencode(urldecode($page_path));
    42354253    $page_path = str_replace('%2F', '/', $page_path);
     
    42854303        }
    42864304    }
     4305
     4306    // We cache misses as well as hits.
     4307    wp_cache_set( $cache_key, $foundid, 'posts' );
    42874308
    42884309    if ( $foundid ) {
Note: See TracChangeset for help on using the changeset viewer.