Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
05/21/2016 02:16:38 AM (9 years ago)
Author:
boonebgorges
Message:

Introduce caching for group memberships.

The new system works like this: The bp_groups_memberships_for_user cache
group stores arrays of membership IDs for individual users. The
bp_groups_memberships cache group stores data about individual memberships.
The new function bp_get_user_groups() populates a user's group memberships
from these caches, and filters them as requested in the function parameters.
Then, the various groups_is_user_*() functions use bp_get_user_groups()
instead of direct, uncached database queries to fetch their data.

In addition, the get_group_extras() method of BP_Groups_Group can now be
greatly simplified, since all necessary pre-fetching of current-user group
memberships happens via the bp_get_user_groups() cache.

Props boonebgorges, dcavins.
See #6327.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-groups/bp-groups-cache.php

    r10766 r10794  
    226226}
    227227add_action( 'groups_delete_group', 'groups_clear_group_type_cache' );
     228
     229/**
     230 * Clear caches on membership save.
     231 *
     232 * @since 2.6.0
     233 */
     234function bp_groups_clear_user_group_cache_on_membership_save( BP_Groups_Member $member ) {
     235    wp_cache_delete( $member->user_id, 'bp_groups_memberships_for_user' );
     236    wp_cache_delete( $member->id, 'bp_groups_memberships' );
     237}
     238add_action( 'groups_member_before_save', 'bp_groups_clear_user_group_cache_on_membership_save' );
     239add_action( 'groups_member_before_remove', 'bp_groups_clear_user_group_cache_on_membership_save' );
     240
     241/**
     242 * Clear group memberships cache on miscellaneous actions not covered by the 'after_save' hook.
     243 *
     244 * @since 2.6.0
     245 */
     246function bp_groups_clear_user_group_cache_on_other_events( $user_id, $group_id ) {
     247    wp_cache_delete( $user_id, 'bp_groups_memberships_for_user' );
     248
     249    $membership = new BP_Groups_Member( $user_id, $group_id );
     250    wp_cache_delete( $membership->id, 'bp_groups_memberships' );
     251}
     252add_action( 'bp_groups_member_before_delete', 'bp_groups_clear_user_group_cache_on_other_events', 10, 2 );
     253add_action( 'bp_groups_member_before_delete_invite', 'bp_groups_clear_user_group_cache_on_other_events', 10, 2 );
    228254
    229255/* List actions to clear super cached pages on, if super cache is installed */
Note: See TracChangeset for help on using the changeset viewer.