From 1edf259b45c531f107aefaf6888703da12e89327 Mon Sep 17 00:00:00 2001 From: Ryan Boren Date: Tue, 15 Jan 2013 17:32:35 +0000 Subject: [PATCH] In get_pages(), cache queries to individual cache buckets instead of storing them in one cached array. Also, store post IDs instead of full objects. This reduces overall memory usage as well as the size of the cache buckets. Use incrementor style passive cache invalidation. Props nprasath002 see #23167 git-svn-id: http://core.svn.wordpress.org/trunk@23300 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/post.php | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/wp-includes/post.php b/wp-includes/post.php index 80b5bfaf45..72b14bde1e 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -3649,15 +3649,18 @@ function get_pages($args = '') { if ( array_diff( $post_status, get_post_stati() ) ) return $pages; - $cache = array(); + // $args can be whatever, only use the args defined in defaults to compute the key $key = md5( serialize( compact(array_keys($defaults)) ) ); - if ( $cache = wp_cache_get( 'get_pages', 'posts' ) ) { - if ( is_array($cache) && isset( $cache[ $key ] ) && is_array( $cache[ $key ] ) ) { - // Convert to WP_Post instances - $pages = array_map( 'get_post', $cache[ $key ] ); - $pages = apply_filters( 'get_pages', $pages, $r ); - return $pages; - } + $last_changed = wp_cache_get( 'last_changed', 'posts' ); + if ( ! $last_changed ) + $last_changed = wp_cache_set( 'last_changed', 1, 'posts' ); + + $cache_key = "get_pages:$key:$last_changed"; + if ( $cache = wp_cache_get( $cache_key, 'posts' ) ) { + // Convert to WP_Post instances + $pages = array_map( 'get_post', $cache ); + $pages = apply_filters('get_pages', $pages, $r); + return $pages; } if ( !is_array($cache) ) @@ -3827,8 +3830,11 @@ function get_pages($args = '') { } } - $cache[ $key ] = $pages; - wp_cache_set( 'get_pages', $cache, 'posts' ); + $page_structure = array(); + foreach ( $pages as $page ) + $page_structure[] = $page->ID; + + wp_cache_set( $cache_key, $page_structure, 'posts' ); // Convert to WP_Post instances $pages = array_map( 'get_post', $pages ); @@ -4663,6 +4669,13 @@ function clean_post_cache( $post ) { wp_cache_delete( 'all_page_ids', 'posts' ); do_action( 'clean_page_cache', $post->ID ); } + + if ( function_exists( 'wp_cache_incr' ) ) { + wp_cache_incr( 'last_changed', 1, 'posts' ); + } else { + $last_changed = wp_cache_get( 'last_changed', 'posts' ); + wp_cache_set( 'last_changed', $last_changed + 1, 'posts' ); + } } /**