Performance: Caching strategies
Caching strategies improve page load speed by reducing server processing and network requests.
<?php
// Use page caching plugin or object cache
// Cached HTML served directly without PHP processing
?><?php // No caching, every page loads fresh get_header(); // heavy database queries here get_footer(); ?>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No caching, full PHP load | High (dynamic DOM generation) | Multiple reflows due to slow load | High paint cost due to delayed content | [X] Bad |
| Page caching with static HTML | Low (static DOM) | Single reflow after fast load | Low paint cost, content appears quickly | [OK] Good |
| No cache headers on assets | N/A | N/A | High paint cost due to delayed asset load | [X] Bad |
| Cache headers with deferred scripts | N/A | N/A | Low paint cost, faster asset load | [OK] Good |
function get_cached_data() {
$cache_key = 'my_data';
$data = wp_cache_get($cache_key);
if ($data === false) {
$data = 'Expensive data';
wp_cache_set($cache_key, $data, '', 3600);
}
return $data;
}
echo get_cached_data();Pass the test and this pattern moves unknown