[buddypress-trac] [BuddyPress Trac] #6327: Improved caching for group membership
noreply at wordpress.org
Mon Mar 30 16:11:04 UTC 2015
#6327: Improved caching for group membership
Reporter: boonebgorges | Owner:
Type: defect (bug) | Status: new
Priority: normal | Milestone: 2.3
Component: Component - Groups | Version:
Severity: normal | Resolution:
Keywords: has-patch |
Changes (by boonebgorges):
* keywords: => has-patch
[attachment:6327.patch] demonstrates what I have in mind. It does the
* Introduces `bp_get_user_groups()`. This function fetches data about a
user's group memberships in a cache-friendly way, and supports some
parameters for filtering the return results.
* Does the necessary cache invalidation. I added a few new hooks to
`BP_Groups_Member` to support this.
* Refactors the `groups_is_user_*()` functions to use the new function. I
also added a couple of missing functions: `groups_is_user_invited()`,
`groups_is_user_banned()`. These could be rewritten as a wrapper for
`groups_is_user_status()` or something like that.
* Refactors `BP_Groups_Group::get_group_extras()` to use the new function.
In this case, we save not only some database overhead, but we also save a
bunch of ugly complexity :)
* Unit tests for the lot.
This all works very well, and cuts down on database queries significantly,
especially when extending with certain types of plugins.
A couple of implementation decisions I made in the patch, which others
might want to comment on:
1. Cache granularity. I've chosen to cache an array of group membership
objects in `'user_groups_' . $user_id` (bucket 'bp_groups'). It would be
slightly more complicated, though maybe better in the long run (in terms
of reusable cache objects), to cache only the membership IDs in
`user_groups_$user_id`, and then to cache membership objects individually.
This is something that could be changed later on, of course.
2. The syntax of `bp_get_user_groups()` works like this: the `is_*`
parameters have default boolean values, and you can pass `null` to any of
them to disable the filters (ie, to return results that have either `true`
or `false` for these fields). We don't really use this syntax anywhere
else (we and WP tend to prefer a string like `'any'`), but I thought I'd
throw it out there, just for laughs.
3. I've only chosen a couple of places (`groups_is_user_*()` and
`get_group_extras()`) to use the new system, though there are a couple
other places where it could doubtless be used as well.
Ticket URL: <https://buddypress.trac.wordpress.org/ticket/6327#comment:4>
BuddyPress Trac <http://buddypress.org/>
More information about the buddypress-trac