diff options
author | Kasper Timm Hansen <kaspth@gmail.com> | 2016-02-15 22:47:44 +0100 |
---|---|---|
committer | Kasper Timm Hansen <kaspth@gmail.com> | 2016-02-20 16:54:32 +0100 |
commit | b4558c10fb8f5379ffe23860c9ad1ee7a227de44 (patch) | |
tree | e970d2796ba27d9d6d5ef7bf4a747103f69104d6 /actionview/lib/action_view/renderer/partial_renderer | |
parent | 454bc1deab3b60f6b4cbe36227471ed40f4c38f9 (diff) | |
download | rails-b4558c10fb8f5379ffe23860c9ad1ee7a227de44.tar.gz rails-b4558c10fb8f5379ffe23860c9ad1ee7a227de44.tar.bz2 rails-b4558c10fb8f5379ffe23860c9ad1ee7a227de44.zip |
Make collection caching explicit.
Having collection caching that wraps templates and automatically tries
to infer if they are cachable proved to be too much of a hassle.
We'd rather have it be something you explicitly turn on.
This removes much of the code and docs to explain the previous automatic
behavior.
This change also removes scoped cache keys and passing cache_options.
Diffstat (limited to 'actionview/lib/action_view/renderer/partial_renderer')
-rw-r--r-- | actionview/lib/action_view/renderer/partial_renderer/collection_caching.rb | 37 |
1 files changed, 11 insertions, 26 deletions
diff --git a/actionview/lib/action_view/renderer/partial_renderer/collection_caching.rb b/actionview/lib/action_view/renderer/partial_renderer/collection_caching.rb index 4860f00243..debfcae870 100644 --- a/actionview/lib/action_view/renderer/partial_renderer/collection_caching.rb +++ b/actionview/lib/action_view/renderer/partial_renderer/collection_caching.rb @@ -1,5 +1,3 @@ -require 'active_support/core_ext/object/try' - module ActionView module CollectionCaching # :nodoc: extend ActiveSupport::Concern @@ -12,7 +10,7 @@ module ActionView private def cache_collection_render - return yield unless cache_collection? + return yield unless @options[:cached] keyed_collection = collection_by_cache_keys cached_partials = collection_cache.read_multi(*keyed_collection.keys) @@ -21,30 +19,14 @@ module ActionView rendered_partials = @collection.empty? ? [] : yield index = 0 - keyed_collection.map do |cache_key, _| - cached_partials.fetch(cache_key) do - rendered_partials[index].tap { index += 1 } - end + fetch_or_cache_partial(cached_partials, order_by: keyed_collection.each_key) do + rendered_partials[index].tap { index += 1 } end end - def cache_collection? - @options.fetch(:cache, automatic_cache_eligible?) - end - - def automatic_cache_eligible? - @template && @template.eligible_for_collection_caching?(as: @options[:as]) - end - - def callable_cache_key? - @options[:cache].respond_to?(:call) - end - def collection_by_cache_keys - seed = callable_cache_key? ? @options[:cache] : ->(i) { i } - @collection.each_with_object({}) do |item, hash| - hash[expanded_cache_key(seed.call(item))] = item + hash[expanded_cache_key(item)] = item end end @@ -53,10 +35,13 @@ module ActionView key.frozen? ? key.dup : key # #read_multi & #write may require mutability, Dalli 2.6.0. end - def collection_cache_rendered_partial(rendered_partial, key_by) - if callable_cache_key? - key = expanded_cache_key(@options[:cache].call(key_by)) - collection_cache.write(key, rendered_partial, @options[:cache_options]) + def fetch_or_cache_partial(cached_partials, order_by:) + order_by.map do |cache_key| + cached_partials.fetch(cache_key) do + yield.tap do |rendered_partial| + collection_cache.write(cache_key, rendered_partial) + end + end end end end |