Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
09/13/2016 04:05:07 AM (10 years ago)
Author:
boonebgorges
Message:

Groups: Improve query efficiency for 'admins' and 'mods' properties of group objects.

Previously, the 'admins' and 'mods' property of BP_Groups_Group
objects were only populated when setting the 'populate_extras' flag.
Even then, the query used to populate these properties was uncached,
and required a join against a global table.

This changeset reworks the way that the 'admins' and 'mods' properties
are accessed and set. The properties are now marked protected, and
are accessible by magic __get(). When requested, the cache for the
both properties is set by a single pair of queries: one to fetch
membership data from the BP table, and one to get user objects from
WordPress. The BP table query is cached, and neither query takes place
if the property is never accessed.

This moves us a step closer to eliminating the populate_extras flag
on BP_Groups_Group objects.

See #5451.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/testcases/groups/cache.php

    r9819 r11087  
    182182
    183183    /**
     184     * @group groups_get_group_mods
     185     */
     186    public function test_groups_get_group_mods_cache() {
     187        $u1 = $this->factory->user->create();
     188        $u2 = $this->factory->user->create();
     189        $g = $this->factory->group->create( array( 'creator_id' => $u1 ) );
     190
     191        // User 2 joins the group
     192        groups_join_group( $g, $u2 );
     193
     194        // prime cache
     195        groups_get_group_mods( $g );
     196
     197        // promote user 2 to an admin
     198        bp_update_is_item_admin( true );
     199        groups_promote_member( $u2, $g, 'mod' );
     200
     201        // assert new cached value
     202        $this->assertEquals( 1, count( groups_get_group_mods( $g ) ) );
     203    }
     204
     205    /**
     206     * @group groups_get_group_mods
     207     */
     208    public function test_groups_get_group_mods_cache_on_member_save() {
     209        $u1 = $this->factory->user->create();
     210        $u2 = $this->factory->user->create();
     211        $g = $this->factory->group->create( array( 'creator_id' => $u1 ) );
     212
     213        // prime cache
     214        groups_get_group_mods( $g );
     215
     216        // promote user 2 to an admin via BP_Groups_Member::save()
     217        self::add_user_to_group( $u2, $g, array( 'is_mod' => 1 ) );
     218
     219        // assert new cached value
     220        $this->assertEquals( 1, count( groups_get_group_mods( $g ) ) );
     221    }
     222
     223    /**
    184224     * @group groups_get_group_admins
    185225     */
Note: See TracChangeset for help on using the changeset viewer.