[wp-trac] [WordPress Trac] #20236: Improper testing of cache retrievals causes wasted queries.
WordPress Trac
wp-trac at lists.automattic.com
Wed Mar 14 15:39:15 UTC 2012
#20236: Improper testing of cache retrievals causes wasted queries.
--------------------------+-----------------------------
Reporter: andy | Owner:
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: General | Version:
Severity: normal | Keywords:
--------------------------+-----------------------------
`wp_cache_get()` can retrieve values such as the empty array which is
considered false by the `!` test. The following function is incorrect
because it will always ignore the cache and repeat the `get_col` and
`wp_cache_add` when the cache contains the empty array. This occurs on
sites which have no pages.
{{{
function get_all_page_ids() {
global $wpdb;
if ( ! $page_ids = wp_cache_get('all_page_ids', 'posts') ) {
$page_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts
WHERE post_type = 'page'");
wp_cache_add('all_page_ids', $page_ids, 'posts');
}
return $page_ids;
}
}}}
The cache check should be written with an understanding of the return type
of `$wpdb->get_col()` (array). Here it is with the check fixed:
{{{
function get_all_page_ids() {
global $wpdb;
$page_ids = wp_cache_get('all_page_ids', 'posts');
if ( ! is_array( $page_ids ) ) {
$page_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts
WHERE post_type = 'page'");
wp_cache_add('all_page_ids', $page_ids, 'posts');
}
return $page_ids;
}
}}}
Patch 1 fixes this occurrence. Please keep this ticket open until all
similar occurrences are found and fixed.
--
Ticket URL: <http://core.trac.wordpress.org/ticket/20236>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software
More information about the wp-trac
mailing list