Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with cached partial collections #33589

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions actionview/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
* Fix issue with cached partial collections

It's possible to render a collection of partials with the `cached: true`
parameter, each of which are also cached. If those partials also
have an `expires_in` value for their cache lifetime. Because of the
key collision between the partial's cache key and the individual
collection elements' cache key, the non-expiring version will win.

Fix is to allow cache parameters (well, only `expires_in`) to be passed
allong with the cache collection render call. i.e., from:

<%= render :partial => 'test/test', :collection => @models, :as => :test, :cached => true %>

to:

<%= render :partial => 'test/test2', :collection => @models, :as => :test, :cached => true, :expires_in => 5.seconds %>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you feel about changing this to:

render :partial => 'test/test2', :collection => @models, :as => :test, :cached => { :expires_in => 5.seconds }


For #33424

*Iain Bryson*

* Deprecate calling private model methods from view helpers.

For example, in methods like `options_from_collection_for_select`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module CollectionCaching # :nodoc:
def cache_collection_render(instrumentation_payload)
return yield unless @options[:cached]

@cache_options = @options.slice(:expires_in) # REVIEW: is Ruby 2.5-requiring code allowed here?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on there being 2.4 builds on Travis CI, probably not :(

keyed_collection = collection_by_cache_keys
cached_partials = collection_cache.read_multi(*keyed_collection.keys)
instrumentation_payload[:cache_hits] = cached_partials.size
Expand All @@ -22,7 +23,7 @@ def cache_collection_render(instrumentation_payload)
rendered_partials = @collection.empty? ? [] : yield

index = 0
fetch_or_cache_partial(cached_partials, order_by: keyed_collection.each_key) do
fetch_or_cache_partial(cached_partials, order_by: keyed_collection.each_key, cache_options: @cache_options) do
rendered_partials[index].tap { index += 1 }
end
end
Expand All @@ -44,11 +45,11 @@ def expanded_cache_key(key)
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:)
def fetch_or_cache_partial(cached_partials, order_by:, cache_options:)
order_by.map do |cache_key|
cached_partials.fetch(cache_key) do
yield.tap do |rendered_partial|
collection_cache.write(cache_key, rendered_partial)
collection_cache.write(cache_key, rendered_partial, cache_options)
end
end
end
Expand Down