Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
05/21/2016 02:16:38 AM (10 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/tests/phpunit/testcases/groups/class-bp-groups-member.php

    r10687 r10794  
    12691269        $this->assertInternalType( 'int', BP_Groups_Member::total_group_count( 123 ) );
    12701270    }
     1271
     1272    /**
     1273     * @group get_memberships_by_id
     1274     */
     1275    public function test_get_memberships_by_id_with_single_id() {
     1276        $users = $this->factory->user->create_many( 2 );
     1277        $groups = $this->factory->group->create_many( 2 );
     1278
     1279        $m0 = $this->add_user_to_group( $users[0], $groups[0] );
     1280        $m1 = $this->add_user_to_group( $users[1], $groups[1] );
     1281
     1282        $found = BP_Groups_Member::get_memberships_by_id( $m0 );
     1283
     1284        $this->assertSame( 1, count( $found ) );
     1285        $this->assertEquals( $m0, $found[0]->id );
     1286    }
     1287
     1288    /**
     1289     * @group get_memberships_by_id
     1290     */
     1291    public function test_get_memberships_by_id_with_multiple_ids() {
     1292        $users = $this->factory->user->create_many( 2 );
     1293        $groups = $this->factory->group->create_many( 2 );
     1294
     1295        $m0 = $this->add_user_to_group( $users[0], $groups[0] );
     1296        $m1 = $this->add_user_to_group( $users[1], $groups[1] );
     1297
     1298        $found = BP_Groups_Member::get_memberships_by_id( array( $m0, $m1 ) );
     1299
     1300        $this->assertSame( 2, count( $found ) );
     1301        $this->assertEqualSets( array( $m0, $m1 ), wp_list_pluck( $found, 'id' ) );
     1302    }
    12711303}
Note: See TracChangeset for help on using the changeset viewer.