Skip to:
Content

BuddyPress.org

Changeset 8537


Ignore:
Timestamp:
06/16/2014 08:19:57 PM (11 years ago)
Author:
r-a-y
Message:

Groups: Cache calls to BP_Groups_Member::get_group_administrator_ids()

When a logged-in user is viewing the group directory and is a member of
each group in the loop, when rendering the "Leave Group" button, a call
is done to fetch the group admins of each group to determine whether the
user is the sole admin.

Since this query is uncached, this could lead to twenty additional DB
queries being run on the page. Thus, this commit caches this query and
invalidates the cache whenever a group member is promoted / demoted or
saved into the database.

Fixes #5711.

Location:
trunk
Files:
3 edited

Legend:

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

    r8200 r8537  
    173173add_action( 'groups_uninvite_user', 'groups_clear_group_user_object_cache', 10, 2 );
    174174add_action( 'groups_remove_member', 'groups_clear_group_user_object_cache', 10, 2 );
     175
     176/**
     177 * Clear group administrator cache.
     178 *
     179 * @since BuddyPress (2.1.0)
     180 *
     181 * @param int $group_id The group ID.
     182 */
     183function groups_clear_group_administrator_cache( $group_id ) {
     184    wp_cache_delete( $group_id, 'bp_group_admins' );
     185}
     186add_action( 'groups_promote_member', 'groups_clear_group_administrator_cache' );
     187add_action( 'groups_demote_member',  'groups_clear_group_administrator_cache' );
     188add_action( 'groups_delete_group',   'groups_clear_group_administrator_cache' );
     189
     190/**
     191 * Clear group administrator cache when a group member is saved.
     192 *
     193 * This accounts for situations where group administrators are added manually
     194 * using {@link BP_Groups_Member::save()}.  Usually via a plugin.
     195 *
     196 * @since BuddyPress (2.1.0)
     197 *
     198 * @param BP_Groups_Member $member
     199 */
     200function groups_clear_group_administrator_cache_on_member_save( BP_Groups_Member $member ) {
     201    groups_clear_group_administrator_cache( $member->group_id );
     202}
     203add_action( 'groups_member_after_save', 'groups_clear_group_administrator_cache_on_member_save' );
    175204
    176205/* List actions to clear super cached pages on, if super cache is installed */
  • trunk/src/bp-groups/bp-groups-classes.php

    r8476 r8537  
    26272627        global $bp, $wpdb;
    26282628
    2629         return $wpdb->get_results( $wpdb->prepare( "SELECT user_id, date_modified FROM {$bp->groups->table_name_members} WHERE group_id = %d AND is_admin = 1 AND is_banned = 0", $group_id ) );
     2629        $group_admins = wp_cache_get( $group_id, 'bp_group_admins' );
     2630
     2631        if ( false === $group_admins ) {
     2632            $group_admins = $wpdb->get_results( $wpdb->prepare( "SELECT user_id, date_modified FROM {$bp->groups->table_name_members} WHERE group_id = %d AND is_admin = 1 AND is_banned = 0", $group_id ) );
     2633
     2634            wp_cache_set( $group_id, $group_admins, 'bp_group_admins' );
     2635        }
     2636
     2637        return $group_admins;
    26302638    }
    26312639
  • trunk/tests/phpunit/testcases/groups/cache.php

    r8224 r8537  
    155155        $this->assertNotEmpty( wp_cache_get( $g2, 'bp_groups' ) );
    156156    }
     157
     158    /**
     159     * @group groups_get_group_admins
     160     */
     161    public function test_groups_get_group_admins_cache() {
     162        $u1 = $this->create_user();
     163        $u2 = $this->create_user();
     164        $g = $this->factory->group->create( array( 'creator_id' => $u1 ) );
     165
     166        // prime cache
     167        groups_get_group_admins( $g );
     168
     169        // promote user 2 to an admin
     170        bp_update_is_item_admin( true );
     171        groups_promote_member( $u2, $g, 'admin' );
     172
     173        // assert that cache is invalidated
     174        $this->assertEmpty( wp_cache_get( $g, 'bp_group_admins' ) );
     175
     176        // assert new cached value
     177        $this->assertEquals( 2, count( groups_get_group_admins( $g ) ) );
     178    }
     179
     180    /**
     181     * @group groups_get_group_admins
     182     */
     183    public function test_groups_get_group_admins_cache_on_member_save() {
     184        $u1 = $this->create_user();
     185        $u2 = $this->create_user();
     186        $g = $this->factory->group->create( array( 'creator_id' => $u1 ) );
     187
     188        // prime cache
     189        groups_get_group_admins( $g );
     190
     191        // promote user 2 to an admin via BP_Groups_Member::save()
     192        self::add_user_to_group( $u2, $g, array( 'is_admin' => 1 ) );
     193
     194        // assert that cache is invalidated
     195        $this->assertEmpty( wp_cache_get( $g, 'bp_group_admins' ) );
     196
     197        // assert new cached value
     198        $this->assertEquals( 2, count( groups_get_group_admins( $g ) ) );
     199    }
    157200}
Note: See TracChangeset for help on using the changeset viewer.