Make WordPress Core


Ignore:
Timestamp:
10/25/2022 12:35:30 AM (2 years ago)
Author:
peterwilsoncc
Message:

Query: Move cache key generation to its own method.

Introduce WP_Query::generate_cache_key() for generating the cache key used by the main database query.

This removes the need for a filter to test that cache keys do not include the WPDB placeholder causing unreachable cache keys. The tests now call WP_Query::generate_cache_key() directly.

The filter wp_query_cache_key is removed as a hard deprecation. The filter was not included in a stable release.

Follow up to [54634].

Props spacedmonkey, jorbin, azaozz, hellofromtonya, mukesh27, peterwilsoncc, desrosj, audrasjb, adamsilverstein, flixos90, davidbaumwald, joedolson, sergeybiryukov.
Fixes #56802.

File:
1 edited

Legend:

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

    r54634 r54685  
    31073107        $id_query_is_cacheable = ! str_contains( strtoupper( $orderby ), ' RAND(' );
    31083108        if ( $q['cache_results'] && $id_query_is_cacheable ) {
    3109             $cache_args = $q;
    3110 
    3111             unset(
    3112                 $cache_args['suppress_filters'],
    3113                 $cache_args['cache_results'],
    3114                 $cache_args['fields'],
    3115                 $cache_args['update_post_meta_cache'],
    3116                 $cache_args['update_post_term_cache'],
    3117                 $cache_args['lazy_load_term_meta'],
    3118                 $cache_args['update_menu_item_cache'],
    3119                 $cache_args['search_orderby_title']
    3120             );
    3121 
    31223109            $new_request = str_replace( $fields, "{$wpdb->posts}.*", $this->request );
    3123             $new_request = $wpdb->remove_placeholder_escape( $new_request );
    3124             $key         = md5( serialize( $cache_args ) . $new_request );
    3125 
    3126             $last_changed = wp_cache_get_last_changed( 'posts' );
    3127             if ( ! empty( $this->tax_query->queries ) ) {
    3128                 $last_changed .= wp_cache_get_last_changed( 'terms' );
    3129             }
    3130 
    3131             $cache_key = "wp_query:$key:$last_changed";
    3132 
    3133             /**
    3134              * Filters query cache key.
    3135              *
    3136              * @since 6.1.0
    3137              *
    3138              * @param string   $cache_key   Cache key.
    3139              * @param array    $cache_args  Query args used to generate the cache key.
    3140              * @param string   $new_request SQL Query.
    3141              * @param WP_Query $query       The WP_Query instance.
    3142              */
    3143             $cache_key = apply_filters( 'wp_query_cache_key', $cache_key, $cache_args, $new_request, $this );
     3110            $cache_key   = $this->generate_cache_key( $q, $new_request );
    31443111
    31453112            $cache_found = false;
     
    47564723        return $elements;
    47574724    }
     4725
     4726    /**
     4727     * Generate cache key.
     4728     *
     4729     * @since 6.1.0
     4730     *
     4731     * @global wpdb $wpdb WordPress database abstraction object.
     4732     *
     4733     * @param array  $args Query arguments.
     4734     * @param string $sql  SQL statement.
     4735     *
     4736     * @return string Cache key.
     4737     */
     4738    protected function generate_cache_key( array $args, $sql ) {
     4739        global $wpdb;
     4740
     4741        unset(
     4742            $args['cache_results'],
     4743            $args['fields'],
     4744            $args['lazy_load_term_meta'],
     4745            $args['update_post_meta_cache'],
     4746            $args['update_post_term_cache'],
     4747            $args['update_menu_item_cache'],
     4748            $args['suppress_filters']
     4749        );
     4750
     4751        $placeholder = $wpdb->placeholder_escape();
     4752        array_walk_recursive(
     4753            $args,
     4754            /*
     4755             * Replace wpdb placeholders with the string used in the database
     4756             * query to avoid unreachable cache keys. This is necessary because
     4757             * the placeholder is randomly generated in each request.
     4758             *
     4759             * $value is passed by reference to allow it to be modified.
     4760             * array_walk_recursive() does not return an array.
     4761             */
     4762            function ( &$value ) use ( $wpdb, $placeholder ) {
     4763                if ( is_string( $value ) && str_contains( $value, $placeholder ) ) {
     4764                    $value = $wpdb->remove_placeholder_escape( $value );
     4765                }
     4766            }
     4767        );
     4768
     4769        // Replace wpdb placeholder in the SQL statement used by the cache key.
     4770        $sql = $wpdb->remove_placeholder_escape( $sql );
     4771        $key = md5( serialize( $args ) . $sql );
     4772
     4773        $last_changed = wp_cache_get_last_changed( 'posts' );
     4774        if ( ! empty( $this->tax_query->queries ) ) {
     4775            $last_changed .= wp_cache_get_last_changed( 'terms' );
     4776        }
     4777
     4778        return "wp_query:$key:$last_changed";
     4779    }
     4780
    47584781    /**
    47594782     * After looping through a nested query, this function
Note: See TracChangeset for help on using the changeset viewer.