在我的rails应用程序中,我尝试使用嵌套缓存,但是当user.profile.full_name
更改时,我的缓存键未到期。因此,当用户更改其姓名时,_profile_product.html.erb
显示的full_name仍为旧名称。
我该如何更改密钥?
简档/ 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 %>
答案 0 :(得分:1)
尝试更改
中的缓存键_profile_products.html.erb
<% cache(['profile-products', @profile_products.map(&:id), @profile_products.map(&:updated_at).max, @profile_products.map{|pp| pp.user.profile.updated_at.to_i }.max]) do %>
<%= render partial: "products/profile_product", collection: @profile_products, as: :product %>
<% end %>
问题是,当用户更新其个人资料名称时,包含整个列表的缓存片段不会过期。
通过将关联的用户配置文件updated_at
的最大值添加到缓存键,缓存片段将在用户更新其配置文件时到期。