[5.x] Enhance template cache for more control over expiry #15970
-
TLDR; The ability to supply template cache duration in a variable and / or apply jitter to the cache duration. In a cached template tag like this: {% cache globally using key "[cache-key]" for 1 week %}
...
{% endcache %} It is not possible to use a variable to supply duration. If a variable is supplied we get a Twig exception about a number being expected. It seems it's either the hard-coded duration or the default specified in general config. It would be useful to be able to supply a duration in a variable, as we have a service / config in a module that controls different cache durations for different scenarios and adds jitter to help prevent cache stampede (at the moment, in other places we hit caches directly). It might also be useful to allow jitter to be applied to cache durations, which should probably defined in general config, perhaps by a callback or via an Event. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Just made this change for Craft 5.5 (6915d47). Going forward the number can be set to any valid expression, including variable names. (The unit must still be hard-coded, e.g. |
Beta Was this translation helpful? Give feedback.
Just made this change for Craft 5.5 (6915d47).
Going forward the number can be set to any valid expression, including variable names. (The unit must still be hard-coded, e.g.
week
.)