diff options
author | yuuji.yaginuma <yuuji.yaginuma@gmail.com> | 2018-02-17 17:25:08 +0900 |
---|---|---|
committer | Yuji Yaginuma <yuuji.yaginuma@gmail.com> | 2018-02-19 07:16:52 +0900 |
commit | 0d9fb7eb8df380b601d0a9a0a796b97e9c3c45c7 (patch) | |
tree | 50b4ff2cbf049fe2a8b303c53ed4832a2a6195e7 /activerecord | |
parent | bec1751ee416aa5a36fd5d147abf97138a0d2efb (diff) | |
download | rails-0d9fb7eb8df380b601d0a9a0a796b97e9c3c45c7.tar.gz rails-0d9fb7eb8df380b601d0a9a0a796b97e9c3c45c7.tar.bz2 rails-0d9fb7eb8df380b601d0a9a0a796b97e9c3c45c7.zip |
Use the query cache when connection is already connected
Fixes #32021.
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/query_cache.rb | 12 | ||||
-rw-r--r-- | activerecord/test/cases/query_cache_test.rb | 11 |
2 files changed, 17 insertions, 6 deletions
diff --git a/activerecord/lib/active_record/query_cache.rb b/activerecord/lib/active_record/query_cache.rb index 8e23128333..c8e340712d 100644 --- a/activerecord/lib/active_record/query_cache.rb +++ b/activerecord/lib/active_record/query_cache.rb @@ -7,20 +7,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 configurations.empty? - yield - else + if connected? || !configurations.empty? connection.cache(&block) + else + yield 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 configurations.empty? - yield - else + if connected? || !configurations.empty? connection.uncached(&block) + else + yield end end end diff --git a/activerecord/test/cases/query_cache_test.rb b/activerecord/test/cases/query_cache_test.rb index f63f6295d6..d635a47c0e 100644 --- a/activerecord/test/cases/query_cache_test.rb +++ b/activerecord/test/cases/query_cache_test.rb @@ -320,6 +320,17 @@ class QueryCacheTest < ActiveRecord::TestCase end end + def test_cache_is_available_when_connection_is_connected + conf = ActiveRecord::Base.configurations + + ActiveRecord::Base.configurations = {} + Task.cache do + assert_queries(1) { Task.find(1); Task.find(1) } + end + ensure + ActiveRecord::Base.configurations = conf + end + def test_cache_is_available_when_using_a_not_connected_connection skip "In-Memory DB can't test for using a not connected connection" if in_memory_db? with_temporary_connection_pool do |