Reading the Craft docs and other related questions on Stack Exchange there seems to be a lot questions about caching, what it does under different scenarios and how to best utilize it. Is there an overview of all of the different caching options available in a Craft installation?

1 Answer 1


There are many different caching levels on a web server, some of them are Craft-specific, and some have nothing to do with Craft, but they often get confused. In addition to that, some of them you have control over, and some of them you get for free.

These are web server extensions installed to provide byte-level PHP caching so that PHP doesn't have to "re-compile" an entire PHP script the next time it is requested. If you are on PHP 5.5+, most *nix distributions have OpCache enabled by default. The settings to control how these extensions behave (TTL, cache busting, etc.) are extension specific.

These extensions are a frequent cause of "I've changed my PHP file, but it appears to be using old code" headaches.

  • Memoization

Craft (and probably many Craft plugins) employ a technique called memoization where the results of an expensive method call get cached so that, on that same request, future calls to that method can read from the cache instead of executing the same expensive code over again.

  • Native Twig caching

Twig, the templating language Craft uses, will take a given Twig template and "compile" it down to PHP code so that subsequent requests to that template will execute the (much faster) PHP directly instead of parsing the Twig template. Craft stores the compiled templates in the Craft storage/runtime/compiled_templates folder by default. If devMode is off, a compiled template will be busted when its source Twig template file's date modified date has changed. If devMode is enabled, the Twig will compile the template on every request.

Craft provides its own (not native to Twig) {% cache %} tags for templates. The template cache tags store its data to wherever Craft is configured to store its general-purpose data caches. By default, that is the file system, but that can be overwritten to other data stores like the database, redis, etc.

Since using this tag involves database queries, you want to be smart about using it. It can be detrimental to cache template code that doesn't generate any database queries. The {% cache %} tag will only cache HTML output, so it also makes no sense to cache Twig variables.

The {% cache %} tag will keep track of any Craft elements (entries, assets, users, tags, etc.) inside of it so that if any of them change, it knows to bust their caches automatically.

  • General purpose data caching

Craft takes advantage of Yii's supplied caching techniques and exposes them via the Cache service class at Craft::$app->getCache() so that Craft and any plugins can have a place to store arbitrary cached data. By default, Craft::$app->getCache() uses [file based caching][14], which gets saved to Craft's storage/runtime/cache folder, but Craft::$app->getCache() can be configured to save to many external sources.

These cache durations are controlled by the cacheDuration config setting.

  • External HTML caching services

You can configure applications like Varnish (and similar cloud-based services like CloudFlare) to provide static/partial page HTML caching to specific URLs with their cache-busting logic.

No doubt missing some other caching methods, but probably worth noting that it's a good general rule of thumb to not double up on caching strategies. For example, the complexity of debugging caching grows exponentially when using Varnish with byte-level caching and the {% cache %} tag and you quickly get diminishing returns performance-wise.

There are many plugins in the Plugin Store that interact with Craft across the different caching levels in Craft to modifying existing or add new caching functionality.

  • Is it still the same in Craftcms 3 or there are any changes?
    – inrsaurabh
    Commented May 8, 2019 at 7:35
  • @inrsaurabh update for Craft 3
    – Brad Bell
    Commented Sep 7, 2020 at 16:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.