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]

In the unlikely event the requested version is not available, we use the
page's updated_at timestamp as a fallback.

[1](AlchemyCMS/alchemy-solidus#108)
  • Loading branch information
mamhoff committed May 8, 2024
1 parent cbef269 commit 215ba02
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
11 changes: 3 additions & 8 deletions app/models/alchemy/page/page_natures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,11 @@ 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
else
published_at.to_s
end
relevant_page_version = (Current.preview_page == self) ? draft_version : public_version
(relevant_page_version&.updated_at || updated_at).to_s
end

# Returns true if the page cache control headers should be set.
Expand Down
25 changes: 24 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 All @@ -732,6 +735,26 @@ module Alchemy
is_expected.to eq(last_week.to_s)
end
end

context "if page has no public version" do
context "in preview mode" do
let(:page) { build_stubbed(:alchemy_page, public_version: nil, draft_version: draft_version, updated_at: now) }
let(:preview) { page }

it "uses updated_at" do
is_expected.to eq(now.to_s)
end
end

context "not in preview mode" do
let(:page) { build_stubbed(:alchemy_page, public_version: nil, draft_version: draft_version, updated_at: now) }
let(:preview) { nil }

it "uses updated_at" do
is_expected.to eq(now.to_s)
end
end
end
end

describe "#public_version" do
Expand Down

0 comments on commit 215ba02

Please sign in to comment.