Skip to content

Commit

Permalink
Optimize the code inside AR::QueryCache middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdan committed Apr 4, 2018
1 parent f339b09 commit fb75c89
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
15 changes: 4 additions & 11 deletions activerecord/lib/active_record/query_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,12 @@ def uncached(&block)
end

def self.run
ActiveRecord::Base.connection_handler.connection_pool_list.map do |pool|
caching_was_enabled = pool.query_cache_enabled

pool.enable_query_cache!

[pool, caching_was_enabled]
end
ActiveRecord::Base.connection_handler.connection_pool_list.
reject { |p| p.query_cache_enabled }.each { |p| p.enable_query_cache! }
end

def self.complete(caching_pools)
caching_pools.each do |pool, caching_was_enabled|
pool.disable_query_cache! unless caching_was_enabled
end
def self.complete(pools)
pools.each { |pool| pool.disable_query_cache! }

ActiveRecord::Base.connection_handler.connection_pool_list.each do |pool|
pool.release_connection if pool.active_connection? && !pool.connection.transaction_open?
Expand Down
3 changes: 2 additions & 1 deletion activerecord/test/cases/query_cache_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,9 @@ def test_query_cache_is_enabled_on_connections_established_after_middleware_runs
assert_not ActiveRecord::Base.connection_handler.active_connections? # sanity check

middleware {
assert ActiveRecord::Base.connection.query_cache_enabled, "QueryCache did not get lazily enabled"
assert_predicate ActiveRecord::Base.connection, :query_cache_enabled
}.call({})
assert_not_predicate ActiveRecord::Base.connection, :query_cache_enabled
end
end

Expand Down

0 comments on commit fb75c89

Please sign in to comment.