1

Rails アプリでネストされたキャッシュを使用しようとしていますが、キャッシュ キーがuser.profile.full_name変更されても有効期限が切れません。そのため、ユーザーが自分の名前を変更しても、表示される full_name_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 %>
4

1 に答える 1

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キャッシュ キーに追加することにより、ユーザーがプロファイルを更新すると、キャッシュ フラグメントが期限切れになります。

于 2016-03-31T21:09:51.610 に答える