首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rails 4缓存过期无效

rails 4缓存过期无效
EN

Stack Overflow用户
提问于 2016-03-31 20:29:28
回答 1查看 269关注 0票数 1

在我的rails应用程序中,我试图使用嵌套缓存,但是当user.profile.full_name被更改时,我的缓存键不会过期。因此,当用户更改其名称时,_profile_product.html.erb显示的_profile_product.html.erb仍然是旧的。

我该怎么换钥匙?

概要文件/show.html.erb

代码语言:javascript
复制
<% 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

代码语言:javascript
复制
<% 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

代码语言:javascript
复制
<% 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 %>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-31 21:09:51

尝试更改缓存键。

_profile_products.html.erb

代码语言:javascript
复制
<% 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的最大值添加到缓存键中,当用户更新其配置文件时,缓存片段将过期。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36343150

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档