Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
02/21/2014 05:51:10 PM (10 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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.