Caching with Craft CMS
Published
Reviewedbyfl
Caching makes a Craft CMS site faster when it covers the expensive, repeating parts. Done wrong, it adds load instead of removing it.
About caching in Craft CMS
Cache the parts of a site that repeat on every page and render them without PHP or database queries. A mega menu is a typical example: it runs the same queries, sometimes 50 and more, on every page. Caching does not replace fixing the slow queries behind it. Backend performance covers the caching layers in general.
Caching options
- Twig cache tag: built-in fragment caching
- Blitz: full-page cache plugin with many options
- Upper: integrates reverse proxies such as Cloudflare, Varnish and KeyCDN with Craft, by fortrabbit co-founder Oliver Stark
Common cache mistakes
In our support experience, caching causes about as many problems as it solves.
- A
{% cache %}tag without a specific key creates thousands of cache items with the same data, a poor hit rate and a lot of disk I/O. - A full-page cache flushed every hour triggers cache warming every hour, which requests every page again.
- Pages that are not cached, such as 404 pages, still run their queries. Bots requesting
wp-loginor crawling a deep content structure hit them over and over.
Static files in /templates
Static files such as CSS and JavaScript in the /templates folder get served through PHP. Place them in /web or a subfolder, so the web server delivers them directly.