gpt4 book ai didi

ruby-on-rails - Rails 4.0 expire_fragment/缓存过期不起作用

转载 作者:数据小太阳 更新时间:2023-10-29 06:26:48 24 4
gpt4 key购买 nike

我一直在尝试使用 Rails 的缓存功能,但我无法使某些缓存片段过期,尽管它们似乎已过期。使用 Rails 教程站点中指出的“俄罗斯娃娃缓存”,我正在使用此配置

<% cache "all_available_releases" do %>
<% @releases.each do |release| %>
<% cache(release) do %>
<html code with>
<%ruby code @release.name blah blah blah%>
<%end%>
<%end%>
<%end%>

我在 release_controller.rb Controller 中使外部缓存过期,我在其中使用 expire_fragment("all_available_releases") 使片段过期。我在更新、删除或添加条目的 Controller 的每个方法中都使用它。

这是 WEBrick 的日志,尽管过期片段已在其中注册,但 5 行后过期片段被读取并使用,而这是不应该的。这个例子是在 destroy 调用之后。

Processing by ReleasesController#destroy as HTML
Parameters: {"authenticity_token"=>"***/***/********************+********=", "id"=>"2"}
Release Load (0.1ms) SELECT "releases".* FROM "releases" WHERE "releases"."id" = ? LIMIT 1 [["id", "2"]]
(0.1ms) begin transaction
SQL (2.0ms) DELETE FROM "releases" WHERE "releases"."id" = ? [["id", 2]]
(148.0ms) commit transaction
Expire fragment views/all_available_releases (0.1ms)
Redirected to http://127.0.0.1:3000/releases
Completed 302 Found in 180ms (ActiveRecord: 150.2ms)


Started GET "/releases" for 127.0.0.1 at 2013-07-03 13:09:51 +0300
Processing by ReleasesController#index as HTML
Read fragment views/all_available_releases/41cb0a928326986f35f41c52bb3d8352 (0.1ms)
Rendered releases/index.html.erb within layouts/application (0.6ms)
Completed 200 OK in 5ms (Views: 4.0ms | ActiveRecord: 0.0ms)

我什至尝试使用 Rails.cache.delete("all_available_releases")而且它也没有用。

如果我删除 <%cache "all_available_releases"%> (和一个 <%end%> )从我的 html.erb 缓存工作正常,并在它应该过期的时候过期。

最佳答案

我认为问题是当您在 View 中缓存片段时,缓存摘要被添加到缓存键 (views/all_available_releases/41cb0a928326986f35f41c52bb3d8352),但 expire_fragment 没有使用摘要( View /all_available_releases)。

如果您将 skip_digest: true 添加到 View 中的缓存调用,它应该会阻止使用摘要。

<% cache "all_available_releases", skip_digest: true do %>
<% @releases.each do |release| %>
<% cache(release) do %>
<html code with>
<%ruby code @release.name blah blah blah%>
<%end%>
<%end%>
<%end%>

缓存摘要仅用于自动缓存过期。如果您需要手动使缓存键过期,则不能使用缓存摘要。

关于ruby-on-rails - Rails 4.0 expire_fragment/缓存过期不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17445994/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com