This blog is part of our Rails 5 series.
After cache digests were introduced in Rails,
all calls to
#cache in views
automatically append a digest of that template
and all of its dependencies to the cache key.
So developers no longer need to manually discard cache for the specific templates they make changes to.
This creates a caching key something like this
which depends upon the template and its dependencies.
So, now if we change
it will change cache key and thus it allows cache to expire automatically.
As we saw in our earlier example, Rails was able to determine template dependencies implicitly. But, sometimes it is not possible to determine dependencies at all.
Let’s see an example below.
To explicitly add dependency on this template, we need to add a comment in special format as follows.
<%# Template Dependency: posts/complete %> <%# Template Dependency: posts/incomplete %>
If we have multiple dependencies, we need to add special comments for all the dependencies one by one.
Using Wildcard in Rails 5
In Rails 5, we can now use a wildcard for adding dependencies on multiple files in a directory. So, instead of adding files one by one we can add dependency using wildcard.