Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
03/03/2014 08:17:19 PM (10 years ago)
Author:
boonebgorges
Message:

Cache userdata after running WP_User_Query in BP_User_Query

WP_User_Query does not cache data that is stored in wp_users in a bulk fashion.
BP, on the other hand, does cache this data, as 'bp_core_userdata_' . $user_id.
This cached userdata is then referenced in many places throughout BP, including
the building of user_domains and userlinks. Thus, it can save up to 20 queries
per page (assuming per_page=20) during query loops.

A corresponding change has been made to the 'fields' parameter passed to
WP_User_Query from BP_User_Query. Because BP caches entire wp_users rows (see
BP_Core_User::get_core_userdata()), we must ensure that BP_User_Query is also
fetching entire rows. We opt not to use WP_User_Query's 'fields=all' parameter,
because it triggers the casting of WP_User objects, which in turn requires
expensive capability lookups.

See #5445

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-core/bp-core-classes.php

    r7993 r8024  
    450450
    451451            // Relevant
    452             'fields'      => array( 'ID', 'user_registered', 'user_login', 'user_nicename', 'display_name', 'user_email' ),
     452            'fields'      => array( 'ID', 'user_login', 'user_pass', 'user_nicename', 'user_email', 'user_url', 'user_registered', 'user_activation_key', 'user_status', 'display_name', 'spam', 'deleted' ),
    453453            'include'     => $this->user_ids,
    454454
     
    458458
    459459        ), $this ) );
     460
     461        // WP_User_Query doesn't cache the data it pulls from wp_users,
     462        // and it does not give us a way to save queries by fetching
     463        // only uncached users. However, BP does cache this data, so
     464        // we set it here.
     465        foreach ( $wp_user_query->results as $u ) {
     466            wp_cache_set( 'bp_core_userdata_' . $u->ID, $u, 'bp' );
     467        }
    460468
    461469        // We calculate total_users using a standalone query, except
Note: See TracChangeset for help on using the changeset viewer.