Make WordPress Core

Changeset 44587


Ignore:
Timestamp:
01/14/2019 11:26:20 PM (5 years ago)
Author:
pento
Message:

Query: Cache the result when get_pages() doesn't find anything in the database.

Props flixos90, soulseekah.
Fixes #43514.

Location:
trunk
Files:
2 edited

Legend:

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

    r44562 r44587  
    50585058
    50595059    $cache_key = "get_pages:$key:$last_changed";
    5060     if ( $cache = wp_cache_get( $cache_key, 'posts' ) ) {
     5060    $cache     = wp_cache_get( $cache_key, 'posts' );
     5061    if ( false !== $cache ) {
    50615062        // Convert to WP_Post instances.
    50625063        $pages = array_map( 'get_post', $cache );
     
    52195220
    52205221    if ( empty( $pages ) ) {
     5222        wp_cache_set( $cache_key, array(), 'posts' );
     5223
    52215224        /** This filter is documented in wp-includes/post.php */
    52225225        $pages = apply_filters( 'get_pages', array(), $r );
  • trunk/tests/phpunit/tests/post/getPages.php

    r43571 r44587  
    701701        $this->assertCount( 2, $exclude6 );
    702702    }
     703
     704    /**
     705     * @ticket 43514
     706     */
     707    function test_get_pages_cache_empty() {
     708        global $wpdb;
     709
     710        wp_cache_delete( 'last_changed', 'posts' );
     711        $this->assertFalse( wp_cache_get( 'last_changed', 'posts' ) );
     712
     713        $num_queries = $wpdb->num_queries;
     714
     715        $pages = get_pages(); // Database gets queried
     716
     717        $this->assertEquals( $num_queries + 1, $wpdb->num_queries );
     718
     719        $num_queries = $wpdb->num_queries;
     720
     721        $pages = get_pages(); // Database should not get queried
     722
     723        $this->assertEquals( $num_queries, $wpdb->num_queries );
     724    }
    703725}
Note: See TracChangeset for help on using the changeset viewer.