Skip to:
Content

BuddyPress.org

Changeset 7918


Ignore:
Timestamp:
02/18/2014 12:50:11 PM (8 years ago)
Author:
boonebgorges
Message:

Pre-fetch blogmeta when in blog loops

This reduces overhead when querying for blog meta in the context of a
bp_has_blogs() template loop. Mirrors similar functionality in the activity,
groups, and xprofile components.

Also introduces an update_meta_cache parameter to the bp_has_blogs() stack, so
that plugin/theme authors can disable this pre-fetching.

Fixes #5398

Location:
trunk
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-blogs/bp-blogs-cache.php

    r7555 r7918  
    1414if ( !defined( 'ABSPATH' ) ) exit;
    1515
     16/**
     17 * Slurp up blogmeta for a specified set of blogs.
     18 *
     19 * It grabs all blogmeta associated with all of the blogs passed
     20 * in $blog_ids and adds it to the WP cache. This improves efficiency when
     21 * using querying blogmeta inline.
     22 *
     23 * @param int|str|array $blog_ids Accepts a single blog ID, or a comma-
     24 *        separated list or array of blog IDs.
     25 */
     26function bp_blogs_update_meta_cache( $blog_ids = false ) {
     27    $cache_args = array(
     28        'object_ids'       => $blog_ids,
     29        'object_type'      => buddypress()->blogs->id,
     30        'object_column'    => 'blog_id',
     31        'cache_group'      => 'blog_meta',
     32        'meta_table'       => buddypress()->blogs->table_name_blogmeta,
     33    );
     34
     35    bp_update_meta_cache( $cache_args );
     36}
    1637/**
    1738 * Clear the blog object cache.
  • trunk/bp-blogs/bp-blogs-classes.php

    r7699 r7918  
    115115     * @param string|bool $search_terms Optional. Search by text stored in
    116116     *        blogmeta (such as the blog name). Default: false.
     117     * @param bool $update_meta_cache Whether to pre-fetch metadata for
     118     *        blogs. Default: true.
    117119     * @return array Multidimensional results array, structured as follows:
    118120     *           'blogs' - Array of located blog objects
    119121     *           'total' - A count of the total blogs matching the filter params
    120122     */
    121     public static function get( $type, $limit = false, $page = false, $user_id = 0, $search_terms = false ) {
     123    public static function get( $type, $limit = false, $page = false, $user_id = 0, $search_terms = false, $update_meta_cache = true ) {
    122124        global $bp, $wpdb;
    123125
     
    162164        $paged_blogs = BP_Blogs_Blog::get_blog_extras( $paged_blogs, $blog_ids, $type );
    163165
     166        if ( $update_meta_cache ) {
     167            bp_blogs_update_meta_cache( $blog_ids );
     168        }
     169
    164170        return array( 'blogs' => $paged_blogs, 'total' => $total_blogs );
    165171    }
  • trunk/bp-blogs/bp-blogs-functions.php

    r7885 r7918  
    3838 *     @type int $per_page Default: 20.
    3939 *     @type int $page Default: 1.
     40 *     @type bool $update_meta_cache Whether to pre-fetch blogmeta. Default: true.
    4041 * }
    4142 * @return array See {@link BP_Blogs_Blog::get()}.
     
    4950        'per_page'     => 20,       // The number of results to return per page
    5051        'page'         => 1,        // The page to return if limiting per page
     52        'update_meta_cache' => true,
    5153    );
    5254
     
    5456    extract( $params, EXTR_SKIP );
    5557
    56     return apply_filters( 'bp_blogs_get_blogs', BP_Blogs_Blog::get( $type, $per_page, $page, $user_id, $search_terms ), $params );
     58    return apply_filters( 'bp_blogs_get_blogs', BP_Blogs_Blog::get( $type, $per_page, $page, $user_id, $search_terms, $update_meta_cache ), $params );
    5759}
    5860
  • trunk/bp-blogs/bp-blogs-template.php

    r7828 r7918  
    170170     * @param string $page_arg The string used as a query parameter in
    171171     *        pagination links. Default: 'bpage'.
    172      */
    173     function __construct( $type, $page, $per_page, $max, $user_id, $search_terms, $page_arg = 'bpage' ) {
     172     * @param bool $update_meta_cache Whether to pre-fetch metadata for
     173     *        queried blogs.
     174     */
     175    function __construct( $type, $page, $per_page, $max, $user_id, $search_terms, $page_arg = 'bpage', $update_meta_cache = true ) {
    174176
    175177        $this->pag_page = isset( $_REQUEST[$page_arg] ) ? intval( $_REQUEST[$page_arg] ) : $page;
     
    179181            $this->blogs = BP_Blogs_Blog::get_by_letter( $_REQUEST['letter'], $this->pag_num, $this->pag_page );
    180182        else
    181             $this->blogs = bp_blogs_get_blogs( array( 'type' => $type, 'per_page' => $this->pag_num, 'page' => $this->pag_page, 'user_id' => $user_id, 'search_terms' => $search_terms ) );
     183            $this->blogs = bp_blogs_get_blogs( array( 'type' => $type, 'per_page' => $this->pag_num, 'page' => $this->pag_page, 'user_id' => $user_id, 'search_terms' => $search_terms, 'update_meta_cache' => $update_meta_cache, ) );
    182184
    183185        if ( !$max || $max >= (int) $this->blogs['total'] )
     
    360362
    361363        'user_id'      => $user_id,       // Pass a user_id to limit to only blogs this user has higher than subscriber access to
    362         'search_terms' => $search_terms   // Pass search terms to filter on the blog title or description.
     364        'search_terms' => $search_terms,  // Pass search terms to filter on the blog title or description.
     365        'update_meta_cache' => true,
    363366    );
    364367
     
    379382    }
    380383
    381     $blogs_template = new BP_Blogs_Template( $type, $page, $per_page, $max, $user_id, $search_terms, $page_arg );
     384    $blogs_template = new BP_Blogs_Template( $type, $page, $per_page, $max, $user_id, $search_terms, $page_arg, $update_meta_cache );
    382385    return apply_filters( 'bp_has_blogs', $blogs_template->has_blogs(), $blogs_template );
    383386}
Note: See TracChangeset for help on using the changeset viewer.