Skip to:
Content

BuddyPress.org

Changeset 7956


Ignore:
Timestamp:
02/21/2014 05:51:10 PM (13 years ago)
Author:
boonebgorges
Message:

Overhaul the way that individual group objects are cached

The existing implementation was broken in a number of ways. Most critically,
user-specific data (like is_member) was being stored in the persistent object
cache without respect to user.

This refactor excludes the user-specific data from being cached along with the
group object. It also reworks the way that items are keyed in the cache, for
greater consistency with WordPress and the other BP components.

See #5407

Location:
trunk
Files:
6 edited

Legend:

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

    r7844 r7956  
    6565 */
    6666function bp_groups_delete_group_cache( $group_id = 0 ) {
    67         wp_cache_delete( 'bp_groups_group_' . $group_id . '_load_users', 'bp' );
    68         wp_cache_delete( 'bp_groups_group_' . $group_id . '_noload_users', 'bp' );
     67        wp_cache_delete( $group_id, 'bp_groups' );
    6968}
    7069add_action( 'groups_delete_group',     'bp_groups_delete_group_cache' );
    … …  
    7978 */
    8079function bp_groups_delete_group_cache_on_metadata_change( $meta_id, $group_id ) {
    81         wp_cache_delete( 'bp_groups_group_' . $group_id . '_load_users', 'bp' );
    82         wp_cache_delete( 'bp_groups_group_' . $group_id . '_noload_users', 'bp' );
     80        wp_cache_delete( $group_id, 'bp_groups' );
    8381}
    8482add_action( 'updated_group_meta', 'bp_groups_delete_group_cache_on_metadata_change', 10, 2 );
  • trunk/bp-groups/bp-groups-classes.php

    r7948 r7956  
    177177                global $wpdb, $bp;
    178178
    179                 if ( $group = $wpdb->get_row( $wpdb->prepare( "SELECT g.* FROM {$bp->groups->table_name} g WHERE g.id = %d", $this->id ) ) ) {
    180                         $this->id                 = $group->id;
    181                         $this->creator_id         = $group->creator_id;
    182                         $this->name               = stripslashes($group->name);
    183                         $this->slug               = $group->slug;
    184                         $this->description        = stripslashes($group->description);
    185                         $this->status             = $group->status;
    186                         $this->enable_forum       = $group->enable_forum;
    187                         $this->date_created       = $group->date_created;
     179                $group = wp_cache_get( $this->id, 'bp_groups' );
     180                if ( false === $group ) {
     181                        $group = $wpdb->get_row( $wpdb->prepare( "SELECT g.* FROM {$bp->groups->table_name} g WHERE g.id = %d", $this->id ) );
     182
     183                        if ( empty( $group ) ) {
     184                                $this->id = 0;
     185                                return;
     186                        }
     187
     188                        $this->id           = $group->id;
     189                        $this->creator_id   = $group->creator_id;
     190                        $this->name         = stripslashes( $group->name );
     191                        $this->slug         = $group->slug;
     192                        $this->description  = stripslashes( $group->description );
     193                        $this->status       = $group->status;
     194                        $this->enable_forum = $group->enable_forum;
     195                        $this->date_created = $group->date_created;
    188196
    189197                        if ( ! empty( $this->args['populate_extras'] ) ) {
    190198                                $this->last_activity      = groups_get_groupmeta( $this->id, 'last_activity' );
    191199                                $this->total_member_count = groups_get_groupmeta( $this->id, 'total_member_count' );
    192                                 $this->is_member          = BP_Groups_Member::check_is_member( bp_loggedin_user_id(), $this->id );
    193                                 $this->is_invited         = BP_Groups_Member::check_has_invite( bp_loggedin_user_id(), $this->id );
    194                                 $this->is_pending         = BP_Groups_Member::check_for_membership_request( bp_loggedin_user_id(), $this->id );
     200
     201                                // Get group admins and mods
     202                                $admin_mods = $wpdb->get_results( apply_filters( 'bp_group_admin_mods_user_join_filter', $wpdb->prepare( "SELECT u.ID as user_id, u.user_login, u.user_email, u.user_nicename, m.is_admin, m.is_mod FROM {$wpdb->users} u, {$bp->groups->table_name_members} m WHERE u.ID = m.user_id AND m.group_id = %d AND ( m.is_admin = 1 OR m.is_mod = 1 )", $this->id ) ) );
     203
     204                                foreach ( (array) $admin_mods as $user ) {
     205                                        if ( (int) $user->is_admin ) {
     206                                                $this->admins[] = $user;
     207                                        } else {
     208                                                $this->mods[] = $user;
     209                                        }
     210                                }
     211
     212                                // Cache general (non-user-specific)
     213                                // information about the group
     214                                wp_cache_set( $this->id, $this, 'bp_groups' );
     215                        }
     216
     217                        // Set user-specific data
     218                        if ( ! empty( $this->args['populate_extras'] ) ) {
     219                                $this->is_member  = BP_Groups_Member::check_is_member( bp_loggedin_user_id(), $this->id );
     220                                $this->is_invited = BP_Groups_Member::check_has_invite( bp_loggedin_user_id(), $this->id );
     221                                $this->is_pending = BP_Groups_Member::check_for_membership_request( bp_loggedin_user_id(), $this->id );
    195222
    196223                                // If this is a private or hidden group, does the current user have access?
    … …  
    203230                                        $this->user_has_access = true;
    204231                                }
    205 
    206                                 // Get group admins and mods
    207                                 $admin_mods = $wpdb->get_results( apply_filters( 'bp_group_admin_mods_user_join_filter', $wpdb->prepare( "SELECT u.ID as user_id, u.user_login, u.user_email, u.user_nicename, m.is_admin, m.is_mod FROM {$wpdb->users} u, {$bp->groups->table_name_members} m WHERE u.ID = m.user_id AND m.group_id = %d AND ( m.is_admin = 1 OR m.is_mod = 1 )", $this->id ) ) );
    208                                 foreach( (array) $admin_mods as $user ) {
    209                                         if ( (int) $user->is_admin )
    210                                                 $this->admins[] = $user;
    211                                         else
    212                                                 $this->mods[] = $user;
    213                                 }
    214232                        }
    215                 } else {
    216                         $this->id = 0;
    217233                }
    218234        }
    … …  
    289305                do_action_ref_array( 'groups_group_after_save', array( &$this ) );
    290306
    291                 wp_cache_delete( 'bp_groups_group_' . $this->id, 'bp' );
     307                wp_cache_delete( $this->id, 'bp_groups' );
    292308
    293309                return true;
    … …  
    317333                do_action_ref_array( 'bp_groups_delete_group', array( &$this, $user_ids ) );
    318334
    319                 wp_cache_delete( 'bp_groups_group_' . $this->id, 'bp' );
     335                wp_cache_delete( $this->id, 'bp_groups' );
    320336
    321337                // Finally remove the group entry from the DB
  • trunk/bp-groups/bp-groups-functions.php

    r7948 r7956  
    4141 */
    4242function groups_get_group( $args = '' ) {
    43         $defaults = array(
     43        $r = wp_parse_args( $args, array(
    4444                'group_id'          => false,
    4545                'load_users'        => false,
    4646                'populate_extras'   => true,
     47        ) );
     48
     49        $group_args = array(
     50                'populate_extras' => $r['populate_extras'],
    4751        );
    4852
    49         $args = wp_parse_args( $args, $defaults );
    50         extract( $args, EXTR_SKIP );
    51 
    52         $cache_key = 'bp_groups_group_' . $group_id . ( $load_users ? '_load_users' : '_noload_users' );
    53 
    54         if ( !$group = wp_cache_get( $cache_key, 'bp' ) ) {
    55                 $group_args = array(
    56                         'populate_extras'   => $populate_extras,
    57                 );
    58 
    59                 $group = new BP_Groups_Group( $group_id, $group_args );
    60                 wp_cache_set( $cache_key, $group, 'bp' );
    61         }
     53        $group = new BP_Groups_Group( $r['group_id'], $group_args );
    6254
    6355        return apply_filters( 'groups_get_group', $group );
  • trunk/tests/testcases/groups/cache.php

    r7843 r7956  
    7474
    7575                // Prime cache
    76                 groups_get_group( array( 'group_id' => $g ) );
     76                groups_get_group( array( 'group_id' => $g, ) );
    7777
    78                 $this->assertNotEmpty( wp_cache_get( 'bp_groups_group_' . $g . '_noload_users', 'bp' ) );
     78                $this->assertNotEmpty( wp_cache_get( $g, 'bp_groups' ) );
    7979
    8080                // Trigger flush
    8181                groups_update_groupmeta( $g, 'foo', 'bar' );
    8282
    83                 $this->assertFalse( wp_cache_get( 'bp_groups_group_' . $g . '_noload_users', 'bp' ) );
     83                $this->assertFalse( wp_cache_get( $g, 'bp_groups' ) );
    8484        }
    8585
    … …  
    9595                groups_get_group( array( 'group_id' => $g ) );
    9696
    97                 $this->assertNotEmpty( wp_cache_get( 'bp_groups_group_' . $g . '_noload_users', 'bp' ) );
     97                $this->assertNotEmpty( wp_cache_get( $g, 'bp_groups' ) );
    9898
    9999                // Trigger flush
    100100                groups_update_groupmeta( $g, 'foo', 'baz' );
    101                 $this->assertFalse( wp_cache_get( 'bp_groups_group_' . $g . '_noload_users', 'bp' ) );
     101                $this->assertFalse( wp_cache_get( $g, 'bp_groups' ) );
    102102        }
    103103
  • trunk/tests/testcases/groups/class-bp-groups-group.php

    r7938 r7956  
    644644
    645645        /**
     646         * @group delete
     647         * @group cache
     648         */
     649        public function test_delete_clear_cache() {
     650                $g = $this->factory->group->create();
     651
     652                // Prime cache
     653                groups_get_group( array( 'group_id' => $g, ) );
     654
     655                $this->assertNotEmpty( wp_cache_get( $g, 'bp_groups' ) );
     656
     657                $group = new BP_Groups_Group( $g );
     658                $group->delete();
     659
     660                $this->assertFalse( wp_cache_get( $g, 'bp_groups' ) );
     661        }
     662
     663        /**
     664         * @group save
     665         * @group cache
     666         */
     667        public function test_save_clear_cache() {
     668                $g = $this->factory->group->create();
     669
     670                // Prime cache
     671                groups_get_group( array( 'group_id' => $g, ) );
     672
     673                $this->assertNotEmpty( wp_cache_get( $g, 'bp_groups' ) );
     674
     675                $group = new BP_Groups_Group( $g );
     676                $group->name = 'Foo';
     677                $group->save();
     678
     679                $this->assertFalse( wp_cache_get( $g, 'bp_groups' ) );
     680        }
     681        /**
    646682         * @group get_group_extras
    647683         */
  • trunk/tests/testcases/groups/functions.php

    r7903 r7956  
    549549                $this->assertNotEmpty( groups_add_groupmeta( $g, 'foo', 'baz' ) );
    550550        }
     551
     552        /**
     553         * @group groups_get_group
     554         * @group cache
     555         */
     556        public function test_groups_get_group_cache_different_users() {
     557                $g = $this->factory->group->create();
     558                $u1 = $this->create_user();
     559                $u2 = $this->create_user();
     560                $this->add_user_to_group( $u1, $g );
     561
     562                $old_user = get_current_user_id();
     563                $this->set_current_user( $u1 );
     564
     565                $group1 = groups_get_group( array( 'group_id' => $g, ) );
     566
     567                $this->set_current_user( $u2 );
     568
     569                $group2 = groups_get_group( array( 'group_id' => $g, ) );
     570
     571                $this->assertNotEquals( $group1, $group2 );
     572
     573                $this->set_current_user( $old_user );
     574        }
    551575}
Note: See TracChangeset for help on using the changeset viewer.