<p>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.</p>
<ul>
<li><a href="https://en.wikipedia.org/wiki/List_of_PHP_accelerators" rel="noreferrer">PHP Accelerators</a></li>
</ul>
<p>These are web server extensions installed to provide byte-level PHP caching so that PHP doesn't have to &quot;re-compile&quot; an entire PHP script the next time it is requested.  If you are on PHP 5.5+, most *nix distributions have <a href="https://secure.php.net/manual/en/book.opcache.php" rel="noreferrer">OpCache</a> enabled by default.  The settings to control how these extensions behave (TTL, cache busting, etc.) are extension specific.</p>
<p>These extensions are a frequent cause of &quot;I've changed my PHP file, but it appears to be using old code&quot; headaches.</p>
<ul>
<li>Memoization</li>
</ul>
<p>Craft (and probably many Craft plugins) employ a technique called <a href="https://en.wikipedia.org/wiki/Memoization" rel="noreferrer">memoization</a> where the results of an expensive method call get cached so that, <em>on that same request</em>, future calls to that method can read from the cache instead of executing the same expensive code over again.</p>
<ul>
<li>Native <a href="http://twig.sensiolabs.org" rel="noreferrer">Twig</a> caching</li>
</ul>
<p>Twig, the templating language Craft uses, will take a given Twig template and &quot;compile&quot; 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 <code>storage/runtime/compiled_templates</code> folder by default.  If <a href="https://craftcms.com/docs/3.x/config/config-settings.html#devmode" rel="noreferrer">devMode</a> 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.</p>
<ul>
<li>Craft template <a href="https://craftcms.com/docs/3.x/dev/tags.html#cache" rel="noreferrer">{% cache %}</a> tags</li>
</ul>
<p>Craft provides its own (not native to Twig) <code>{% cache %}</code> 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 <a href="https://craftcms.com/docs/3.x/config/#cache-component" rel="noreferrer">the database, redis</a>, etc.</p>
<p>Since using this tag involves database queries, you want to be <a href="https://craftcms.stackexchange.com/questions/105/what-are-the-best-practices-for-using-the-cache-tag">smart</a> about using it.  It can be detrimental to cache template code that doesn't generate any database queries.  The <code>{% cache %}</code> tag will <em>only</em> cache HTML output, so it also makes no sense to cache Twig variables.</p>
<p>The <code>{% cache %}</code> 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.</p>
<ul>
<li>General purpose data caching</li>
</ul>
<p>Craft takes advantage of Yii's <a href="https://www.yiiframework.com/doc/guide/2.0/en/caching-data" rel="noreferrer">supplied caching techniques</a> and exposes them via the Cache service class at <code>Craft::$app-&gt;getCache()</code> so that Craft and any plugins can have a place to store arbitrary cached data.  By default, <code>Craft::$app-&gt;getCache()</code> uses [file based caching][14], which gets saved to Craft's <code>storage/runtime/cache</code> folder, but <code>Craft::$app-&gt;getCache()</code> can be <a href="https://craftcms.com/docs/3.x/config/#cache-component" rel="noreferrer">configured</a> to save to many <a href="https://www.yiiframework.com/doc/guide/2.0/en/caching-data#supported-cache-storage" rel="noreferrer">external sources</a>.</p>
<p>These cache durations are controlled by the <a href="https://craftcms.com/docs/3.x/config/config-settings.html#cacheduration" rel="noreferrer">cacheDuration</a> config setting.</p>
<ul>
<li>External HTML caching services</li>
</ul>
<p>You can configure applications like <a href="https://www.varnish-cache.org/" rel="noreferrer">Varnish</a> (and similar cloud-based services like <a href="https://www.cloudflare.com/" rel="noreferrer">CloudFlare</a>) to provide static/partial page HTML caching to specific URLs with their cache-busting logic.</p>
<p>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 <code>{% cache %}</code> tag and you quickly get diminishing returns performance-wise.</p>
<p>There are <a href="https://plugins.craftcms.com/search?q=cache" rel="noreferrer">many plugins</a> in the Plugin Store that interact with Craft across the different caching levels in Craft to modifying existing or add new caching functionality.</p>

CC BY-SA 4.0
2019-05-08T07:35:28.300Z
Is it still the same in Craftcms 3 or there are any changes?
inrsaurabh
CC BY-SA 4.0
2020-09-07T16:19:49.263Z
@inrsaurabh update for Craft 3
Brad Bell
CC BY-SA 4.0
2016-01-26T06:22:37.520Z
2020-09-07T16:19:36.137Z
2020-09-07T16:19:36.137Z
Brad Bell
<p>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?</p>

<cache><caching><filecache>
What are the cache options available to a Craft installation?
# What are the cache options available to a Craft installation?

(Asked by: username_0 on 2016-01-26)

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?

**Tags:** cache, caching, filecache

---

## 1 answers

### Answer by username_0 on 2016-01-26. Score: 40 | Accepted answer

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.

* [PHP Accelerators](https://en.wikipedia.org/wiki/List_of_PHP_accelerators)

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](https://secure.php.net/manual/en/book.opcache.php) 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](https://en.wikipedia.org/wiki/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](http://twig.sensiolabs.org) 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](https://craftcms.com/docs/3.x/config/config-settings.html#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 template [{% cache %}](https://craftcms.com/docs/3.x/dev/tags.html#cache) tags

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](https://craftcms.com/docs/3.x/config/#cache-component), etc.

Since using this tag involves database queries, you want to be [smart](https://craftcms.stackexchange.com/questions/105/what-are-the-best-practices-for-using-the-cache-tag) 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](https://www.yiiframework.com/doc/guide/2.0/en/caching-data) 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](https://craftcms.com/docs/3.x/config/#cache-component) to save to many [external sources](https://www.yiiframework.com/doc/guide/2.0/en/caching-data#supported-cache-storage).

These cache durations are controlled by the [cacheDuration](https://craftcms.com/docs/3.x/config/config-settings.html#cacheduration) config setting.

* External HTML caching services

You can configure applications like [Varnish](https://www.varnish-cache.org/) (and similar cloud-based services like [CloudFlare](https://www.cloudflare.com/)) 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](https://plugins.craftcms.com/search?q=cache) in the Plugin Store that interact with Craft across the different caching levels in Craft to modifying existing or add new caching functionality.

> **Comments:**
> **username_1**: Is it still the same in Craftcms 3 or there are any changes?  
> **username_0**: @inrsaurabh update for Craft 3  

---

