buddypress/bp-blogs/bp-blogs-cache.php
Boone B Gorges e21309e51b 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

git-svn-id: https://buddypress.svn.wordpress.org/trunk@7918 cdf35c40-ae34-48e0-9cc9-0c9da1808c22
2014-02-18 12:50:11 +00:00

76 lines
2.6 KiB
PHP

<?php
/**
* BuddyPress Blogs Caching.
*
* Caching functions handle the clearing of cached objects and pages on specific
* actions throughout BuddyPress.
*
* @package BuddyPress
* @subpackage BlogsCache
*/
// Exit if accessed directly
if ( !defined( 'ABSPATH' ) ) exit;
/**
* Slurp up blogmeta for a specified set of blogs.
*
* It grabs all blogmeta associated with all of the blogs passed
* in $blog_ids and adds it to the WP cache. This improves efficiency when
* using querying blogmeta inline.
*
* @param int|str|array $blog_ids Accepts a single blog ID, or a comma-
* separated list or array of blog IDs.
*/
function bp_blogs_update_meta_cache( $blog_ids = false ) {
$cache_args = array(
'object_ids' => $blog_ids,
'object_type' => buddypress()->blogs->id,
'object_column' => 'blog_id',
'cache_group' => 'blog_meta',
'meta_table' => buddypress()->blogs->table_name_blogmeta,
);
bp_update_meta_cache( $cache_args );
}
/**
* Clear the blog object cache.
*
* @since BuddyPress (1.0.0)
*
* @param int $blog_id ID of the current blog.
* @param int $user_id ID of the user whose blog cache should be cleared.
*/
function bp_blogs_clear_blog_object_cache( $blog_id, $user_id ) {
wp_cache_delete( 'bp_blogs_of_user_' . $user_id, 'bp' );
wp_cache_delete( 'bp_total_blogs_for_user_' . $user_id, 'bp' );
}
/**
* Clear cache when a new blog is created.
*
* @since BuddyPress (1.0.0)
*
* @param BP_Blogs_Blog $recorded_blog_obj The recorded blog, passed by
* 'bp_blogs_new_blog'.
*/
function bp_blogs_format_clear_blog_cache( $recorded_blog_obj ) {
bp_blogs_clear_blog_object_cache( false, $recorded_blog_obj->user_id );
wp_cache_delete( 'bp_total_blogs', 'bp' );
}
// List actions to clear object caches on
add_action( 'bp_blogs_remove_blog_for_user', 'bp_blogs_clear_blog_object_cache', 10, 2 );
add_action( 'bp_blogs_new_blog', 'bp_blogs_format_clear_blog_cache', 10, 2 );
// List actions to clear super cached pages on, if super cache is installed
add_action( 'bp_blogs_remove_data_for_blog', 'bp_core_clear_cache' );
add_action( 'bp_blogs_remove_comment', 'bp_core_clear_cache' );
add_action( 'bp_blogs_remove_post', 'bp_core_clear_cache' );
add_action( 'bp_blogs_remove_blog_for_user', 'bp_core_clear_cache' );
add_action( 'bp_blogs_remove_blog', 'bp_core_clear_cache' );
add_action( 'bp_blogs_new_blog_comment', 'bp_core_clear_cache' );
add_action( 'bp_blogs_new_blog_post', 'bp_core_clear_cache' );
add_action( 'bp_blogs_new_blog', 'bp_core_clear_cache' );
add_action( 'bp_blogs_remove_data', 'bp_core_clear_cache' );