buddypress/bp-notifications/bp-notifications-cache.php
Boone B Gorges 820bb51450 Object caching for the more frequently used items in bp-notifications
Several notifications queries are made on every pageload:
- an unread count to set up navigation
- an unread count for the toolbar
- the notification items themselves for the toolbar

These three queries actually result in the identical SQL query, so we can
eliminate two of them by caching the value by the pageload. With persistent
caching, the performance benefits are obviously even greater.

The chosen technique for caching here is 'all_for_user_'. This makes
invalidation fairly straightforward, because we can simply bust the
'all_for_user_x' cache whenever a save or delete takes place on a notification
item that is related to user_x. For more complex parts of the notification
component (such as the notifications page itself), if caching is desired,
it'll probably make sense to split the query and cache individual items.

Fixes #5377

git-svn-id: https://buddypress.svn.wordpress.org/trunk@7813 cdf35c40-ae34-48e0-9cc9-0c9da1808c22
2014-02-06 20:33:50 +00:00

41 lines
1.2 KiB
PHP

<?php
/**
* Functions related to notifications caching.
*
* @since BuddyPress (2.0.0)
*/
/**
* Invalidate 'all_for_user_' cache when saving.
*
* @since BuddyPress (2.0.0)
*
* @param BP_Notification_Notification $n Notification object.
*/
function bp_notifications_clear_all_for_user_cache_after_save( BP_Notifications_Notification $n ) {
wp_cache_delete( 'all_for_user_' . $n->user_id, 'bp_notifications' );
}
add_action( 'bp_notification_after_save', 'bp_notifications_clear_all_for_user_cache_after_save' );
/**
* Invalidate the 'all_for_user_' cache when deleting.
*
* @since BuddyPress (2.0.0)
*
* @param int $args Notification deletion arguments.
*/
function bp_notifications_clear_all_for_user_cache_before_delete( $args ) {
// Pull up a list of items matching the args (those about te be deleted)
$ns = BP_Notifications_Notification::get( $args );
$user_ids = array();
foreach ( $ns as $n ) {
$user_ids[] = $n->user_id;
}
foreach ( array_unique( $user_ids ) as $user_id ) {
wp_cache_delete( 'all_for_user_' . $user_id, 'bp_notifications' );
}
}
add_action( 'bp_notification_before_delete', 'bp_notifications_clear_all_for_user_cache_before_delete' );