【问题标题】:Rails 4 cache: How to disable default caching for particular controller action or view?Rails 4 缓存:如何禁用特定控制器操作或视图的默认缓存?
【发布时间】:2018-01-14 18:22:08
【问题描述】:

我们有Rails 4.2 应用程序在生产环境中运行,具有以下配置(与缓存相关)。到目前为止,我们还没有实现任何缓存技术(默认设置)。

config.action_controller.perform_caching = true

# Use a different cache store in production.
# config.cache_store = :mem_cache_store

我遇到了特定视图的问题,它没有相应地显示更新的数据。

有没有一种方法可以为不在整个应用程序中的特定视图禁用片段缓存?

【问题讨论】:

  • 为什么不从该视图中删除您的 cache some_model do 块?
  • @omnikron - 视图中没有这样的代码。我认为 Rails 在内部处理
  • 不,您必须明确调用cache,否则您的视图将不会被缓存。如果您在任何地方都没有,那么您的任何视图都不会被缓存,除非您正在做其他事情。请参阅rails caching documentation 了解更多信息。也许您对“默认情况下 Rails 提供片段缓存”的位置感到困惑?这只是意味着它在默认情况下是可用的——它实际上并没有为你实现它。
  • @omnikron - 不太确定。但值得交叉检查。谢谢
  • @omnikron - 你是对的!我真的对应用程序中的类似视图感到困惑。感谢您对我上面的愚蠢陈述的评论:)

标签: ruby-on-rails ruby-on-rails-4 caching fragment-caching


【解决方案1】:

TLDR:虽然缓存可能会导致问题,但在尝试修复它之前验证您是否实际使用它是值得的!

禁用单个视图缓存的最简单方法也是最明显的(但可能很容易错过):

从您的视图中移除 cache model do 块。

默认情况下,Rails 不会为您进行任何缓存,因此它将完全处理它。如果您没有在视图中调用 cache 并带有块,那么您没有使用缓存

例如:

缓存:

# app/views/something_important/show.html.haml
= cache something_important do
  = render_something_expensive

未缓存:

# app/views/something_important/show.html.haml
= render_something_expensive

【讨论】:

    猜你喜欢
    • 2014-07-19
    • 1970-01-01
    • 2013-06-19
    • 2012-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-10
    • 1970-01-01
    相关资源
    最近更新 更多