Only write to collection cache if we have a callable cache key.
A callable cache key writes to the collection cache under a certain namespace. Which means if we don't have scoped cache key we can just rely on the `cache model_name do` in the templates to cache them. Less writes, more sharing. Add `assert_customer_cached` to better illustrate this in tests, and remove tests which then don't communicate as much.
This commit is contained in:
parent
29e5c20578
commit
a88c5ff96d
|
@ -410,6 +410,7 @@ class AutomaticCollectionCacheTest < ActionController::TestCase
|
|||
def test_collection_fetches_cached_views
|
||||
get :index
|
||||
assert_equal 1, @controller.partial_rendered_times
|
||||
assert_customer_cached 'david/1', 'david, 1'
|
||||
|
||||
get :index
|
||||
assert_equal 1, @controller.partial_rendered_times
|
||||
|
@ -441,23 +442,15 @@ class AutomaticCollectionCacheTest < ActionController::TestCase
|
|||
|
||||
def test_caching_with_callable_cache_key
|
||||
get :index_with_callable_cache_key
|
||||
assert_equal 1, @controller.partial_rendered_times
|
||||
assert_select ':root', 'david, 1'
|
||||
|
||||
get :index_with_callable_cache_key
|
||||
assert_equal 1, @controller.partial_rendered_times
|
||||
assert_select ':root', 'david, 1'
|
||||
assert_customer_cached 'cached_david', 'david, 1'
|
||||
assert_customer_cached 'david/1', 'david, 1'
|
||||
end
|
||||
|
||||
def test_caching_mixing_callable_cache_key_and_automatic_caching
|
||||
get :index
|
||||
assert_equal 1, @controller.partial_rendered_times
|
||||
assert_select ':root', 'david, 1'
|
||||
|
||||
get :index_with_callable_cache_key
|
||||
assert_equal 1, @controller.partial_rendered_times, 'individual cache not reused with collection'
|
||||
assert_select ':root', 'david, 1'
|
||||
end
|
||||
private
|
||||
def assert_customer_cached(key, content)
|
||||
assert_match content,
|
||||
ActionView::PartialRenderer.collection_cache.read("views/#{key}/7c228ab609f0baf0b1f2367469210937")
|
||||
end
|
||||
end
|
||||
|
||||
class FragmentCacheKeyTestController < CachingController
|
||||
|
|
|
@ -51,10 +51,14 @@ module ActionView
|
|||
end
|
||||
|
||||
def fetch_or_cache_partial(cached_partials, order_by:)
|
||||
rely_on_individual_cache_call = !callable_cache_key?
|
||||
|
||||
order_by.map do |key|
|
||||
cached_partials.fetch(key) do
|
||||
yield.tap do |rendered_partial|
|
||||
collection_cache.write(key, rendered_partial, @options[:cache_options])
|
||||
unless rely_on_individual_cache_call
|
||||
collection_cache.write(key, rendered_partial, @options[:cache_options])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue