Expire fragment caching when inner HTML has changed?

165 views Asked by At

I have the following in my users.html.slim view:

.header
  Welcome
  - cache ['user/details', user.last_modified] do
    = render 'user/details', user: @user

My problem is that I have changed the user/details markup, and since the users haven't actually changed I still get the cached version.

According to the docs, since the HTML has changed, the cache should expire

Cache fragments will also be expired when the view fragment changes (e.g., the HTML in the view changes). The string of characters at the end of the key is a template tree digest. It is an MD5 hash computed based on the contents of the view fragment you are caching. If you change the view fragment, the MD5 hash will change, expiring the existing file.

But that doesn't seem to be the case.

What is the proper way of solving this scenario? Where the parameters that you rely on the keys haven't changed, but the actual markup has changed. I am thinking on doing something like:

- cache ['user/details/v2', user.last_modified] do
    = render 'user/details', user: @user

But I am wondering if that's what is more appropriate in these scenarios.

1

There are 1 answers

2
kobaltz On

Instead of user.last_modified just pass user.

This will create a cache key based on the user.id and user.updated_at.

If last_modified is in fact the thing that you want to be the cache key, you reference @user but your cache key is user.