ActiveRecord::Base 扩展了此模块,因此这些方法可在模型中使用。
方法
实例公共方法
cache(&block) 链接
如果 Active Record 已配置,则在块内启用查询缓存。如果未配置,它将执行给定的块。
源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/query_cache.rb, line 10 def cache(&block) if connected? || !configurations.empty? pool = connection_pool was_enabled = pool.query_cache_enabled begin pool.enable_query_cache(&block) ensure pool.clear_query_cache unless was_enabled end else yield end end
uncached(dirties: true, &block) 链接
在禁用查询缓存的情况下运行块。
如果在执行块之前启用了查询缓存,则在之后会重新启用它。
设置 dirties: false 以防止所有连接上的查询缓存被写操作清除。(默认情况下,写操作会使所有连接的查询缓存失效,以防它们是缓存已过时的副本。)
源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/query_cache.rb, line 33 def uncached(dirties: true, &block) if connected? || !configurations.empty? connection_pool.disable_query_cache(dirties: dirties, &block) else yield end end