提交 e176353b 编写于 作者: A Aaron Patterson

clear cache on body close so that cache remains during rendering

fixes #13547

The body may use the local cache during rendering.  `call`ing the app
doesn't mean that rendering is finished, so we need to wait until
`close` is called on the body.
上级 2875b4a6
require 'active_support/core_ext/object/duplicable'
require 'active_support/core_ext/string/inflections'
require 'rack/body_proxy'
module ActiveSupport
module Cache
......@@ -83,9 +84,14 @@ def new(app)
def call(env)
LocalCacheRegistry.set_cache_for(local_cache_key, LocalStore.new)
@app.call(env)
ensure
response = @app.call(env)
response[2] = ::Rack::BodyProxy.new(response[2]) do
LocalCacheRegistry.set_cache_for(local_cache_key, nil)
end
response
rescue Exception
LocalCacheRegistry.set_cache_for(local_cache_key, nil)
raise
end
end
......
......@@ -3,6 +3,42 @@
require 'active_support/cache'
require 'dependencies_test_helpers'
module ActiveSupport
module Cache
module Strategy
module LocalCache
class MiddlewareTest < ActiveSupport::TestCase
def test_local_cache_cleared_on_close
key = "super awesome key"
assert_nil LocalCacheRegistry.cache_for key
middleware = Middleware.new('<3', key).new(->(env) {
assert LocalCacheRegistry.cache_for(key), 'should have a cache'
[200, {}, []]
})
_, _, body = middleware.call({})
assert LocalCacheRegistry.cache_for(key), 'should still have a cache'
body.each { }
assert LocalCacheRegistry.cache_for(key), 'should still have a cache'
body.close
assert_nil LocalCacheRegistry.cache_for(key)
end
def test_local_cache_cleared_on_exception
key = "super awesome key"
assert_nil LocalCacheRegistry.cache_for key
middleware = Middleware.new('<3', key).new(->(env) {
assert LocalCacheRegistry.cache_for(key), 'should have a cache'
raise
})
assert_raises(RuntimeError) { middleware.call({}) }
assert_nil LocalCacheRegistry.cache_for(key)
end
end
end
end
end
end
class CacheKeyTest < ActiveSupport::TestCase
def test_entry_legacy_optional_ivars
legacy = Class.new(ActiveSupport::Cache::Entry) do
......@@ -577,6 +613,7 @@ def test_middleware
result = @cache.write('foo', 'bar')
assert_equal 'bar', @cache.read('foo') # make sure 'foo' was written
assert result
[200, {}, []]
}
app = @cache.middleware.new(app)
app.call({})
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册