Q:

Prevent craftcms caching CMS page with dynamic template

I understood CraftCMS caching should exclude dynamic values, however I have a page built as an Entity within CraftCMS admin which {% include %}'s a template with a form.  The templates dynamic form values are somehow being rendered with cached values.
The included template form uses dynamic values from e.g. {{ craft.session.getFlash(key, '') }} which are getting cached by CraftCMS causing the form to render with someone else's session values.
Neither the Entity nor template had {% cache %} flags, and I've tried various cache settings to try and prevent such as {% cache unless craft.session.hasFlash('key') %} within the form template, and before the {% include %} within the Entity page, however the template is still rendered with cached form values.
This is CraftCMS 2.4 with no custom caching plugins, using a Redis cache store and default Craft caching config values.
How can I prevent this? Thanks!

A:

The {% cache %} tag does not exclude dynamic data.
What it does, is to basically make note of any element criteria (i.e. any code relating to pulling entries, categories, assets, tags or users) contained in the Twig code inside the {% cache %}{% endcache %} pair.
Then, whenever something triggers a change to the related element criteria (e.g. if an entry is edited, published or deleted), the cache will automatically be cleared.
This is all related to elements, though – so any other dynamic data you may have in your template, such as session variables, will not trigger the same behaviour.
If your entry or form templates doesn't have any {% cache %} tags, I'm guessing you do have some in a base template wrapping your entry and form templates? If so, you'll need to take a more granular approach to your caching, e.g. by caching components separately and avoid {% cache %} tag nesting. To that end, your unless statement should work nicely for escaping dynamic data caching, as long as the component you're caching isn't wrapped by a higher-level {% cache %} tag.

