Skip to content

Commit 723d410

Browse files
authored
Merge pull request rails#53484 from zzak/query_cache-config-disable
Respect db config `query_cache` in railtie executor hook
2 parents eda12e5 + 4885e9a commit 723d410

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

activerecord/lib/active_record/query_cache.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ def uncached(dirties: true, &block)
3535
end
3636

3737
def self.run
38-
ActiveRecord::Base.connection_handler.each_connection_pool.reject(&:query_cache_enabled).each(&:enable_query_cache!)
38+
ActiveRecord::Base.connection_handler.each_connection_pool.reject(&:query_cache_enabled).each do |pool|
39+
next if pool.db_config&.query_cache == false
40+
pool.enable_query_cache!
41+
end
3942
end
4043

4144
def self.complete(pools)

activerecord/test/cases/query_cache_test.rb

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,79 @@ def test_query_cache_is_applied_to_all_connections
141141
clean_up_connection_handler
142142
end
143143

144+
def test_cache_is_not_applied_when_config_is_false
145+
ActiveRecord::Base.connected_to(role: :reading) do
146+
db_config = ActiveRecord::Base.configurations.configs_for(env_name: "arunit", name: "primary")
147+
ActiveRecord::Base.establish_connection(db_config.configuration_hash.merge(query_cache: false))
148+
end
149+
150+
mw = middleware { |env|
151+
ActiveRecord::Base.connection_handler.connection_pool_list(:reading).each do |pool|
152+
assert_not_predicate pool.lease_connection, :query_cache_enabled
153+
assert_nil pool.query_cache.instance_variable_get(:@max_size)
154+
end
155+
}
156+
157+
mw.call({})
158+
ensure
159+
clean_up_connection_handler
160+
end
161+
162+
def test_cache_is_applied_when_config_is_string
163+
ActiveRecord::Base.connected_to(role: :reading) do
164+
db_config = ActiveRecord::Base.configurations.configs_for(env_name: "arunit", name: "primary")
165+
ActiveRecord::Base.establish_connection(db_config.configuration_hash.merge(query_cache: "unlimited"))
166+
end
167+
168+
mw = middleware { |env|
169+
ActiveRecord::Base.connection_handler.connection_pool_list(:reading).each do |pool|
170+
assert_predicate pool.lease_connection, :query_cache_enabled
171+
assert_nil pool.query_cache.instance_variable_get(:@max_size)
172+
end
173+
}
174+
175+
mw.call({})
176+
ensure
177+
clean_up_connection_handler
178+
end
179+
180+
def test_cache_is_applied_when_config_is_integer
181+
ActiveRecord::Base.connected_to(role: :reading) do
182+
db_config = ActiveRecord::Base.configurations.configs_for(env_name: "arunit", name: "primary")
183+
ActiveRecord::Base.establish_connection(db_config.configuration_hash.merge(query_cache: 42))
184+
end
185+
186+
mw = middleware { |env|
187+
ActiveRecord::Base.connection_handler.connection_pool_list(:reading).each do |pool|
188+
assert_predicate pool.lease_connection, :query_cache_enabled
189+
assert_equal 42, pool.query_cache.instance_variable_get(:@max_size)
190+
end
191+
}
192+
193+
mw.call({})
194+
ensure
195+
clean_up_connection_handler
196+
end
197+
198+
def test_cache_is_applied_when_config_is_nil
199+
ActiveRecord::Base.connected_to(role: :reading) do
200+
db_config = ActiveRecord::Base.configurations.configs_for(env_name: "arunit", name: "primary")
201+
ActiveRecord::Base.establish_connection(db_config.configuration_hash.merge(query_cache: nil))
202+
end
203+
204+
mw = middleware { |env|
205+
ActiveRecord::Base.connection_handler.connection_pool_list(:reading).each do |pool|
206+
assert_predicate pool.lease_connection, :query_cache_enabled
207+
end
208+
}
209+
210+
mw.call({})
211+
ensure
212+
clean_up_connection_handler
213+
end
214+
215+
216+
144217
if Process.respond_to?(:fork) && !in_memory_db?
145218
def test_query_cache_with_forked_processes
146219
ActiveRecord::Base.connected_to(role: :reading) do

0 commit comments

Comments
 (0)