Prevent craftcms caching CMS page with dynamic template
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.