August 17, 2016
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.
# app/views/users/show.html.erb
<% cache user do %>
<h1>All Posts</h1>
<%= render user.posts %>
<% end %>
# app/views/posts/_post.html.erb
<% cache post do %>
<p> <%= post.content %></p>
<p> <%= post.created_at.to_s %>
<%= render 'posts/completed' %>
<% end %>
This creates a caching key something like this
views/users/605416233-20129410191209/d9fb66b12bx8edf46707c67ab41d93cb2
which
depends upon the template and its dependencies.
So, now if we change posts/_completed.html.erb
, 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.
# app/views/users/show.html.erb
<% cache user do %>
<h1>All Posts</h1>
<%= render user.posts %>
<% end %>
# app/views/posts/_post.html.erb
<% cache post do %>
<p> <%= post.content %></p>
<p> <%= post.created_at.to_s %>
<%= render_post_complete_or_not(post) %>
<% end %>
# app/helpers/posts_helper.rb
module PostsHelper
def render_post_complete_or_not(post)
if post.completed?
render 'posts/complete'
else
render 'posts/incomplete'
end
end
end
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.
# app/views/posts/_post.html.erb
<% cache post do %>
<p> <%= post.content %></p>
<p> <%= post.created_at.to_s %>
<%# Template Dependency: posts/complete %>
<%# Template Dependency: posts/incomplete %>
<%= render_post_complete_or_not(post) %>
<% end %>
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.
# app/views/posts/_post.html.erb
<% cache post do %>
<p> <%= post.content %></p>
<p> <%= post.created_at.to_s %>
<%# Template Dependency: posts/* %>
<%= render_post_complete_or_not(post) %>
<% end %>
If this blog was helpful, check out our full blog archive.