Skip to:
Content

BuddyPress.org

Changeset 9534


Ignore:
Timestamp:
02/23/2015 01:39:52 AM (9 years ago)
Author:
boonebgorges
Message:

Use 'bp_member_member_type' as the member type cache bucket name.

Using 'bp_member_type' was creating the potential for collisions between WP's
taxonomy cache (which uses the taxonomy name 'bp_member_type' and term IDs as
cache keys) and BP's per-member member type cache (which uses the bucket
'bp_member_type' and user IDs as cache keys). The collisions take place only
when there is a 'bp_member_type' term ID that overlaps with a user ID.

The new cache group 'bp_member_member_type' is chosen to underscore that what's
being cached is the relationship between individual members and the user types
to which they belong.

Props imath, johnjamesjacoby.
Fixes #6242.

Location:
branches/2.2
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2/src/bp-members/bp-members-cache.php

    r9448 r9534  
    1515 */
    1616function bp_members_prefetch_member_type( BP_User_Query $bp_user_query ) {
    17     $uncached_member_ids = bp_get_non_cached_ids( $bp_user_query->user_ids, 'bp_member_type' );
     17    $uncached_member_ids = bp_get_non_cached_ids( $bp_user_query->user_ids, 'bp_member_member_type' );
    1818
    1919    $member_types = bp_get_object_terms( $uncached_member_ids, 'bp_member_type', array(
     
    3333    $cached_member_ids = array();
    3434    foreach ( $keyed_member_types as $user_id => $user_member_types ) {
    35         wp_cache_set( $user_id, $user_member_types, 'bp_member_type' );
     35        wp_cache_set( $user_id, $user_member_types, 'bp_member_member_type' );
    3636        $cached_member_ids[] = $user_id;
    3737    }
     
    3939    // Cache an empty value for users with no type.
    4040    foreach ( array_diff( $uncached_member_ids, $cached_member_ids ) as $no_type_id ) {
    41         wp_cache_set( $no_type_id, '', 'bp_member_type' );
     41        wp_cache_set( $no_type_id, '', 'bp_member_member_type' );
    4242    }
    4343}
     
    5454 */
    5555function bp_members_clear_member_type_cache( $user_id ) {
    56     wp_cache_delete( $user_id, 'bp_member_type' );
     56    wp_cache_delete( $user_id, 'bp_member_member_type' );
    5757}
    5858add_action( 'wpmu_delete_user', 'bp_members_clear_member_type_cache' );
  • branches/2.2/src/bp-members/bp-members-functions.php

    r9428 r9534  
    25842584    // Bust the cache if the type has been updated.
    25852585    if ( ! is_wp_error( $retval ) ) {
    2586         wp_cache_delete( $user_id, 'bp_member_type' );
     2586        wp_cache_delete( $user_id, 'bp_member_member_type' );
    25872587
    25882588        /**
     
    26132613 */
    26142614function bp_get_member_type( $user_id, $single = true ) {
    2615     $types = wp_cache_get( $user_id, 'bp_member_type' );
     2615    $types = wp_cache_get( $user_id, 'bp_member_member_type' );
    26162616
    26172617    if ( false === $types ) {
    2618         $types = bp_get_object_terms( $user_id, 'bp_member_type'  );
     2618        $types = bp_get_object_terms( $user_id, 'bp_member_type' );
    26192619
    26202620        if ( ! is_wp_error( $types ) ) {
    26212621            $types = wp_list_pluck( $types, 'name' );
    2622             wp_cache_set( $user_id, $types, 'bp_member_type' );
     2622            wp_cache_set( $user_id, $types, 'bp_member_member_type' );
    26232623        }
    26242624    }
  • branches/2.2/tests/phpunit/testcases/core/class-bp-user-query.php

    r9448 r9534  
    515515        ) );
    516516
    517         $this->assertSame( array( 'foo' ), wp_cache_get( $users[0], 'bp_member_type' ) );
    518         $this->assertSame( array( 'bar' ), wp_cache_get( $users[1], 'bp_member_type' ) );
    519         $this->assertSame( array( 'foo' ), wp_cache_get( $users[2], 'bp_member_type' ) );
    520         $this->assertSame( '', wp_cache_get( $users[3], 'bp_member_type' ) );
     517        $this->assertSame( array( 'foo' ), wp_cache_get( $users[0], 'bp_member_member_type' ) );
     518        $this->assertSame( array( 'bar' ), wp_cache_get( $users[1], 'bp_member_member_type' ) );
     519        $this->assertSame( array( 'foo' ), wp_cache_get( $users[2], 'bp_member_member_type' ) );
     520        $this->assertSame( '', wp_cache_get( $users[3], 'bp_member_member_type' ) );
    521521    }
    522522}
  • branches/2.2/tests/phpunit/testcases/members/types.php

    r9448 r9534  
    206206        $this->assertFalse( bp_get_member_type( $u ) );
    207207    }
     208
     209    /**
     210     * @group BP6242
     211     * @group cache
     212     */
     213    public function test_bp_get_member_type_should_not_conflict_with_term_cache() {
     214        global $wpdb;
     215
     216        // Offset IDs.
     217        $dummy_terms = $this->factory->tag->create_many( 5 );
     218
     219        $u1 = $this->factory->user->create();
     220        bp_register_member_type( 'foo' );
     221        bp_set_member_type( $u1, 'foo' );
     222
     223        // Fetch a term ID.
     224        $terms = get_terms( 'bp_member_type', array( 'hide_empty' => false, 'fields' => 'all' ) );
     225
     226        // Make sure the user's ID matches a term ID, to force a cache confusion.
     227        $u2 = $this->factory->user->create();
     228        $new_user_id = $terms[0]->term_id;
     229        $wpdb->update( $wpdb->users, array( 'ID' => $new_user_id ), array( 'ID' => $u2 ) );
     230
     231        bp_set_member_type( $new_user_id, 'foo' );
     232
     233        // Reprime the taxonomy cache.
     234        $terms = get_terms( 'bp_member_type', array( 'hide_empty' => false, 'fields' => 'all' ) );
     235
     236        $this->assertSame( 'foo', bp_get_member_type( $new_user_id, true ) );
     237    }
    208238}
Note: See TracChangeset for help on using the changeset viewer.