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

Conversation

iainbryson
Copy link

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 %>

For #33424

*Iain Bryson*

Summary

Provide a general description of the code changes in your pull
request... were there any bugs you had fixed? If so, mention them. If
these bugs have open GitHub issues, be sure to tag them here as well,
to keep the conversation linked together.

Other Information

If there's anything else that's important and relevant to your pull
request, mention that information here. This could include
benchmarks, or other information.

If you are updating any of the CHANGELOG files or are asked to update the
CHANGELOG files by reviewers, please add the CHANGELOG entry at the top of the file.

Finally, if your pull request affects documentation or any non-code
changes, guidelines for those changes are available
here

Thanks for contributing to Rails!

    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 %>

    For rails#33424

    *Iain Bryson*
@rails-bot
Copy link

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @rafaelfranca (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review.

Please see the contribution instructions for more information.

@@ -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 :(


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 }

@ghost
Copy link

ghost commented Oct 15, 2019

Does this PR still in work? I'm looking for a way to invalidate cached collection on specific actions (like if the locale change).

@serene
Copy link

serene commented Oct 17, 2019

@Escanxr Maybe we should pull the changes into a new PR that is up to date with master?

@ghost
Copy link

ghost commented Oct 17, 2019

@Escanxr Maybe we should pull the changes into a new PR that is up to date with master?

I think so. I hope I will be available to do it this week-end

@rails-bot
Copy link

rails-bot bot commented Jan 15, 2020

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
Thank you for your contributions.

@rails-bot rails-bot bot added the stale label Jan 15, 2020
@rails-bot rails-bot bot closed this Jan 22, 2020
@jvillarejo
Copy link
Contributor

Sorry for the up, but it would be good to have this.
I have a collection that has a file attachment which I want to expire_in with the same expires_in configured for ActiveStorage

Is there anyway I can help ?

@mpkhr
Copy link

mpkhr commented Mar 28, 2024

+1

Ideally the expiration can be individual for each entry in the collection. Like the cache key name, which can be customized by giving a block to the render statement.

@jclusso
Copy link

jclusso commented Apr 16, 2024

This would be great to have, but as a work around, you can use something like this to force expire your cache every so often.

This would expire the cache every 5 minutes.

<%= render collection: @posts, partial: 'post', cached: -> (post) { [post, (Time.now.min / 5)] } %>

@mpkhr
Copy link

mpkhr commented Apr 16, 2024

Yep, that's what I currently do. Thanks for mentioning it though, so people know it's possible.

The request is in favor of recyclable cache keys over key-based expiration, which was a significant improvement in caching efficiency (DHH said: We went from only being able to keep 18 hours of caching to, I believe, 3 weeks. It was the single biggest performance boost that Basecamp 3 has ever seen.). So I think it's worth extending the collection-renderer with this capability

@jclusso
Copy link

jclusso commented Apr 16, 2024

I opened a new PR to do this. #51579

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants