Skip to content

Commit

Permalink
Use page version's updated_at timestamp as cache key
Browse files Browse the repository at this point in the history
Currently, it's hard for us to invalidate caches for Alchemy pages when
content that's referenced through ingredients with related objects
changes.

For example, in a situation where a user combines Alchemy and Solidus
using the `alchemy_solidus` gem, a page's cache key does not update when
a product that's referenced through a SpreeProduct ingredient changes.

There's a PR up on the alchemy_solidus gem that touches ingredients in
these situations, and with this change, that touching can be used for
breaking caches. [1]

[1](AlchemyCMS/alchemy-solidus#108)
  • Loading branch information
mamhoff committed May 7, 2024
1 parent cbef269 commit 7cdcf7a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
8 changes: 3 additions & 5 deletions app/models/alchemy/page/page_natures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,13 @@ def layout_partial_name

# Returns the version that's taken for Rails' recycable cache key.
#
# Uses the +published_at+ value that's updated when the user publishes the page.
#
# If the page is the current preview it uses the +updated_at+ value as cache key.
# Always uses the +updated_at+ value.
#
def cache_version
if Current.preview_page == self
updated_at.to_s
draft_version.updated_at.to_s
else
published_at.to_s
public_version.updated_at.to_s
end
end

Expand Down
5 changes: 4 additions & 1 deletion spec/models/alchemy/page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -708,9 +708,12 @@ module Alchemy
let(:last_week) { Time.current - 1.week }

let(:page) do
build_stubbed(:alchemy_page, updated_at: now, published_at: last_week)
build_stubbed(:alchemy_page, public_version: public_version, draft_version: draft_version)
end

let(:public_version) { build_stubbed(:alchemy_page_version, updated_at: last_week) }
let(:draft_version) { build_stubbed(:alchemy_page_version, updated_at: now) }

subject { page.cache_version }

before do
Expand Down

0 comments on commit 7cdcf7a

Please sign in to comment.