Skip to content

Cache related content #929

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

Open
wants to merge 4 commits into
base: 2024-upgrades-main
Choose a base branch
from
Open

Conversation

esoterik
Copy link
Member

The diff is quite large due to including all the files from a single deeplinks page as a test fixture -- this is not that big of a PR!

Cache related content on action pages. Currently this cache will become stale only when the action page is updated; we could add an expiry time instead if that's what we'd prefer or decide we need (I think blogposts that get attached to actions are usually not changing by the time they're added to an action, but that assumption could be wrong).

Rails.cache.fetch("#{cache_key_with_version}/related_content") do
RelatedContent.as_hash(related_content_url)
end
end
Copy link
Member

Choose a reason for hiding this comment

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

It may be good to let OpenURI::HTTPError bubble up out of RelatedContent and move the rescue to this method now, so that the {} value doesn't get cached in that case.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ended up addressing this by changing RelatedContent to return nil in all fail states & used the skip_nil option on cache.fetch to avoid caching nil

<h4>Related Content</h4>
<%= link_to action_page.related_content_url do %>
<div class="image">
<%= image_tag(action_page.related_content.image) %>
Copy link
Member

Choose a reason for hiding this comment

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

The title/image methods in this view are called on a hash now so it needed some slight changes:

   <h4>Related Content</h4>
   <%= link_to action_page.related_content_url do %>
     <div class="image">
-      <%= image_tag(action_page.related_content.image) %>
+      <%= image_tag(action_page.related_content[:image_url]) %>
     </div>
-    <h5><%= action_page.related_content.title %></h5>
+    <h5><%= action_page.related_content[:title] %></h5>
   <% end %>
 </div>
 <div class="desc-text">
@@ -14,8 +14,8 @@
   <h4>Related Content</h4>
   <%= link_to action_page.related_content_url do %>
     <div class="image">
-      <%= image_tag(action_page.related_content.image) %>
+      <%= image_tag(action_page.related_content[:image_url]) %>
     </div>
-    <h5><%= action_page.related_content.title %></h5>
+    <h5><%= action_page.related_content[:title] %></h5>
   <% end %>
 </div>

@esoterik esoterik force-pushed the cache-related-content branch from 8a7e2c1 to 7ced577 Compare April 4, 2025 04:13
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

Successfully merging this pull request may close these issues.

2 participants