Cache queries in `get_page_by_path()`.

Props spacedmonkey.
Fixes #36711.
Built from https://develop.svn.wordpress.org/trunk@37479


git-svn-id: http://core.svn.wordpress.org/trunk@37447 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Boone Gorges 2016-05-21 17:27:29 +00:00
parent c5c80b064d
commit 98050e404d
2 changed files with 22 additions and 1 deletions

View File

@ -4231,6 +4231,24 @@ function get_page( $page, $output = OBJECT, $filter = 'raw') {
function get_page_by_path( $page_path, $output = OBJECT, $post_type = 'page' ) { function get_page_by_path( $page_path, $output = OBJECT, $post_type = 'page' ) {
global $wpdb; global $wpdb;
$last_changed = wp_cache_get( 'last_changed', 'posts' );
if ( false === $last_changed ) {
$last_changed = microtime();
wp_cache_set( 'last_changed', $last_changed, 'posts' );
}
$hash = md5( $page_path . serialize( $post_type ) );
$cache_key = "get_page_by_path:$hash:$last_changed";
$cached = wp_cache_get( $cache_key, 'posts' );
if ( false !== $cached ) {
// Special case: '0' is a bad `$page_path`.
if ( '0' === $cached || 0 === $cached ) {
return;
} else {
return get_post( $cached );
}
}
$page_path = rawurlencode(urldecode($page_path)); $page_path = rawurlencode(urldecode($page_path));
$page_path = str_replace('%2F', '/', $page_path); $page_path = str_replace('%2F', '/', $page_path);
$page_path = str_replace('%20', ' ', $page_path); $page_path = str_replace('%20', ' ', $page_path);
@ -4285,6 +4303,9 @@ function get_page_by_path( $page_path, $output = OBJECT, $post_type = 'page' ) {
} }
} }
// We cache misses as well as hits.
wp_cache_set( $cache_key, $foundid, 'posts' );
if ( $foundid ) { if ( $foundid ) {
return get_post( $foundid, $output ); return get_post( $foundid, $output );
} }

View File

@ -4,7 +4,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '4.6-alpha-37478'; $wp_version = '4.6-alpha-37479';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.