Rails3 - 使用 Rails.cache.fetch 在开发模式下进行缓存

发布于 2024-10-31 15:46:37 字数 388 浏览 1 评论 0原文

在开发中,以下(简化的)语句始终记录缓存未命中,在生产中它按预期工作:

@categories = Rails.cache.fetch("categories", :expires_in => 5.minutes) do
  Rails.logger.info "+++ Cache missed +++"
  Category.all
end

如果我在 config/development.rb 中将 config.cache_classes 从 false 更改为 true,它在开发模式下也能正常工作,但是,这使得开发相当痛苦。是否有任何类似于 config.cache_classes = false 的配置设置,除了 Rails.cache.fetch 如果可能从缓存中获取?

In development, the following (simplified) statement always logs a cache miss, in production it works as expected:

@categories = Rails.cache.fetch("categories", :expires_in => 5.minutes) do
  Rails.logger.info "+++ Cache missed +++"
  Category.all
end

If I change config.cache_classes from false to true in config/development.rb, it works as well in development mode, however, this makes development rather painful. Is there any configuration setting that is like config.cache_classes = false except that Rails.cache.fetch is fetching from cache if possible?

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

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

发布评论

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

评论(1

仙女 2024-11-07 15:46:37

尝试将以下内容放入 /config/environments/development.rb 中:

# Temporarily enable caching in development (COMMENT OUT WHEN DONE!)
config.action_controller.perform_caching = true

此外,如果您的缓存存储配置位于 /config/environments/development.rb 中,那么您将需要将相应的行复制到 development.rb 中。例如,如果您的缓存存储是 Dalli memcache gem:

# copied from production.rb into development.rb for caching in development
config.cache_store = :dalli_store, '127.0.0.1' 

希望有帮助。

Try placing the following in /config/environments/development.rb:

# Temporarily enable caching in development (COMMENT OUT WHEN DONE!)
config.action_controller.perform_caching = true

Additionally, if your cache store configuration is in /config/environments/production.rb, then you will need to copy the appropriate line into development.rb as well. For example, if your cache store is the Dalli memcache gem:

# copied from production.rb into development.rb for caching in development
config.cache_store = :dalli_store, '127.0.0.1' 

Hope that helps.

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