在我的rails应用程序中,我试图使用嵌套缓存,但是当user.profile.full_name被更改时,我的缓存键不会过期。因此,当用户更改其名称时,_profile_product.html.erb显示的_profile_product.html.erb仍然是旧的。
我该怎么换钥匙?
概要文件/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 %>发布于 2016-03-31 21:09:51
尝试更改缓存键。
_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的最大值添加到缓存键中,当用户更新其配置文件时,缓存片段将过期。
https://stackoverflow.com/questions/36343150
复制相似问题