Rails - 片段缓存不会过期

发布于 2024-07-20 06:21:33 字数 345 浏览 2 评论 0原文

这个把我难住了。

我有一个带有缓存片段的视图:

 - cache :key=>"news" do    
   %h2 News
   - etc

我有一个使用的清理器:

def expire_home_cache
  puts "expire_home_cache"
  expire_fragment(:key => "news") 
end

调用清理器是因为我可以在控制台输出中看到“expire_home_cache”。

但片段没有更新......

有什么想法吗?

This one has me stumped.

I have a view with a cached fragment:

 - cache :key=>"news" do    
   %h2 News
   - etc

I have a sweeper that uses:

def expire_home_cache
  puts "expire_home_cache"
  expire_fragment(:key => "news") 
end

The sweeper is called as I can see "expire_home_cache" in the console output.

But the fragment is not updated ...

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

飞烟轻若梦 2024-07-27 06:21:33

尝试将 expire_fragment(:key => "news") 替换为 ActionController::Base.new.expire_fragment(:key => "news")

没有时间解释,但这对我有用。

Try replacing expire_fragment(:key => "news") with ActionController::Base.new.expire_fragment(:key => "news")

No time to explain, but it worked for me.

千年*琉璃梦 2024-07-27 06:21:33

你可以尝试这个:

   cache("news") do    
     %h2 News
     - etc
   end

并且...

def expire_home_cache
  puts "expire_home_cache"
  expire_fragment("news") 
end

...或者尝试这个...

 - cache({:key=>"news"}) do    
   %h2 News
   - etc

我认为问题可能是ruby或rails很难确定密钥到底是什么,因此缓存方法和expire_fragment正在生成两个不同的缓存键。

You might try this:

   cache("news") do    
     %h2 News
     - etc
   end

and...

def expire_home_cache
  puts "expire_home_cache"
  expire_fragment("news") 
end

...or try this ...

 - cache({:key=>"news"}) do    
   %h2 News
   - etc

I am thinking the issue may be that ruby or rails is having a hard time determining what the key is exactly and so the cache method and expire_fragment are generating two different cache keys.

愁以何悠 2024-07-27 06:21:33

正确的方法是:

cache :news do

  ...
end

然后在你的扫地机中:

expire_fragment :news

The proper way to do this is:

cache :news do

  ...
end

And then in your sweeper:

expire_fragment :news
叹沉浮 2024-07-27 06:21:33

这并不能直接回答您的问题,但是您是否尝试过使用 timed_fragment_cache 插件作为替代方案?

http://github.com/tricycle/timed_fragment_cache/tree/master

我发现了这个这是使我的项目中的片段过期的更简单的方法。

This doesn't directly answer your question, but have you tried the timed_fragment_cache plugin as an alternative?

http://github.com/tricycle/timed_fragment_cache/tree/master

I found this to be a much simpler way of expiring fragments in my projects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文