From 98050e404d04140039f94cd38241a594b0b1b528 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Sat, 21 May 2016 17:27:29 +0000 Subject: [PATCH] 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 --- wp-includes/post.php | 21 +++++++++++++++++++++ wp-includes/version.php | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/wp-includes/post.php b/wp-includes/post.php index 1920e84f9a..ceaa1d96a8 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -4231,6 +4231,24 @@ function get_page( $page, $output = OBJECT, $filter = 'raw') { function get_page_by_path( $page_path, $output = OBJECT, $post_type = 'page' ) { 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 = str_replace('%2F', '/', $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 ) { return get_post( $foundid, $output ); } diff --git a/wp-includes/version.php b/wp-includes/version.php index 8e44732726..2659386a23 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @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.