【发布时间】:2016-07-20 11:34:33
【问题描述】:
在我的 Rails 应用程序中,我尝试使用嵌套缓存,但是当 user.profile.full_name 更改时,我的缓存键不会过期。因此,当用户更改他/她的姓名时,_profile_product.html.erb 显示的全名仍然是旧的。
我应该如何更改密钥?
profiles/show.html.erb
<% cache(@profile) do %> #this is the profile info and the cache key expires properly when @profile.full_name changes
<%= @profile.full_name %>
.....
<% end %>
<% if @profile.user.products.any? %> #not nested in the previous cache;
#products belonging to the profile are listed with this code under the profile info
<%= render 'products/profile_products' %>
<% end %>
_profile_products.html.erb
<% cache(['profile-products', @profile_products.map(&:id), @profile_products.map(&:updated_at).max]) do %>
<%= render partial: "products/profile_product", collection: @profile_products, as: :product %>
<% end %>
_profile_product.html.erb
<% cache (['profile-product-single', product, product.user.profile]) do %>
<%= product.name %>
<%= product.user.profile.full_name %> #if I change profile name this one won't change thanks to the cache
<% end %>
【问题讨论】:
标签: ruby-on-rails caching fragment-caching