The native {% cache %}
tag is great, but in some cases the element queries Craft creates to clear the caches can become too complex, which can bog down your system. Cache Flag provides an alternative (and in most cases, more performant) way to have your caches clear automatically when your content changes.
Looking for the Craft 3 version? It's right here!
Cache Tag adds the {% cacheflag %}
tag to Twig, which – like P&T's Cold Cache plugin – doesn't create element queries for automatic cache breaking. Instead, Cache Flag gives you granular control over when a particular cache should be cleared, by adding flags to content and caches.
Cache Flag draws inspiration from the excellent CE Cache plugin for ExpressionEngine, which implements tags in a similar manner.
The basic concept is that you add one or more flags (basically just strings, could be anything) to your content (sections, category groups, element types, etc) and to your caches (using the flagged
parameter for the {% cacheflag %}
tag). Whenever an element is saved or deleted, Cache Flag clears any caches with matching flags. Simple!
Let's say you have section called "Awesome Stuff", and there's a cache that you want to clear every time content in that section changes. First, you add the flag awesome
to the Awesome Stuff section in Cache Flag. Then, you flag the cache(s) you want to clear with awesome
in your template, using Cache Flag's flagged
parameter:
{% cacheflag flagged "awesome" %}
...
{% endcacheflag %}
Now, whenever an entry in the Awesome Stuff section is saved or deleted, the above cache will be cleared.
Suppose you also want to have the above cache cleared whenever a category in a particular category group is published or deleted. You could add the flag awesome
to the relevant category group as well, or you could add another flag to it entirely, e.g. radical
. You can use a pipe delimiter to specify multiple flags in your template:
{% cacheflag flagged "awesome|radical" %}
...
{% endcacheflag %}
Beyond the flagged
parameter, the {% cacheflag %}
tag supports all the same parameters as the native {% cache %}
tag – so I'll just refer to the official documentation for the latter.
Cache Flag dispatches two events:
cacheFlag.beforeDeleteFlaggedCaches
Dispatches just before Cache Flag deletes one or several template caches by flag.
Event parameters: flags
(array of flags having caches deleted) and ids
(the IDs of all the templatecaches being deleted)
cacheFlag.deleteFlaggedCaches
Dispatches immediately after Cache Flag has deleted one or several template caches by flag.
Event parameters: flags
(array of flags having caches deleted), ids
(the IDs of all the templatecaches being deleted) and result
(either false
or the number of rows affected in craft_templatecaches
).
Listening to the Cache Flag events work as you'd expect:
craft()->on('cacheFlag.deleteFlaggedCaches', [$this, 'onDeleteFlaggedCaches']);
...
public function onDeleteFlaggedCaches(Event $event)
{
$flags = $event->params['flags'];
// ...custom logic, e.g. cache warming for the affected flags etc
}
- Fixes an issue where custom element types might return the wrong element type
- Adds
cacheFlag.beforeDeleteFlaggedCaches
andcacheFlag.deleteFlaggedCaches
events
- Fixes a Live Preview related issue – thanks @michaelramuta!
- Fixes an issue where programmatically busting the cache using console commands or non-CP HTTP requests was impossible
- Really fixes the regression error introduced in CacheFlag 1.1.5
- Fixes a regression error introduced in CacheFlag 1.1.5 – thanks @aelvan!
- Fixes an issue where CacheFlag would potentially create a lot of database queries when a keyed, non-global template cache was made global
- Fixes an issue where clearing individual flags would fail when CSRF was enabled (thanks @aelvan)
- Fixes an issue w/ Live Preview (thanks @mjatharvest
- Added option to clear individual flagged caches from CP section
- Caches are now cleared for elements that have their status changed via element indexes
- Fixed error where elementType flags was not being saved (thanks André)
- The CP section now has a single Save button, and uses AJAX
- Empty flags are now deleted from the database
- Flagged caches using deleted flags are now cleared
- Fixed an issue where Cache Flag would create duplicate caches
- Fixed an issue where Cache Flag would not save flags where CSRF were enabled
- Fixed issue #2, where caches would fail to clear due to a typo
- Fixed issue #3, where Cache Flags CP section would choke on Craft Personal & Client
- Fixed an issue where saving a global set wouldnt break flagged caches
- Fixed a breaking bug resulting from a typo (thanks André Elvan!)
- Added Craft 2.5 features (release feed etc)
- Fixed an issue w/ blank uid, dateCreated, dateUpdated columns for flagged caches
- Fixed several issues in Twig parser #1
- Fixed an issue w/ wrong name for CacheFlagService
- Initial public release
Stay tuned for upcoming features.
- Events and hooks for cache warming etc.
- Varnish support
Cache Flag is provided free of charge. The author is not responsible for any data loss or other problems resulting from the use of this plugin. Please report any bugs, feature requests or other issues here. As Cache Flag is a hobby project, no promises are made regarding response time, feature implementations or bug amendments. Pull requests are very welcome!