Skip to:
Content

BuddyPress.org

Changeset 11072


Ignore:
Timestamp:
09/09/2016 03:30:07 PM (5 years ago)
Author:
boonebgorges
Message:

Cache the results of group ID and count queries.

Query results are cached using an incrementor, and are invalidated in
bulk whenever a group is created, updated, or deleted, or when
groupmeta is modified.

See #5451.

Location:
trunk
Files:
3 edited

Legend:

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

    r10819 r11072  
    253253add_action( 'bp_groups_member_before_delete_invite', 'bp_groups_clear_user_group_cache_on_other_events', 10, 2 );
    254254add_action( 'groups_accept_invite', 'bp_groups_clear_user_group_cache_on_other_events', 10, 2 );
     255
     256/**
     257 * Reset cache incrementor for the Groups component.
     258 *
     259 * This function invalidates all cached results of group queries,
     260 * whenever one of the following events takes place:
     261 *   - A group is created or updated.
     262 *   - A group is deleted.
     263 *   - A group's metadata is modified.
     264 *
     265 * @since 2.7.0
     266 *
     267 * @return bool True on success, false on failure.
     268 */
     269function bp_groups_reset_cache_incrementor() {
     270    return bp_core_reset_incrementor( 'bp_groups' );
     271}
     272add_action( 'groups_group_after_save', 'bp_groups_reset_cache_incrementor' );
     273add_action( 'bp_groups_delete_group',  'bp_groups_reset_cache_incrementor' );
     274add_action( 'updated_group_meta',      'bp_groups_reset_cache_incrementor' );
     275add_action( 'deleted_group_meta',      'bp_groups_reset_cache_incrementor' );
     276add_action( 'added_group_meta',        'bp_groups_reset_cache_incrementor' );
    255277
    256278/* List actions to clear super cached pages on, if super cache is installed */
  • trunk/src/bp-groups/classes/class-bp-groups-group.php

    r11071 r11072  
    824824        );
    825825
    826 
    827826        if ( ! empty( $r['user_id'] ) ) {
    828827            $sql['from'] .= " JOIN {$bp->groups->table_name_members} m ON ( g.id = m.group_id )";
     
    966965         */
    967966        $paged_groups_sql = apply_filters( 'bp_groups_get_paged_groups_sql', $paged_groups_sql, $sql, $r );
    968         $paged_group_ids  = $wpdb->get_col( $paged_groups_sql );
     967
     968        $cached = bp_core_get_incremented_cache( $paged_groups_sql, 'bp_groups' );
     969        if ( false === $cached ) {
     970            $paged_group_ids = $wpdb->get_col( $paged_groups_sql );
     971            bp_core_set_incremented_cache( $paged_groups_sql, 'bp_groups', $paged_group_ids );
     972        } else {
     973            $paged_group_ids = $cached;
     974        }
    969975
    970976        $uncached_group_ids = bp_get_non_cached_ids( $paged_group_ids, 'bp_groups' );
     
    9941000         */
    9951001        $total_groups_sql = apply_filters( 'bp_groups_get_total_groups_sql', $total_groups_sql, $sql, $r );
    996         $total_groups     = (int) $wpdb->get_var( $total_groups_sql );
     1002
     1003        $cached = bp_core_get_incremented_cache( $total_groups_sql, 'bp_groups' );
     1004        if ( false === $cached ) {
     1005            $total_groups = (int) $wpdb->get_var( $total_groups_sql );
     1006            bp_core_set_incremented_cache( $total_groups_sql, 'bp_groups', $total_groups );
     1007        } else {
     1008            $total_groups = (int) $cached;
     1009        }
    9971010
    9981011        $group_ids = array();
  • trunk/tests/phpunit/testcases/groups/class-bp-groups-group.php

    r11071 r11072  
    508508    }
    509509
     510    /**
     511     * @group cache
     512     * @ticket BP5451
     513     * @ticket BP6643
     514     */
     515    public function test_get_queries_should_be_cached() {
     516        global $wpdb;
     517
     518        $g = $this->factory->group->create();
     519
     520        $found1 = BP_Groups_Group::get();
     521
     522        $num_queries = $wpdb->num_queries;
     523
     524        $found2 = BP_Groups_Group::get();
     525
     526        $this->assertEqualSets( $found1, $found2 );
     527        $this->assertSame( $num_queries, $wpdb->num_queries );
     528    }
     529
     530    /**
     531     * @group cache
     532     * @ticket BP5451
     533     * @ticket BP6643
     534     */
     535    public function test_get_query_caches_should_be_busted_by_groupmeta_update() {
     536        global $wpdb;
     537
     538        $groups = $this->factory->group->create_many( 2 );
     539        groups_update_groupmeta( $groups[0], 'foo', 'bar' );
     540        groups_update_groupmeta( $groups[1], 'foo', 'bar' );
     541
     542        $found1 = BP_Groups_Group::get( array(
     543            'meta_query' => array(
     544                array(
     545                    'key' => 'foo',
     546                    'value' => 'bar',
     547                ),
     548            ),
     549        ) );
     550
     551        $this->assertEqualSets( array( $groups[0], $groups[1] ), wp_list_pluck( $found1['groups'], 'id' ) );
     552
     553        groups_update_groupmeta( $groups[1], 'foo', 'baz' );
     554
     555        $found2 = BP_Groups_Group::get( array(
     556            'meta_query' => array(
     557                array(
     558                    'key' => 'foo',
     559                    'value' => 'bar',
     560                ),
     561            ),
     562        ) );
     563
     564        $this->assertEqualSets( array( $groups[0] ), wp_list_pluck( $found2['groups'], 'id' ) );
     565    }
     566
     567    /**
     568     * @group cache
     569     * @ticket BP5451
     570     * @ticket BP6643
     571     */
     572    public function test_get_query_caches_should_be_busted_by_group_save() {
     573        global $wpdb;
     574
     575        $groups = $this->factory->group->create_many( 2 );
     576        groups_update_groupmeta( $groups[0], 'foo', 'bar' );
     577        groups_update_groupmeta( $groups[1], 'foo', 'bar' );
     578
     579        $found1 = BP_Groups_Group::get( array(
     580            'search_terms' => 'Foo',
     581        ) );
     582
     583        $this->assertEmpty( $found1['groups'] );
     584
     585        $group0 = groups_get_group( array( 'group_id' => $groups[0] ) );
     586        $group0->name = 'Foo';
     587        $group0->save();
     588
     589        $found2 = BP_Groups_Group::get( array(
     590            'search_terms' => 'Foo',
     591        ) );
     592
     593        $this->assertEqualSets( array( $groups[0] ), wp_list_pluck( $found2['groups'], 'id' ) );
     594    }
     595
     596    /**
     597     * @group cache
     598     * @ticket BP5451
     599     * @ticket BP6643
     600     */
     601    public function test_get_query_caches_should_be_busted_by_group_delete() {
     602        global $wpdb;
     603
     604        $groups = $this->factory->group->create_many( 2 );
     605
     606        $found1 = BP_Groups_Group::get();
     607
     608        $this->assertEqualSets( $groups, wp_list_pluck( $found1['groups'], 'id' ) );
     609
     610        $group0 = groups_get_group( array( 'group_id' => $groups[0] ) );
     611        $group0->delete();
     612
     613        $found2 = BP_Groups_Group::get();
     614
     615        $this->assertEqualSets( array( $groups[1] ), wp_list_pluck( $found2['groups'], 'id' ) );
     616    }
     617
    510618    /** convert_type_to_order_orderby() **********************************/
    511619
Note: See TracChangeset for help on using the changeset viewer.