【问题标题】:Why is rails query caching not working?为什么 Rails 查询缓存不起作用?
【发布时间】:2023-03-15 16:21:01
【问题描述】:

在我的开发环境中,我有一个生成数百个相同查询的请求:

Person Load (24.4ms)  SELECT "persons".* FROM "persons" WHERE ("persons"."person_id" = 517) LIMIT 1  
. . .   
Person Load (64.4ms)  SELECT "persons".* FROM "persons" WHERE ("persons"."person_id" = 517) LIMIT 1

这是为什么?我认为 Rails 应该在每个请求的基础上默认启用查询缓存?

config/development.rb:  
config.cache_classes = false  
config.perform_caching = true    
. . .    
# Show full error reports and disable caching  
config.consider_all_requests_local       = true  
config.action_view.debug_rjs             = true  
config.action_controller.perform_caching = true  
config.active_support.deprecation        = :log  

【问题讨论】:

  • 生产环境中是否缓存了查询?

标签: ruby-on-rails ruby-on-rails-3 query-cache


【解决方案1】:

如果您要连接到多个数据库(例如在某些模型中使用建立连接),似乎只有 ActiveRecord::Base.connection 启用了查询缓存。不确定这里是否是这种情况,但肯定是连接到多个数据库的一个棘手的副作用。

【讨论】:

  • 只是补充一点,解决这个问题的方法似乎是在你想要缓存的代码周围手动使用 YourRecord.cache { }。请记住,缓存将在块结束时被擦除,因此请确保在块退出之前运行您想要缓存的任何重复查询。
  • @MattZukowski,你认为这是不是 Rails 错误?
【解决方案2】:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-22
    • 2021-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-01
    • 2016-10-29
    • 1970-01-01
    相关资源
    最近更新 更多