Make WordPress Core

Changeset 55657

Timestamp:
04/18/2023 11:48:46 AM (16 months ago)
Author:
spacedmonkey
Message:

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.

Location:
trunk
Files:
1 added
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-user-query.php

    r55654 r55657  
    120120            'login__in'           => array(),
    121121            'login__not_in'       => array(),
     122
    122123        );
    123124
     
    141142     * @since 5.3.0 Introduced the 'meta_type_key' parameter.
    142143     * @since 5.9.0 Added 'capability', 'capability__in', and 'capability__not_in' parameters.
     144
    143145     *
    144146     * @global wpdb     $wpdb     WordPress database abstraction object.
     
    255257     *     @type string[]        $login__not_in       An array of logins to exclude. Users matching one of these
    256258     *                                                logins will not be included in results. Default empty array.
     259
    257260     * }
    258261     */
     
    790793
    791794        $qv =& $this->query_vars;
     795
     796
     797
     798
     799
    792800
    793801        /**
     
    817825                {$this->query_limit}
    818826            ";
    819 
    820             if ( is_array( $qv['fields'] ) ) {
    821                 $this->results = $wpdb->get_results( $this->request );
     827            $cache_value   = false;
     828            $cache_key     = $this->generate_cache_key( $qv, $this->request );
     829            $cache_group   = 'users-queries';
     830            if ( $qv['cache_results'] ) {
     831                $cache_value = wp_cache_get( $cache_key, $cache_group );
     832            }
     833            if ( false !== $cache_value ) {
     834                $this->results     = $cache_value['user_data'];
     835                $this->total_users = $cache_value['total_users'];
    822836            } else {
    823                 $this->results = $wpdb->get_col( $this->request );
    824             }
    825 
    826             if ( isset( $qv['count_total'] ) && $qv['count_total'] ) {
    827                 /**
    828                  * Filters SELECT FOUND_ROWS() query for the current WP_User_Query instance.
    829                  *
    830                  * @since 3.2.0
    831                  * @since 5.1.0 Added the `$this` parameter.
    832                  *
    833                  * @global wpdb $wpdb WordPress database abstraction object.
    834                  *
    835                  * @param string        $sql   The SELECT FOUND_ROWS() query for the current WP_User_Query.
    836                  * @param WP_User_Query $query The current WP_User_Query instance.
    837                  */
    838                 $found_users_query = apply_filters( 'found_users_query', 'SELECT FOUND_ROWS()', $this );
    839 
    840                 $this->total_users = (int) $wpdb->get_var( $found_users_query );
     837
     838                if ( is_array( $qv['fields'] ) ) {
     839                    $this->results = $wpdb->get_results( $this->request );
     840                } else {
     841                    $this->results = $wpdb->get_col( $this->request );
     842                }
     843
     844                if ( isset( $qv['count_total'] ) && $qv['count_total'] ) {
     845                    /**
     846                     * Filters SELECT FOUND_ROWS() query for the current WP_User_Query instance.
     847                     *
     848                     * @since 3.2.0
     849                     * @since 5.1.0 Added the `$this` parameter.
     850                     *
     851                     * @global wpdb $wpdb WordPress database abstraction object.
     852                     *
     853                     * @param string        $sql   The SELECT FOUND_ROWS() query for the current WP_User_Query.
     854                     * @param WP_User_Query $query The current WP_User_Query instance.
     855                     */
     856                    $found_users_query = apply_filters( 'found_users_query', 'SELECT FOUND_ROWS()', $this );
     857
     858                    $this->total_users = (int) $wpdb->get_var( $found_users_query );
     859                }
     860
     861                if ( $qv['cache_results'] ) {
     862                    $cache_value = array(
     863                        'user_data'   => $this->results,
     864                        'total_users' => $this->total_users,
     865                    );
     866                    wp_cache_add( $cache_key, $cache_value, $cache_group );
     867                }
    841868            }
    842869        }
     
    10121039
    10131040    /**
     1041
     1042
     1043
     1044
     1045
     1046
     1047
     1048
     1049
     1050
     1051
     1052
     1053
     1054
     1055
     1056
     1057
     1058
     1059
     1060
     1061
     1062
     1063
     1064
     1065
     1066
     1067
     1068
     1069
     1070
     1071
     1072
     1073
     1074
     1075
     1076
     1077
     1078
     1079
     1080
     1081
     1082
     1083
     1084
     1085
     1086
     1087
     1088
    10141089     * Parses an 'order' query variable and casts it to ASC or DESC as necessary.
    10151090     *
  • trunk/src/wp-includes/default-filters.php

    r55620 r55657  
    115115add_action( 'updated_post_meta', 'wp_cache_set_posts_last_changed' );
    116116add_action( 'deleted_post_meta', 'wp_cache_set_posts_last_changed' );
     117
     118
     119
     120
     121
     122
     123
     124
    117125
    118126// Term meta.
  • trunk/src/wp-includes/load.php

    r55652 r55657  
    777777                'user_meta',
    778778                'userslugs',
     779
    779780            )
    780781        );
  • trunk/src/wp-includes/ms-blogs.php

    r55537 r55657  
    574574                        'user_meta',
    575575                        'userslugs',
     576
    576577                    )
    577578                );
     
    667668                        'user_meta',
    668669                        'userslugs',
     670
    669671                    )
    670672                );
  • trunk/src/wp-includes/ms-functions.php

    r55642 r55657  
    296296    }
    297297
     298
    298299    restore_current_blog();
    299300
  • trunk/src/wp-includes/user.php

    r55584 r55657  
    19081908
    19091909    wp_cache_delete( $user->ID, 'user_meta' );
     1910
    19101911
    19111912    /**
     
    50175018    );
    50185019}
     5020
     5021
     5022
     5023
     5024
     5025
     5026
     5027
     5028
  • trunk/tests/phpunit/includes/abstract-testcase.php

    r55537 r55657  
    413413                'user_meta',
    414414                'userslugs',
     415
    415416            )
    416417        );
Note: See TracChangeset for help on using the changeset viewer.