mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-12 04:40:26 +00:00
Users: Cache database queries within WP_User_Query class.
Cache the results of database queries within `WP_User_Query` class. Only cache queries that are requesting 3 or less fields so that caches are not storing full user objects. Cache results are stored in a new global cache group named `users-queries`. Add a new parameter to `WP_User_Query` called `cache_results` to allow developers to opt out of a receiving cached results. `cache_results` parameter defaults to true. Also add a new helper function called `wp_cache_set_users_last_changed`, similar to `wp_cache_set_posts_last_changed` that incroments last changed value in cache group `users`. Ensure that `wp_cache_set_users_last_changed` is called whenever user / user meta is modified for proper cache invalidation. Props johnjamesjacoby, spacedmonkey, westi, dd32, strategio, srikanthmeenakshi, OllieJones, khoipro, rjasdfiii, flixos90, mukesh27, peterwilsoncc. Fixes #40613. git-svn-id: https://develop.svn.wordpress.org/trunk@55657 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -119,6 +119,7 @@ class WP_User_Query {
|
||||
'login' => '',
|
||||
'login__in' => array(),
|
||||
'login__not_in' => array(),
|
||||
'cache_results' => true,
|
||||
);
|
||||
|
||||
return wp_parse_args( $args, $defaults );
|
||||
@@ -140,6 +141,7 @@ class WP_User_Query {
|
||||
* @since 5.1.0 Introduced the 'meta_compare_key' parameter.
|
||||
* @since 5.3.0 Introduced the 'meta_type_key' parameter.
|
||||
* @since 5.9.0 Added 'capability', 'capability__in', and 'capability__not_in' parameters.
|
||||
* @since 6.3.0 Added 'cache_results' parameter.
|
||||
*
|
||||
* @global wpdb $wpdb WordPress database abstraction object.
|
||||
* @global WP_Roles $wp_roles WordPress role management object.
|
||||
@@ -254,6 +256,7 @@ class WP_User_Query {
|
||||
* logins will be included in results. Default empty array.
|
||||
* @type string[] $login__not_in An array of logins to exclude. Users matching one of these
|
||||
* logins will not be included in results. Default empty array.
|
||||
* @type bool $cache_results Whether to cache user information. Default true.
|
||||
* }
|
||||
*/
|
||||
public function prepare_query( $query = array() ) {
|
||||
@@ -790,6 +793,11 @@ class WP_User_Query {
|
||||
|
||||
$qv =& $this->query_vars;
|
||||
|
||||
// Do not cache results if more than 3 fields are requested.
|
||||
if ( is_array( $qv['fields'] ) && count( $qv['fields'] ) > 3 ) {
|
||||
$qv['cache_results'] = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the users array before the query takes place.
|
||||
*
|
||||
@@ -816,28 +824,47 @@ class WP_User_Query {
|
||||
{$this->query_orderby}
|
||||
{$this->query_limit}
|
||||
";
|
||||
|
||||
if ( is_array( $qv['fields'] ) ) {
|
||||
$this->results = $wpdb->get_results( $this->request );
|
||||
} else {
|
||||
$this->results = $wpdb->get_col( $this->request );
|
||||
$cache_value = false;
|
||||
$cache_key = $this->generate_cache_key( $qv, $this->request );
|
||||
$cache_group = 'users-queries';
|
||||
if ( $qv['cache_results'] ) {
|
||||
$cache_value = wp_cache_get( $cache_key, $cache_group );
|
||||
}
|
||||
if ( false !== $cache_value ) {
|
||||
$this->results = $cache_value['user_data'];
|
||||
$this->total_users = $cache_value['total_users'];
|
||||
} else {
|
||||
|
||||
if ( isset( $qv['count_total'] ) && $qv['count_total'] ) {
|
||||
/**
|
||||
* Filters SELECT FOUND_ROWS() query for the current WP_User_Query instance.
|
||||
*
|
||||
* @since 3.2.0
|
||||
* @since 5.1.0 Added the `$this` parameter.
|
||||
*
|
||||
* @global wpdb $wpdb WordPress database abstraction object.
|
||||
*
|
||||
* @param string $sql The SELECT FOUND_ROWS() query for the current WP_User_Query.
|
||||
* @param WP_User_Query $query The current WP_User_Query instance.
|
||||
*/
|
||||
$found_users_query = apply_filters( 'found_users_query', 'SELECT FOUND_ROWS()', $this );
|
||||
if ( is_array( $qv['fields'] ) ) {
|
||||
$this->results = $wpdb->get_results( $this->request );
|
||||
} else {
|
||||
$this->results = $wpdb->get_col( $this->request );
|
||||
}
|
||||
|
||||
$this->total_users = (int) $wpdb->get_var( $found_users_query );
|
||||
if ( isset( $qv['count_total'] ) && $qv['count_total'] ) {
|
||||
/**
|
||||
* Filters SELECT FOUND_ROWS() query for the current WP_User_Query instance.
|
||||
*
|
||||
* @since 3.2.0
|
||||
* @since 5.1.0 Added the `$this` parameter.
|
||||
*
|
||||
* @global wpdb $wpdb WordPress database abstraction object.
|
||||
*
|
||||
* @param string $sql The SELECT FOUND_ROWS() query for the current WP_User_Query.
|
||||
* @param WP_User_Query $query The current WP_User_Query instance.
|
||||
*/
|
||||
$found_users_query = apply_filters( 'found_users_query', 'SELECT FOUND_ROWS()', $this );
|
||||
|
||||
$this->total_users = (int) $wpdb->get_var( $found_users_query );
|
||||
}
|
||||
|
||||
if ( $qv['cache_results'] ) {
|
||||
$cache_value = array(
|
||||
'user_data' => $this->results,
|
||||
'total_users' => $this->total_users,
|
||||
);
|
||||
wp_cache_add( $cache_key, $cache_value, $cache_group );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1010,6 +1037,54 @@ class WP_User_Query {
|
||||
return $_orderby;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate cache key.
|
||||
*
|
||||
* @since 6.3.0
|
||||
*
|
||||
* @global wpdb $wpdb WordPress database abstraction object.
|
||||
*
|
||||
* @param array $args Query arguments.
|
||||
* @param string $sql SQL statement.
|
||||
* @return string Cache key.
|
||||
*/
|
||||
protected function generate_cache_key( array $args, $sql ) {
|
||||
global $wpdb;
|
||||
|
||||
// Replace wpdb placeholder in the SQL statement used by the cache key.
|
||||
$sql = $wpdb->remove_placeholder_escape( $sql );
|
||||
|
||||
$key = md5( $sql );
|
||||
$last_changed = wp_cache_get_last_changed( 'users' );
|
||||
|
||||
if ( empty( $args['orderby'] ) ) {
|
||||
// Default order is by 'user_login'.
|
||||
$ordersby = array( 'user_login' => '' );
|
||||
} elseif ( is_array( $args['orderby'] ) ) {
|
||||
$ordersby = $args['orderby'];
|
||||
} else {
|
||||
// 'orderby' values may be a comma- or space-separated list.
|
||||
$ordersby = preg_split( '/[,\s]+/', $args['orderby'] );
|
||||
}
|
||||
|
||||
$blog_id = 0;
|
||||
if ( isset( $args['blog_id'] ) ) {
|
||||
$blog_id = absint( $args['blog_id'] );
|
||||
}
|
||||
if ( ( $args['has_published_posts'] && $blog_id ) || in_array( 'post_count', $ordersby, true ) ) {
|
||||
$switch = get_current_blog_id() !== $blog_id;
|
||||
if ( $switch ) {
|
||||
switch_to_blog( $blog_id );
|
||||
}
|
||||
$last_changed .= wp_cache_get_last_changed( 'posts' );
|
||||
if ( $switch ) {
|
||||
restore_current_blog();
|
||||
}
|
||||
}
|
||||
|
||||
return "get_users:$key:$last_changed";
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses an 'order' query variable and casts it to ASC or DESC as necessary.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user