﻿id,summary,reporter,owner,description,type,status,priority,milestone,component,version,severity,resolution,keywords,cc
20236,Improper testing of cache retrievals causes wasted queries.,andy,,"`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.",defect (bug),closed,normal,3.4,Cache,,normal,fixed,has-patch,
