Conditional Cacheing in Craft

Ian Ebden
By Ian Ebden ·

Looks like this article was written a while ago. Some of the ideas and techniques described may now be outdated. Just sayin’.

Cacheing in Craft can improve performance for subsequent requests, as they will have less work to do, which is nice. Trouble is, if you're working locally the cache can sometimes get in the way – especially if you've spent the last 15 minutes trying to figure out why your changes aren't showing... er, for example.

Fear not. There's a simple solution thanks to Craft's devMode config setting. Using a cache unless tag in your templates will mean stuff will only cache on the production site, not you local or staging instances.

{% cache for 1 month unless craft.config.devMode %}

The more I work with Craft the more I find simple one-line solutions to everyday issues.

By the way, if you're new to Craft's {% cache %} tag be sure to thoroughly read through the docs, as cacheing is not always necessary, nor is it always the answer to performance problems.