diff options
author | Tsukasa OISHI <oishi_t@kakaku.com> | 2017-06-28 14:12:12 +0900 |
---|---|---|
committer | Tsukasa OISHI <tsukasa.oishi@gmail.com> | 2017-06-29 01:34:48 +0900 |
commit | 1b4360dea81b0c04589a62b8af7c555cfc0509ca (patch) | |
tree | adf870f1e61802d958e42dd18f093f891d517fde /activerecord/lib/active_record | |
parent | 5241b265126ac9ecadf1414feaf4c42ceb056432 (diff) | |
download | rails-1b4360dea81b0c04589a62b8af7c555cfc0509ca.tar.gz rails-1b4360dea81b0c04589a62b8af7c555cfc0509ca.tar.bz2 rails-1b4360dea81b0c04589a62b8af7c555cfc0509ca.zip |
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
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/query_cache.rb | 12 |
1 files changed, 6 insertions, 6 deletions
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 |