Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails rails 4缓存过期不工作_Ruby On Rails_Caching_Fragment Caching - Fatal编程技术网

Ruby on rails rails 4缓存过期不工作

Ruby on rails rails 4缓存过期不工作,ruby-on-rails,caching,fragment-caching,Ruby On Rails,Caching,Fragment Caching,在我的rails应用程序中,我尝试使用嵌套缓存,但当user.profile.full\u name更改时,我的缓存密钥不会过期。因此,当用户更改其姓名时,\u profile\u product.html.erb显示的全名将保持原来的名称 我该怎么换钥匙 profiles/show.html.erb <% cache(@profile) do %> #this is the profile info and the cache key expires properly when @

在我的rails应用程序中,我尝试使用嵌套缓存,但当
user.profile.full\u name
更改时,我的缓存密钥不会过期。因此,当用户更改其姓名时,
\u profile\u 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.full\u名称更改时,缓存密钥将正确过期
.....
#未嵌套在上一个缓存中;
#属于配置文件的产品与此代码一起列在配置文件信息下
_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 %>
<% 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 %>

_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 %>

#如果我更改配置文件名,由于缓存的原因,此配置文件不会更改

尝试在中更改缓存密钥

_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 %>
<% 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 %>

问题在于,当用户更新其配置文件名时,包含整个列表的cachefragment不会过期

通过将关联用户配置文件的
updated_at
的最大值添加到缓存键,当用户更新其配置文件时,缓存片段将过期