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

Add support for expires_in: when using render with collection: #51579

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions actionview/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
* Add `expires_in:` option to `render` when used with `collection:`

*Jarrett Lusso*

Please check [7-2-stable](https://github.com/rails/rails/blob/7-2-stable/actionview/CHANGELOG.md) for previous changes.
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def fetch_or_cache_partial(cached_partials, template, order_by:)
end

unless entries_to_write.empty?
collection_cache.write_multi(entries_to_write)
collection_cache.write_multi(entries_to_write, @options.slice(:expires_in))
jclusso marked this conversation as resolved.
Show resolved Hide resolved
end

keyed_partials
Expand Down
11 changes: 11 additions & 0 deletions actionview/test/template/render_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,17 @@ class CachedCustomer < Customer; end
assert_equal "Hello: david", ActionView::PartialRenderer.collection_cache.read(key)
end

test "template body written to cache with expiration when expires_in set" do
customer = Customer.new("jarrett", 2)
key = cache_key(customer, "test/_customer")
@view.render(partial: "test/customer", collection: [customer], cached: true, expires_in: 1.hour)
assert_equal "Hello: jarrett", ActionView::PartialRenderer.collection_cache.read(key)

travel 2.hours

assert_nil ActionView::PartialRenderer.collection_cache.read(key)
end

test "collection caching does not cache by default" do
customer = Customer.new("david", 1)
key = cache_key(customer, "test/_customer")
Expand Down
9 changes: 9 additions & 0 deletions guides/source/caching_with_rails.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ do not overwrite each other:
cached: ->(product) { [I18n.locale, product] } %>
```

Additionally, you can configure `expires_in` to explicitly set the expiration.

```html+erb
<%= render partial: 'products/product',
collection: @products,
cached: true,
expires_in: 1.hour %>
```

### Russian Doll Caching

You may want to nest cached fragments inside other cached fragments. This is
Expand Down