Writes should always clear the query cache

It shouldn't matter whether or not QC is enabled, inserts, updates, etc
(anything that modifies the state of the DB) should invalidate the
cache.
上级 2d695a91
......@@ -18,7 +18,7 @@ def dirties_query_cache(base, *method_names)
method_names.each do |method_name|
base.class_eval <<-end_code, __FILE__, __LINE__ + 1
def #{method_name}(*)
ActiveRecord::Base.clear_query_caches_for_current_thread if @query_cache_enabled
ActiveRecord::Base.clear_query_caches_for_current_thread
super
end
end_code
......
......@@ -40,6 +40,25 @@ def teardown
super
end
def test_writes_should_always_clear_cache
assert_cache :off
mw = middleware { |env|
Post.first
query_cache = ActiveRecord::Base.connection.query_cache
assert_equal 1, query_cache.length, query_cache.keys
Post.connection.uncached do
# should clear the cache
Post.create!(title: "a new post", body: "and a body")
end
query_cache = ActiveRecord::Base.connection.query_cache
assert_equal 0, query_cache.length, query_cache.keys
}
mw.call({})
assert_cache :off
end
def test_exceptional_middleware_clears_and_disables_cache_on_error
assert_cache :off
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册