From 1b4360dea81b0c04589a62b8af7c555cfc0509ca Mon Sep 17 00:00:00 2001 From: Tsukasa OISHI Date: Wed, 28 Jun 2017 14:12:12 +0900 Subject: Enable query cache if set a configurations ActiveRecord query cache is available when a connection is connected. Therefore, query cache is unavailable when entering the ActiveRecord::Base.cache block without being connected. ```ruby ActiveRecord::Base.cache do Task.find(1) # access to database. Task.find(1) # access to database. unavailable query cache end ``` If we use query cache with batch script etc, we need to connect before that. ```ruby Task.connection ActiveRecord::Base.cache do Task.find(1) # access to database. Task.find(1) # available query cache end ``` Before version 3.1, query cache had been enabled if a configuration was set up. In order to solve the `DATABASE_URL` issue(#8074), ActiveRecord has checked whether a connection is connected or not. Today, ActiveRecord.configurations respect `DATABASE_URL`. https://github.com/rails/rails/blob/master/activerecord/lib/active_record/core.rb#L46 --- activerecord/lib/active_record/query_cache.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'activerecord/lib/active_record/query_cache.rb') diff --git a/activerecord/lib/active_record/query_cache.rb b/activerecord/lib/active_record/query_cache.rb index ec246e97bc..e4c2e1f86f 100644 --- a/activerecord/lib/active_record/query_cache.rb +++ b/activerecord/lib/active_record/query_cache.rb @@ -5,20 +5,20 @@ module ActiveRecord # Enable the query cache within the block if Active Record is configured. # If it's not, it will execute the given block. def cache(&block) - if connected? - connection.cache(&block) - else + if configurations.empty? yield + else + connection.cache(&block) end end # Disable the query cache within the block if Active Record is configured. # If it's not, it will execute the given block. def uncached(&block) - if connected? - connection.uncached(&block) - else + if configurations.empty? yield + else + connection.uncached(&block) end end end -- cgit v1.2.3