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

How do you manage fragment caching invalidation? #245

Closed
guillaumebriday opened this issue Dec 27, 2024 · 1 comment
Closed

How do you manage fragment caching invalidation? #245

guillaumebriday opened this issue Dec 27, 2024 · 1 comment

Comments

@guillaumebriday
Copy link
Member

Hey! 👋

This one’s probably a question for @djmb or @dhh at Basecamp. 😊

I use fragment caching quite a bit, but I often run into issues when the cached content includes helpers, translations (like date formats or text), etc. Here’s an example:

<%# app/views/comments/index.html.erb %>
<%= render partial: 'comments/comment', collection: @comments, cached: true %>

<%# app/views/comments/_comment.html.erb %>
<%= t('comments.title') %>
<%= badge_tag(@comment.status) %>

<%= t(comment.created_at, format: :long) %>

<div class="prose">
  <%= comment.content %>
</div>

If the output of a helper or translation changes, the cache isn’t invalidated unless the associated data (like the comments) is updated.

The Rails docs suggest adding comments to adjust the MD5 hash for cache keys, but that seems really hard to maintain: https://guides.rubyonrails.org/caching_with_rails.html#external-dependencies.

When I used Redis, the cache size was so small that it frequently invalidated itself. But now, with Solid Cache, it doesn’t.

In your video (https://www.youtube.com/watch?v=wYeVne3aRow), you mentioned using a background job to invalidate caches, but I’m unclear on how you handle this process overall.

I guess we could also configure SolidCache with a max_age that matches our deployment frequency, https://github.com/rails/solid_cache?tab=readme-ov-file#configuration, I don't know..

How do you manage this?

@dhh
Copy link
Member

dhh commented Dec 27, 2024

Do all such translations using JavaScript. Like the local-time gem does.

@dhh dhh closed this as completed Dec 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants