Skip to content

Commit

Permalink
Merge pull request #2334 from tvdeyen/recycable-cache-keys
Browse files Browse the repository at this point in the history
Add support for Rails' recycable cache keys
  • Loading branch information
tvdeyen authored May 6, 2022
2 parents a204263 + a8974de commit 82062c2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
10 changes: 5 additions & 5 deletions app/models/alchemy/page/page_natures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,17 @@ def layout_partial_name
page_layout.parameterize.underscore
end

# Returns the key that's taken for cache path.
# 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.
# If the page is the current preview it uses the +updated_at+ value as cache key.
#
def cache_key
def cache_version
if Page.current_preview == id
"alchemy/pages/#{id}-#{updated_at}"
updated_at.to_s
else
"alchemy/pages/#{id}-#{published_at}"
published_at.to_s
end
end

Expand Down
8 changes: 4 additions & 4 deletions spec/models/alchemy/page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -735,15 +735,15 @@ class AnotherUrlPathClass; end
end
end

describe "#cache_key" do
describe "#cache_version" do
let(:now) { Time.current }
let(:last_week) { Time.current - 1.week }

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

subject { page.cache_key }
subject { page.cache_version }

before do
expect(Page).to receive(:current_preview).and_return(preview)
Expand All @@ -753,15 +753,15 @@ class AnotherUrlPathClass; end
let(:preview) { page.id }

it "uses updated_at" do
is_expected.to eq("alchemy/pages/#{page.id}-#{now}")
is_expected.to eq(now.to_s)
end
end

context "when current page not in preview mode" do
let(:preview) { nil }

it "uses published_at" do
is_expected.to eq("alchemy/pages/#{page.id}-#{last_week}")
is_expected.to eq(last_week.to_s)
end
end
end
Expand Down

0 comments on commit 82062c2

Please sign in to comment.