WordPress.org

Make WordPress Core

Ticket #42252: 42252.5.diff

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

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

     
    57105710 * Get last changed date for the specified cache group.
    57115711 *
    57125712 * @since 4.7.0
     5713 * @since 5.0.0 Now supports the $prefix parameter.
    57135714 *
    5714  * @param string $group Where the cache contents are grouped.
     5715 * @param string $group  Where the cache contents are grouped.
     5716 * @param string $prefix Optional. Prefix to look for a specific last changed key. Default empty string.
    57155717 *
    57165718 * @return string $last_changed UNIX timestamp with microseconds representing when the group was last changed.
    57175719 */
    5718 function wp_cache_get_last_changed( $group ) {
    5719         $last_changed = wp_cache_get( 'last_changed', $group );
     5720function wp_cache_get_last_changed( $group, $prefix = '' ) {
     5721        $key = $prefix . 'last_changed';
     5722
     5723        $last_changed = wp_cache_get( $key, $group );
    57205724
    57215725        if ( ! $last_changed ) {
    57225726                $last_changed = microtime();
    5723                 wp_cache_set( 'last_changed', $last_changed, $group );
     5727                wp_cache_set( $key, $last_changed, $group );
    57245728        }
    57255729
    57265730        return $last_changed;
  • src/wp-includes/ms-blogs.php

     
    447447                        'blog_id' => $blog_id,
    448448                        'domain'  => null,
    449449                        'path'    => null,
     450                        'site_id' => get_current_network_id(),
    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        // The following arguments are relevant for a special 'last_changed' prefix.
     480        $last_changed_args = array(
     481                'domain'     => $blog->domain,
     482                'path'       => $blog->path,
     483                'network_id' => $blog->site_id,
     484        );
     485
     486        $last_changed_prefixes = array( '' );
     487
     488        // Generate all possible variations.
     489        $combinations = array( array() );
     490        foreach ( $last_changed_args as $key => $value ) {
     491                $value = (string) $value;
     492
     493                foreach ( $combinations as $combination ) {
     494                        $current_args = array_merge( $combination, array( $key => $value ) );
     495                        $last_changed_prefixes[] = md5( serialize( $current_args ) ) . '_';
     496
     497                        $combinations[] = $current_args;
     498                }
     499        }
     500
     501        foreach ( $last_changed_prefixes as $last_changed_prefix ) {
     502                wp_cache_set( $last_changed_prefix . 'last_changed', $current_time, 'sites' );
     503        }
    477504
    478505        /**
    479506         * Fires after the blog details cache is cleared.