WordPress.org

Make WordPress Core

Opened 7 months ago

Last modified 2 months ago

#40613 new defect (bug)

Add query cache to WP_User_Query class

Reported by: johnjamesjacoby Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 3.1
Component: Users Keywords: has-patch needs-testing
Focuses: performance Cc:

Description (last modified by johnjamesjacoby)

When calling get_users() the query itself is not cached. If multiple calls to get_users() with the same $args array occur, each will result in a database hit.

Note that each individual user object is cached, and that query caches will need to be invalidated when users are added/removed/edited.

Theoretically, on busy sites with many users who are constantly changing passwords and things, caching user queries may not yield a significant performance improvement. For the majority of installations (where users are not shifted around nearly as much) this could result in several additional cache hits per page load (especially with persistent caches in place.)

Functions like wp_dropdown_users() are called multiple times when viewing the Posts list-table, and many plugins also call get_users() knowing that user objects are cached (but not noticing the additional database hits for each call itself.)

Membership plugins like BuddyPress would have inherent benefits, and bbPress could operate more efficiently when querying for the intersection of many topics with many users who are engaged in them.

Patch imminent.

Attachments (2)

40613.patch (3.1 KB) - added by johnjamesjacoby 7 months ago.
40613.diff (6.9 KB) - added by spacedmonkey 2 months ago.

Download all attachments as: .zip

Change History (6)

#1 @johnjamesjacoby
7 months ago

  • Description modified (diff)

#2 @spacedmonkey
2 months ago

#41847 was marked as a duplicate.

@spacedmonkey
2 months ago

#3 @spacedmonkey
2 months ago

  • Keywords has-patch needs-testing added
  • Version set to 3.1

My first patch adds basic caching. It add cache invalidation for user meta, adding users to blogs and CRUD functions for users.

This changes all queries to be ids, get the whole user object. Then for fields queries, just pick off the fields out of the WP_User object that you want.

Other big change is to the cache_users function. This function is very similar to _prime_<type>_cache function. I have added new param to cache_users of $update_meta_cache to optionally prime user meta caches.

Note: See TracTickets for help on using tickets.