Make WordPress Core

Changeset 23300


Ignore:
Timestamp:
01/15/2013 05:32:35 PM (12 years ago)
Author:
ryan
Message:

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

File:
1 edited

Legend:

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

    r23265 r23300  
    36503650        return $pages;
    36513651
    3652     $cache = array();
     3652    // $args can be whatever, only use the args defined in defaults to compute the key
    36533653    $key = md5( serialize( compact(array_keys($defaults)) ) );
    3654     if ( $cache = wp_cache_get( 'get_pages', 'posts' ) ) {
    3655         if ( is_array($cache) && isset( $cache[ $key ] ) && is_array( $cache[ $key ] ) ) {
    3656             // Convert to WP_Post instances
    3657             $pages = array_map( 'get_post', $cache[ $key ] );
    3658             $pages = apply_filters( 'get_pages', $pages, $r );
    3659             return $pages;
    3660         }
     3654    $last_changed = wp_cache_get( 'last_changed', 'posts' );
     3655    if ( ! $last_changed )
     3656        $last_changed = wp_cache_set( 'last_changed', 1, 'posts' );
     3657
     3658    $cache_key = "get_pages:$key:$last_changed";
     3659    if ( $cache = wp_cache_get( $cache_key, 'posts' ) ) {
     3660        // Convert to WP_Post instances
     3661        $pages = array_map( 'get_post', $cache );
     3662        $pages = apply_filters('get_pages', $pages, $r);
     3663        return $pages;
    36613664    }
    36623665
     
    38283831    }
    38293832
    3830     $cache[ $key ] = $pages;
    3831     wp_cache_set( 'get_pages', $cache, 'posts' );
     3833    $page_structure = array();
     3834    foreach ( $pages as $page )
     3835        $page_structure[] = $page->ID;
     3836
     3837    wp_cache_set( $cache_key, $page_structure, 'posts' );
    38323838
    38333839    // Convert to WP_Post instances
     
    46644670        do_action( 'clean_page_cache', $post->ID );
    46654671    }
     4672
     4673    if ( function_exists( 'wp_cache_incr' ) ) {
     4674        wp_cache_incr( 'last_changed', 1, 'posts' );
     4675    } else {
     4676        $last_changed = wp_cache_get( 'last_changed', 'posts' );
     4677        wp_cache_set( 'last_changed', $last_changed + 1, 'posts' );
     4678    }
    46664679}
    46674680
Note: See TracChangeset for help on using the changeset viewer.