Sha256: 237a130112feec3bfda7b928c2a42517e9b2eaed4cf04e7d686eff288b5e973f

Contents?: true

Size: 1.67 KB

Versions: 32

Compression:

Stored size: 1.67 KB

Contents

module ActionView
  module CollectionCaching # :nodoc:
    extend ActiveSupport::Concern

    included do
      # Fallback cache store if Action View is used without Rails.
      # Otherwise overridden in Railtie to use Rails.cache.
      mattr_accessor(:collection_cache) { ActiveSupport::Cache::MemoryStore.new }
    end

    private
      def cache_collection_render(instrumentation_payload)
        return yield unless @options[:cached]

        keyed_collection = collection_by_cache_keys
        cached_partials  = collection_cache.read_multi(*keyed_collection.keys)
        instrumentation_payload[:cache_hits] = cached_partials.size

        @collection = keyed_collection.reject { |key, _| cached_partials.key?(key) }.values
        rendered_partials = @collection.empty? ? [] : yield

        index = 0
        fetch_or_cache_partial(cached_partials, order_by: keyed_collection.each_key) do
          rendered_partials[index].tap { index += 1 }
        end
      end

      def collection_by_cache_keys
        @collection.each_with_object({}) do |item, hash|
          hash[expanded_cache_key(item)] = item
        end
      end

      def expanded_cache_key(key)
        key = @view.fragment_cache_key(@view.cache_fragment_name(key, virtual_path: @template.virtual_path))
        key.frozen? ? key.dup : key # #read_multi & #write may require mutability, Dalli 2.6.0.
      end

      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
end

Version data entries

32 entries across 32 versions & 4 rubygems

Version Path
actionview-5.0.7.2 lib/action_view/renderer/partial_renderer/collection_caching.rb
actionview-5.0.7.1 lib/action_view/renderer/partial_renderer/collection_caching.rb
actionview-5.0.7 lib/action_view/renderer/partial_renderer/collection_caching.rb
actionview-5.0.6 lib/action_view/renderer/partial_renderer/collection_caching.rb
actionview-5.0.6.rc1 lib/action_view/renderer/partial_renderer/collection_caching.rb
actionview-5.0.5 lib/action_view/renderer/partial_renderer/collection_caching.rb
actionview-5.0.5.rc2 lib/action_view/renderer/partial_renderer/collection_caching.rb
actionview-5.0.5.rc1 lib/action_view/renderer/partial_renderer/collection_caching.rb
actionview-5.0.4 lib/action_view/renderer/partial_renderer/collection_caching.rb
actionview-5.0.4.rc1 lib/action_view/renderer/partial_renderer/collection_caching.rb
actionview-5.0.3 lib/action_view/renderer/partial_renderer/collection_caching.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/actionview-5.0.2/lib/action_view/renderer/partial_renderer/collection_caching.rb
actionview-5.0.2 lib/action_view/renderer/partial_renderer/collection_caching.rb
actionview-5.0.2.rc1 lib/action_view/renderer/partial_renderer/collection_caching.rb
autocompl-0.2.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/actionview-5.0.1/lib/action_view/renderer/partial_renderer/collection_caching.rb
autocompl-0.2.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/actionview-5.0.1/lib/action_view/renderer/partial_renderer/collection_caching.rb
autocompl-0.2.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/actionview-5.0.1/lib/action_view/renderer/partial_renderer/collection_caching.rb
autocompl-0.1.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/actionview-5.0.1/lib/action_view/renderer/partial_renderer/collection_caching.rb
autocompl-0.1.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/actionview-5.0.1/lib/action_view/renderer/partial_renderer/collection_caching.rb
autocompl-0.1.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/actionview-5.0.1/lib/action_view/renderer/partial_renderer/collection_caching.rb