Repeated database queries kill performance. wp_cache API provides in-memory object caching natively.
Cache Expensive Query:
$cache_key = 'popular_posts';
$posts = wp_cache_get($cache_key);
if ($posts === false) {
// Cache miss - query database
$posts = $wpdb->get_results("SELECT * FROM wp_posts WHERE .....");
wp_cache_set($cache_key, $posts, '', 3600); // Cache 1 hour
}
return $posts;
Note: Works with persistent object cache plugins (Redis, Memcached) automatically. Without them, cache only lasts for single page load (still helpful for reducing duplicate queries on same page).
