12

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?

30

There are many different levels of caching on a web server, some of them are Craft-specific and some that 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 that are 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 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 use in templates. The template cache tags will always store its data in the database in the templatecaches, templatecacheelements and templatecachecriteria tables. There is no native way to get it to store that data in another location (memcache, file system, etc).

Since using this tag involves database queries, you want to be smart about how you use 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 try to cache Twig variables.

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

It's also worth noting that there are many Craft plugins available to enhance or alter default {% cache %} tag functionality.

  • General purpose data caching

Craft takes advantage of Yii's supplied caching techniques and exposes them via the CraftService class at craft()->cache so that Craft and any plugins can have a place to store arbitrary cached data. By default, craft()->cache uses file based caching, which gets saved to Craft's craft/storage/runtime/cache folder, but craft()->cache can be configured, via the cacheMethod config setting, to save to external sources such as APC, the database, EAccelerator, Memcache, Redis, Wincache, XCache and Zend data.

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 own 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.

|improve this answer|||||
  • Is it still the same in Craftcms 3 or there are any changes? – inrsaurabh May 8 '19 at 7:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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