WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 3 years ago

#43514 closed defect (bug) (fixed)

`get_pages()` DB query result not cached if nothing found

Reported by: flixos90 Owned by: pento
Milestone: 5.1 Priority: normal
Severity: normal Version:
Component: Query Keywords: has-patch has-unit-tests
Focuses: Cc:

Description

get_pages() uses a cache key in order to not have to query the DB when it's not necessary. However, if the DB query returns an empty result, that result is not cached, causing that DB query to be executed every time the function is called with these parameters.

Since the function uses wp_cache_get_last_changed( 'posts' ) anyway, it should be safe to also set an empty array if nothing is found, to prevent those unnecessary database requests.

Attachments (2)

43514.diff (870 bytes) - added by flixos90 4 years ago.
43514.tests.diff (939 bytes) - added by soulseekah 4 years ago.
Tests

Download all attachments as: .zip

Change History (8)

@flixos90
4 years ago

#1 @flixos90
4 years ago

  • Keywords has-patch added; needs-patch removed

43514.diff ensures that the cache key is also set when the DB query returns no results. The initial check after retrieving the cached value has been adjusted accordingly to be more strict, like it is commonly handled in core (check if value is not false).

@soulseekah
4 years ago

Tests

#2 @soulseekah
4 years ago

  • Keywords has-unit-tests added; needs-unit-tests removed

Tests attached.

#3 @SergeyBiryukov
4 years ago

  • Milestone changed from Awaiting Review to 5.0

#4 @flixos90
3 years ago

  • Milestone changed from 5.0 to 5.1

#5 @pento
3 years ago

  • Owner set to pento
  • Status changed from new to assigned

#6 @pento
3 years ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 44587:

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

Props flixos90, soulseekah.
Fixes #43514.

Note: See TracTickets for help on using tickets.