Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
05/21/2016 02:16:38 AM (4 years ago)
Author:
boonebgorges
Message:

Introduce caching for group memberships.

The new system works like this: The bp_groups_memberships_for_user cache
group stores arrays of membership IDs for individual users. The
bp_groups_memberships cache group stores data about individual memberships.
The new function bp_get_user_groups() populates a user's group memberships
from these caches, and filters them as requested in the function parameters.
Then, the various groups_is_user_*() functions use bp_get_user_groups()
instead of direct, uncached database queries to fetch their data.

In addition, the get_group_extras() method of BP_Groups_Group can now be
greatly simplified, since all necessary pre-fetching of current-user group
memberships happens via the bp_get_user_groups() cache.

Props boonebgorges, dcavins.
See #6327.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-groups/classes/class-bp-groups-member.php

    r10487 r10794  
    822822        }
    823823
     824        /**
     825         * Fires before a group invitation is deleted.
     826         *
     827         * @since 2.6.0
     828         *
     829         * @param int $user_id  ID of the user.
     830         * @param int $group_id ID of the group.
     831         */
     832        do_action( 'bp_groups_member_before_delete_invite', $user_id, $group_id );
     833
    824834        $table_name = buddypress()->groups->table_name_members;
    825835
     
    10511061
    10521062        return $wpdb->get_results( $wpdb->prepare( "SELECT user_id, date_modified FROM {$bp->groups->table_name_members} WHERE group_id = %d AND is_mod = 1 AND is_banned = 0", $group_id ) );
     1063    }
     1064
     1065    /**
     1066     * Get group membership objects by ID (or an array of IDs).
     1067     *
     1068     * @since 2.6.0
     1069     *
     1070     * @param int|string|array $membership_ids Single membership ID or comma-separated/array list of membership IDs.
     1071     * @return array
     1072     */
     1073    public static function get_memberships_by_id( $membership_ids ) {
     1074        global $wpdb;
     1075
     1076        $bp = buddypress();
     1077
     1078        $membership_ids = implode( ',', wp_parse_id_list( $membership_ids ) );
     1079        return $wpdb->get_results( "SELECT * FROM {$bp->groups->table_name_members} WHERE id IN ({$membership_ids})" );
    10531080    }
    10541081
     
    11621189
    11631190    /**
     1191     * Get all membership IDs for a user.
     1192     *
     1193     * @since 2.6.0
     1194     *
     1195     * @param int $user_id ID of the user.
     1196     * @return array
     1197     */
     1198    public static function get_membership_ids_for_user( $user_id ) {
     1199        global $wpdb;
     1200
     1201        $bp = buddypress();
     1202
     1203        $group_ids = $wpdb->get_col( $wpdb->prepare( "SELECT id FROM {$bp->groups->table_name_members} WHERE user_id = %d ORDER BY id ASC", $user_id ) );
     1204
     1205        return $group_ids;
     1206    }
     1207
     1208    /**
    11641209     * Delete all memberships for a given group.
    11651210     *
Note: See TracChangeset for help on using the changeset viewer.