Skip to content

Commit

Permalink
Merge pull request rails#53484 from zzak/query_cache-config-disable
Browse files Browse the repository at this point in the history
Respect db config `query_cache` in railtie executor hook
  • Loading branch information
byroot authored Oct 29, 2024
2 parents eda12e5 + 4885e9a commit 723d410
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
5 changes: 4 additions & 1 deletion activerecord/lib/active_record/query_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ def uncached(dirties: true, &block)
end

def self.run
ActiveRecord::Base.connection_handler.each_connection_pool.reject(&:query_cache_enabled).each(&:enable_query_cache!)
ActiveRecord::Base.connection_handler.each_connection_pool.reject(&:query_cache_enabled).each do |pool|
next if pool.db_config&.query_cache == false
pool.enable_query_cache!
end
end

def self.complete(pools)
Expand Down
73 changes: 73 additions & 0 deletions activerecord/test/cases/query_cache_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,79 @@ def test_query_cache_is_applied_to_all_connections
clean_up_connection_handler
end

def test_cache_is_not_applied_when_config_is_false
ActiveRecord::Base.connected_to(role: :reading) do
db_config = ActiveRecord::Base.configurations.configs_for(env_name: "arunit", name: "primary")
ActiveRecord::Base.establish_connection(db_config.configuration_hash.merge(query_cache: false))
end

mw = middleware { |env|
ActiveRecord::Base.connection_handler.connection_pool_list(:reading).each do |pool|
assert_not_predicate pool.lease_connection, :query_cache_enabled
assert_nil pool.query_cache.instance_variable_get(:@max_size)
end
}

mw.call({})
ensure
clean_up_connection_handler
end

def test_cache_is_applied_when_config_is_string
ActiveRecord::Base.connected_to(role: :reading) do
db_config = ActiveRecord::Base.configurations.configs_for(env_name: "arunit", name: "primary")
ActiveRecord::Base.establish_connection(db_config.configuration_hash.merge(query_cache: "unlimited"))
end

mw = middleware { |env|
ActiveRecord::Base.connection_handler.connection_pool_list(:reading).each do |pool|
assert_predicate pool.lease_connection, :query_cache_enabled
assert_nil pool.query_cache.instance_variable_get(:@max_size)
end
}

mw.call({})
ensure
clean_up_connection_handler
end

def test_cache_is_applied_when_config_is_integer
ActiveRecord::Base.connected_to(role: :reading) do
db_config = ActiveRecord::Base.configurations.configs_for(env_name: "arunit", name: "primary")
ActiveRecord::Base.establish_connection(db_config.configuration_hash.merge(query_cache: 42))
end

mw = middleware { |env|
ActiveRecord::Base.connection_handler.connection_pool_list(:reading).each do |pool|
assert_predicate pool.lease_connection, :query_cache_enabled
assert_equal 42, pool.query_cache.instance_variable_get(:@max_size)
end
}

mw.call({})
ensure
clean_up_connection_handler
end

def test_cache_is_applied_when_config_is_nil
ActiveRecord::Base.connected_to(role: :reading) do
db_config = ActiveRecord::Base.configurations.configs_for(env_name: "arunit", name: "primary")
ActiveRecord::Base.establish_connection(db_config.configuration_hash.merge(query_cache: nil))
end

mw = middleware { |env|
ActiveRecord::Base.connection_handler.connection_pool_list(:reading).each do |pool|
assert_predicate pool.lease_connection, :query_cache_enabled
end
}

mw.call({})
ensure
clean_up_connection_handler
end



if Process.respond_to?(:fork) && !in_memory_db?
def test_query_cache_with_forked_processes
ActiveRecord::Base.connected_to(role: :reading) do
Expand Down

0 comments on commit 723d410

Please sign in to comment.