WordPress.org

Make WordPress Core

Ticket #42252: 42252.diff

File 42252.diff, 6.0 KB (added by flixos90, 3 years ago)
  • src/wp-includes/class-wp-site-query.php

     
    238238                 */
    239239                do_action_ref_array( 'pre_get_sites', array( &$this ) );
    240240
    241                 // $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
    242                 $_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) );
    243 
    244                 // Ignore the $fields argument as the queried result will be the same regardless.
    245                 unset( $_args['fields'] );
     241                $cache_key = $this->get_cache_key( $this->query_vars );
    246242
    247                 $key = md5( serialize( $_args ) );
    248                 $last_changed = wp_cache_get_last_changed( 'sites' );
    249 
    250                 $cache_key = "get_sites:$key:$last_changed";
    251243                $cache_value = wp_cache_get( $cache_key, 'sites' );
    252244
    253245                if ( false === $cache_value ) {
     
    696688                        return 'DESC';
    697689                }
    698690        }
     691
     692        /**
     693         * Generates the cache key to lookup query results for a specific set of arguments.
     694         *
     695         * @since 5.0.0
     696         *
     697         * @param array $query_vars Array of WP_Site_Query arguments. See WP_Site_Query::__construct().
     698         * @return string Cache key to use for the lookup.
     699         */
     700        protected function get_cache_key( $query_vars ) {
     701                // $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
     702                $_args = wp_array_slice_assoc( $query_vars, array_keys( $this->query_var_defaults ) );
     703
     704                // Ignore the $fields argument as the queried result will be the same regardless.
     705                unset( $_args['fields'] );
     706
     707                // Ignore the $update_site_cache as it does not affect the query.
     708                unset( $_args['update_site_cache'] );
     709
     710                // Use the same cache key for array lookups with one element and single value lookups.
     711                $single_multi_mappings = array(
     712                        'ID'         => 'site__in',
     713                        'network_id' => 'network__in',
     714                        'domain'     => 'domain__in',
     715                        'path'       => 'path__in',
     716                        'lang_id'    => 'lang__in',
     717                );
     718                foreach ( $single_multi_mappings as $single_var => $multi_var ) {
     719                        if ( empty( $_args[ $single_var ] ) && is_array( $_args[ $multi_var ] ) && count( $_args[ $multi_var ] ) === 1 ) {
     720                                $_args[ $single_var ] = array_pop( $_args[ $multi_var ] );
     721                                $_args[ $multi_var ] = '';
     722                        }
     723                }
     724
     725                $key = md5( serialize( $_args ) );
     726
     727                /* The following part of the code checks some common combinations of query vars.
     728                 * If all non-empty values that actually affect the last_changed date for the query
     729                 * belong to one of the special argument combinations, a more specific last_changed
     730                 * cache key is used instead of the global one. */
     731
     732                // Only consider arguments that are not empty and have thus been passed to the query.
     733                $used_args = array_filter( $_args );
     734
     735                // The following arguments affect the query result, but not its last_changed value.
     736                $ignore_args = array( 'number', 'offset', 'no_found_rows', 'orderby', 'order', 'count' );
     737                $used_args = array_diff_key( $used_args, array_flip( $ignore_args ) );
     738
     739                $last_changed_prefix = '';
     740                if ( count( $used_args ) === 3 && isset( $used_args['domain'] ) && isset( $used_args['path'] ) && isset( $used_args['network_id'] ) ) {
     741                        $last_changed_prefix = md5( $used_args['domain'] . $used_args['path'] . $used_args['network_id'] ) . '_';
     742                } elseif ( count( $used_args ) === 2 && isset( $used_args['domain'] ) && isset( $used_args['path'] ) ) {
     743                        $last_changed_prefix = md5( $used_args['domain'] . $used_args['path'] ) . '_';
     744                } elseif ( count( $used_args ) === 1 && isset( $used_args['domain'] ) ) {
     745                        $last_changed_prefix = md5( $used_args['domain'] ) . '_';
     746                } elseif ( count( $used_args ) === 1 && isset( $used_args['path'] ) ) {
     747                        $last_changed_prefix = md5( $used_args['path'] ) . '_';
     748                }
     749
     750                $last_changed = wp_cache_get_last_changed( 'sites', $last_changed_prefix );
     751
     752                return "get_sites:$key:$last_changed";
     753        }
    699754}
  • src/wp-includes/functions.php

     
    56965696 * Get last changed date for the specified cache group.
    56975697 *
    56985698 * @since 4.7.0
     5699 * @since 5.0.0 Now supports the $prefix parameter.
    56995700 *
    5700  * @param string $group Where the cache contents are grouped.
     5701 * @param string $group  Where the cache contents are grouped.
     5702 * @param string $prefix Optional. Prefix to look for a specific last changed key. Default empty string.
    57015703 *
    57025704 * @return string $last_changed UNIX timestamp with microseconds representing when the group was last changed.
    57035705 */
    5704 function wp_cache_get_last_changed( $group ) {
    5705         $last_changed = wp_cache_get( 'last_changed', $group );
     5706function wp_cache_get_last_changed( $group, $prefix = '' ) {
     5707        $key = $prefix . 'last_changed';
     5708
     5709        $last_changed = wp_cache_get( $key, $group );
    57065710
    57075711        if ( ! $last_changed ) {
    57085712                $last_changed = microtime();
    5709                 wp_cache_set( 'last_changed', $last_changed, $group );
     5713                wp_cache_set( $key, $last_changed, $group );
    57105714        }
    57115715
    57125716        return $last_changed;
  • src/wp-includes/ms-blogs.php

     
    447447                        'blog_id' => $blog_id,
    448448                        'domain'  => null,
    449449                        'path'    => null,
     450                        'site_id' => null,
    450451                ) );
    451452        }
    452453
     
    473474         */
    474475        do_action( 'clean_site_cache', $blog_id, $blog, $domain_path_key );
    475476
    476         wp_cache_set( 'last_changed', microtime(), 'sites' );
     477        $current_time = microtime();
     478
     479        $last_changed_keys = array(
     480                md5( $blog->domain . $blog->path . $blog->site_id ) . '_last_changed',
     481                md5( $blog->domain . $blog->path ) . '_last_changed',
     482                md5( $blog->domain ) . '_last_changed',
     483                md5( $blog->path ) . '_last_changed',
     484                'last_changed',
     485        );
     486        foreach ( $last_changed_keys as $last_changed_key ) {
     487                wp_cache_set( $last_changed_key, $current_time, 'sites' );
     488        }
    477489
    478490        /**
    479491         * Fires after the blog details cache is cleared.