Make WordPress Core

Changeset 55526


Ignore:
Timestamp:
03/10/2023 04:27:23 PM (15 months ago)
Author:
spacedmonkey
Message:

Cache API: Introduce new queries cache groups.

Give developers more control over how query caches are handled within an object caches. Now all caches that cache the result of a query, are cached in a group that is suffixed with -queries. Developers can use these groups, to add custom cache invalidation rules or to make them none persistent.

Props spacedmonkey, owi, tillkruess, skithund, peterwilsoncc, flixos90, sergeybiryukov, mukesh27.
Fixes #57625.

Location:
trunk/src/wp-includes
Files:
13 edited

Legend:

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

    r54133 r55526  
    453453
    454454        $cache_key   = "get_comments:$key:$last_changed";
    455         $cache_value = wp_cache_get( $cache_key, 'comment' );
     455        $cache_value = wp_cache_get( $cache_key, 'comment-queries' );
    456456        if ( false === $cache_value ) {
    457457            $comment_ids = $this->get_comment_ids();
     
    464464                'found_comments' => $this->found_comments,
    465465            );
    466             wp_cache_add( $cache_key, $cache_value, 'comment' );
     466            wp_cache_add( $cache_key, $cache_value, 'comment-queries' );
    467467        } else {
    468468            $comment_ids          = $cache_value['comment_ids'];
     
    10391039            foreach ( $_parent_ids as $parent_id ) {
    10401040                $cache_key        = "get_comment_child_ids:$parent_id:$key:$last_changed";
    1041                 $parent_child_ids = wp_cache_get( $cache_key, 'comment' );
     1041                $parent_child_ids = wp_cache_get( $cache_key, 'comment-queries' );
    10421042                if ( false !== $parent_child_ids ) {
    10431043                    $child_ids = array_merge( $child_ids, $parent_child_ids );
     
    10731073                    $data[ $cache_key ] = $children;
    10741074                }
    1075                 wp_cache_set_multiple( $data, 'comment' );
     1075                wp_cache_set_multiple( $data, 'comment-queries' );
    10761076            }
    10771077
  • trunk/src/wp-includes/class-wp-network-query.php

    r54637 r55526  
    251251
    252252        $cache_key   = "get_network_ids:$key:$last_changed";
    253         $cache_value = wp_cache_get( $cache_key, 'networks' );
     253        $cache_value = wp_cache_get( $cache_key, 'network-queries' );
    254254
    255255        if ( false === $cache_value ) {
     
    263263                'found_networks' => $this->found_networks,
    264264            );
    265             wp_cache_add( $cache_key, $cache_value, 'networks' );
     265            wp_cache_add( $cache_key, $cache_value, 'network-queries' );
    266266        } else {
    267267            $network_ids          = $cache_value['network_ids'];
  • trunk/src/wp-includes/class-wp-query.php

    r55252 r55526  
    28092809
    28102810            $cache_key   = "comment_feed:$key:$last_changed";
    2811             $comment_ids = wp_cache_get( $cache_key, 'comment' );
     2811            $comment_ids = wp_cache_get( $cache_key, 'comment-queries' );
    28122812            if ( false === $comment_ids ) {
    28132813                $comment_ids = $wpdb->get_col( $comments_request );
    2814                 wp_cache_add( $cache_key, $comment_ids, 'comment' );
     2814                wp_cache_add( $cache_key, $comment_ids, 'comment-queries' );
    28152815            }
    28162816            _prime_comment_caches( $comment_ids, false );
     
    31663166            $cache_found = false;
    31673167            if ( null === $this->posts ) {
    3168                 $cached_results = wp_cache_get( $cache_key, 'posts', false, $cache_found );
     3168                $cached_results = wp_cache_get( $cache_key, 'post-queries', false, $cache_found );
    31693169
    31703170                if ( $cached_results ) {
     
    32213221                );
    32223222
    3223                 wp_cache_set( $cache_key, $cache_value, 'posts' );
     3223                wp_cache_set( $cache_key, $cache_value, 'post-queries' );
    32243224            }
    32253225
     
    32543254                );
    32553255
    3256                 wp_cache_set( $cache_key, $cache_value, 'posts' );
     3256                wp_cache_set( $cache_key, $cache_value, 'post-queries' );
    32573257            }
    32583258
     
    33293329            );
    33303330
    3331             wp_cache_set( $cache_key, $cache_value, 'posts' );
     3331            wp_cache_set( $cache_key, $cache_value, 'post-queries' );
    33323332        }
    33333333
     
    33683368
    33693369            $comment_cache_key = "comment_feed:$comment_key:$comment_last_changed";
    3370             $comment_ids       = wp_cache_get( $comment_cache_key, 'comment' );
     3370            $comment_ids       = wp_cache_get( $comment_cache_key, 'comment-queries' );
    33713371            if ( false === $comment_ids ) {
    33723372                $comment_ids = $wpdb->get_col( $comments_request );
    3373                 wp_cache_add( $comment_cache_key, $comment_ids, 'comment' );
     3373                wp_cache_add( $comment_cache_key, $comment_ids, 'comment-queries' );
    33743374            }
    33753375            _prime_comment_caches( $comment_ids, false );
  • trunk/src/wp-includes/class-wp-site-query.php

    r54637 r55526  
    359359
    360360        $cache_key   = "get_sites:$key:$last_changed";
    361         $cache_value = wp_cache_get( $cache_key, 'sites' );
     361        $cache_value = wp_cache_get( $cache_key, 'site-queries' );
    362362
    363363        if ( false === $cache_value ) {
     
    371371                'found_sites' => $this->found_sites,
    372372            );
    373             wp_cache_add( $cache_key, $cache_value, 'sites' );
     373            wp_cache_add( $cache_key, $cache_value, 'site-queries' );
    374374        } else {
    375375            $site_ids          = $cache_value['site_ids'];
  • trunk/src/wp-includes/class-wp-term-query.php

    r55083 r55526  
    776776
    777777        $cache_key = $this->generate_cache_key( $args, $this->request );
    778         $cache     = wp_cache_get( $cache_key, 'terms' );
     778        $cache     = wp_cache_get( $cache_key, 'term-queries' );
    779779
    780780        if ( false !== $cache ) {
     
    802802        if ( 'count' === $_fields ) {
    803803            $count = $wpdb->get_var( $this->request ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
    804             wp_cache_set( $cache_key, $count, 'terms' );
     804            wp_cache_set( $cache_key, $count, 'term-queries' );
    805805            return $count;
    806806        }
     
    809809
    810810        if ( empty( $terms ) ) {
    811             wp_cache_add( $cache_key, array(), 'terms' );
     811            wp_cache_add( $cache_key, array(), 'term-queries' );
    812812            return array();
    813813        }
     
    889889        }
    890890
    891         wp_cache_add( $cache_key, $term_cache, 'terms' );
     891        wp_cache_add( $cache_key, $term_cache, 'term-queries' );
    892892        $this->terms = $this->format_terms( $term_objects, $_fields );
    893893
  • trunk/src/wp-includes/comment.php

    r55424 r55526  
    32943294    }
    32953295
    3296     wp_cache_set( 'last_changed', microtime(), 'comment' );
     3296    wp_cache_set_comments_last_changed();
    32973297}
    32983298
  • trunk/src/wp-includes/general-template.php

    r55515 r55526  
    20552055        $key     = md5( $query );
    20562056        $key     = "wp_get_archives:$key:$last_changed";
    2057         $results = wp_cache_get( $key, 'posts' );
     2057        $results = wp_cache_get( $key, 'post-queries' );
    20582058        if ( ! $results ) {
    20592059            $results = $wpdb->get_results( $query );
    2060             wp_cache_set( $key, $results, 'posts' );
     2060            wp_cache_set( $key, $results, 'post-queries' );
    20612061        }
    20622062        if ( $results ) {
     
    20802080        $key     = md5( $query );
    20812081        $key     = "wp_get_archives:$key:$last_changed";
    2082         $results = wp_cache_get( $key, 'posts' );
     2082        $results = wp_cache_get( $key, 'post-queries' );
    20832083        if ( ! $results ) {
    20842084            $results = $wpdb->get_results( $query );
    2085             wp_cache_set( $key, $results, 'posts' );
     2085            wp_cache_set( $key, $results, 'post-queries' );
    20862086        }
    20872087        if ( $results ) {
     
    21042104        $key     = md5( $query );
    21052105        $key     = "wp_get_archives:$key:$last_changed";
    2106         $results = wp_cache_get( $key, 'posts' );
     2106        $results = wp_cache_get( $key, 'post-queries' );
    21072107        if ( ! $results ) {
    21082108            $results = $wpdb->get_results( $query );
    2109             wp_cache_set( $key, $results, 'posts' );
     2109            wp_cache_set( $key, $results, 'post-queries' );
    21102110        }
    21112111        if ( $results ) {
     
    21302130        $key     = md5( $query );
    21312131        $key     = "wp_get_archives:$key:$last_changed";
    2132         $results = wp_cache_get( $key, 'posts' );
     2132        $results = wp_cache_get( $key, 'post-queries' );
    21332133        if ( ! $results ) {
    21342134            $results = $wpdb->get_results( $query );
    2135             wp_cache_set( $key, $results, 'posts' );
     2135            wp_cache_set( $key, $results, 'post-queries' );
    21362136        }
    21372137        $arc_w_last = '';
     
    21692169        $key     = md5( $query );
    21702170        $key     = "wp_get_archives:$key:$last_changed";
    2171         $results = wp_cache_get( $key, 'posts' );
     2171        $results = wp_cache_get( $key, 'post-queries' );
    21722172        if ( ! $results ) {
    21732173            $results = $wpdb->get_results( $query );
    2174             wp_cache_set( $key, $results, 'posts' );
     2174            wp_cache_set( $key, $results, 'post-queries' );
    21752175        }
    21762176        if ( $results ) {
  • trunk/src/wp-includes/link-template.php

    r55485 r55526  
    19851985    $cache_key = "adjacent_post:$key:$last_changed";
    19861986
    1987     $result = wp_cache_get( $cache_key, 'posts' );
     1987    $result = wp_cache_get( $cache_key, 'post-queries' );
    19881988    if ( false !== $result ) {
    19891989        if ( $result ) {
     
    19981998    }
    19991999
    2000     wp_cache_set( $cache_key, $result, 'posts' );
     2000    wp_cache_set( $cache_key, $result, 'post-queries' );
    20012001
    20022002    if ( $result ) {
  • trunk/src/wp-includes/ms-functions.php

    r55398 r55526  
    14011401    }
    14021402
    1403     wp_cache_set( 'last_changed', microtime(), 'sites' );
     1403    wp_cache_set_sites_last_changed();
    14041404
    14051405    return $blog_id;
  • trunk/src/wp-includes/ms-site.php

    r54947 r55526  
    989989    do_action( 'clean_site_cache', $blog_id, $blog, $domain_path_key );
    990990
    991     wp_cache_set( 'last_changed', microtime(), 'sites' );
     991    wp_cache_set_sites_last_changed();
    992992
    993993    /**
  • trunk/src/wp-includes/post.php

    r55437 r55526  
    56765676    $hash      = md5( $page_path . serialize( $post_type ) );
    56775677    $cache_key = "get_page_by_path:$hash:$last_changed";
    5678     $cached    = wp_cache_get( $cache_key, 'posts' );
     5678    $cached    = wp_cache_get( $cache_key, 'post-queries' );
    56795679    if ( false !== $cached ) {
    56805680        // Special case: '0' is a bad `$page_path`.
     
    57435743
    57445744    // We cache misses as well as hits.
    5745     wp_cache_set( $cache_key, $foundid, 'posts' );
     5745    wp_cache_set( $cache_key, $foundid, 'post-queries' );
    57465746
    57475747    if ( $foundid ) {
     
    59845984
    59855985    $cache_key = "get_pages:$key:$last_changed";
    5986     $cache     = wp_cache_get( $cache_key, 'posts' );
     5986    $cache     = wp_cache_get( $cache_key, 'post-queries' );
    59875987    if ( false !== $cache ) {
    59885988        _prime_post_caches( $cache, false, false );
     
    61496149
    61506150    if ( empty( $pages ) ) {
    6151         wp_cache_set( $cache_key, array(), 'posts' );
     6151        wp_cache_set( $cache_key, array(), 'post-queries' );
    61526152
    61536153        /** This filter is documented in wp-includes/post.php */
     
    61926192    }
    61936193
    6194     wp_cache_set( $cache_key, $page_structure, 'posts' );
     6194    wp_cache_set( $cache_key, $page_structure, 'post-queries' );
    61956195
    61966196    // Convert to WP_Post instances.
     
    73567356    }
    73577357
    7358     wp_cache_set( 'last_changed', microtime(), 'posts' );
     7358    wp_cache_set_posts_last_changed();
    73597359}
    73607360
  • trunk/src/wp-includes/query.php

    r54962 r55526  
    11551155    $last_changed = wp_cache_get_last_changed( 'posts' );
    11561156    $cache_key    = "find_post_by_old_slug:$key:$last_changed";
    1157     $cache        = wp_cache_get( $cache_key, 'posts' );
     1157    $cache        = wp_cache_get( $cache_key, 'post-queries' );
    11581158    if ( false !== $cache ) {
    11591159        $id = $cache;
    11601160    } else {
    11611161        $id = (int) $wpdb->get_var( $query );
    1162         wp_cache_set( $cache_key, $id, 'posts' );
     1162        wp_cache_set( $cache_key, $id, 'post-queries' );
    11631163    }
    11641164
     
    11981198        $last_changed = wp_cache_get_last_changed( 'posts' );
    11991199        $cache_key    = "find_post_by_old_date:$key:$last_changed";
    1200         $cache        = wp_cache_get( $cache_key, 'posts' );
     1200        $cache        = wp_cache_get( $cache_key, 'post-queries' );
    12011201        if ( false !== $cache ) {
    12021202            $id = $cache;
     
    12071207                $id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts, $wpdb->postmeta AS pm_slug, $wpdb->postmeta AS pm_date WHERE ID = pm_slug.post_id AND ID = pm_date.post_id AND post_type = %s AND pm_slug.meta_key = '_wp_old_slug' AND pm_slug.meta_value = %s AND pm_date.meta_key = '_wp_old_date'" . $date_query, $post_type, get_query_var( 'name' ) ) );
    12081208            }
    1209             wp_cache_set( $cache_key, $id, 'posts' );
     1209            wp_cache_set( $cache_key, $id, 'post-queries' );
    12101210        }
    12111211    }
  • trunk/src/wp-includes/taxonomy.php

    r55334 r55526  
    848848    $last_changed = wp_cache_get_last_changed( 'terms' );
    849849    $cache_key    = 'get_objects_in_term:' . md5( $sql ) . ":$last_changed";
    850     $cache        = wp_cache_get( $cache_key, 'terms' );
     850    $cache        = wp_cache_get( $cache_key, 'term-queries' );
    851851    if ( false === $cache ) {
    852852        $object_ids = $wpdb->get_col( $sql );
    853         wp_cache_set( $cache_key, $object_ids, 'terms' );
     853        wp_cache_set( $cache_key, $object_ids, 'term-queries' );
    854854    } else {
    855855        $object_ids = (array) $cache;
     
    28592859
    28602860    wp_cache_delete( $object_id, $taxonomy . '_relationships' );
    2861     wp_cache_delete( 'last_changed', 'terms' );
     2861    wp_cache_set_terms_last_changed();
    28622862
    28632863    /**
     
    29572957
    29582958        wp_cache_delete( $object_id, $taxonomy . '_relationships' );
    2959         wp_cache_delete( 'last_changed', 'terms' );
     2959        wp_cache_set_terms_last_changed();
    29602960
    29612961        /**
     
    35433543    }
    35443544
    3545     wp_cache_delete( 'last_changed', 'terms' );
     3545    wp_cache_set_terms_last_changed();
    35463546
    35473547    /**
     
    36183618    }
    36193619
    3620     wp_cache_set( 'last_changed', microtime(), 'terms' );
     3620    wp_cache_set_terms_last_changed();
    36213621}
    36223622
     
    36313631    wp_cache_delete( 'all_ids', $taxonomy );
    36323632    wp_cache_delete( 'get', $taxonomy );
    3633     wp_cache_delete( 'last_changed', 'terms' );
     3633    wp_cache_set_terms_last_changed();
    36343634
    36353635    // Regenerate cached hierarchy.
Note: See TracChangeset for help on using the changeset viewer.