Add article's record key to Fastly Surrogate-Key to separate caches (#5543)

* Add article's record key to Fastly Surrogate-Key to separate caches

After #4744 we successfully setup an auto purging system based on Fastly's surrogate key based cache and purging API.
Unfortunately this worked only in the `:show` action as comments are their own tree-based resource.
The `:index` though, is related to a single article, so without the article's record key in the `Surrogate-Key` header the first time we hit the API the result would become the value stored in Fastly's API, for all articles.

* Fix commentable mock for job spec

* Add comment.commentable.purge to the worker as well

* Add more thorough tests for commentable
This commit is contained in:
rhymes 2020-01-17 14:45:47 +01:00 committed by Ben Halpern
parent 4e41e4a2ac
commit 1bc0469b2d
6 changed files with 45 additions and 5 deletions

View file

@ -10,7 +10,7 @@ module Api
@comments = article.comments.includes(:user).select(%i[id processed_html user_id ancestry]).arrange
set_surrogate_key_header "comments", edge_cache_keys(@comments)
set_surrogate_key_header article.record_key, "comments", edge_cache_keys(@comments)
end
def show

View file

@ -7,6 +7,8 @@ module Comments
return unless comment&.commentable
comment.purge
comment.commentable.purge
service.call(comment.commentable)
end
end

View file

@ -9,6 +9,8 @@ module Comments
return unless comment&.commentable
comment.purge
comment.commentable.purge
EdgeCache::Commentable::Bust.call(comment.commentable)
end
end

View file

@ -18,6 +18,7 @@ RSpec.describe Comments::BustCacheJob, type: :job do
before do
allow(comment).to receive(:commentable).and_return(commentable)
allow(comment).to receive(:purge)
allow(commentable).to receive(:purge)
allow(Comment).to receive(:find_by).with(id: comment_id).and_return(comment)
end
@ -27,7 +28,24 @@ RSpec.describe Comments::BustCacheJob, type: :job do
expect(edge_cache_commentable_bust_service).to have_received(:call).with(comment.commentable).once
end
it "does not call the service with a comment without a commentable" do
it "does not call purge on comment when commentable is not available" do
allow(comment).to receive(:commentable).and_return(nil)
described_class.perform_now(comment_id, edge_cache_commentable_bust_service)
expect(comment).not_to have_received(:purge)
expect(commentable).not_to have_received(:purge)
end
it "does not call purge on commentable when commentable is not available" do
allow(comment).to receive(:commentable).and_return(nil)
described_class.perform_now(comment_id, edge_cache_commentable_bust_service)
expect(commentable).not_to have_received(:purge)
end
it "does not call the service when commentable is not available" do
allow(comment).to receive(:commentable).and_return(nil)
described_class.perform_now(comment_id, edge_cache_commentable_bust_service)

View file

@ -55,7 +55,7 @@ RSpec.describe "Api::V0::Comments", type: :request do
get "/api/comments?a_id=#{article.id}"
expected_key = [
"comments", sibling_root_comment.record_key,
article.record_key, "comments", sibling_root_comment.record_key,
root_comment.record_key, child_comment.record_key,
grandchild_comment.record_key, great_grandchild_comment.record_key
].to_set

View file

@ -6,7 +6,7 @@ RSpec.describe Comments::BustCacheWorker, type: :worker do
describe "#perform" do
let(:worker) { subject }
before(:each) do
before do
allow(EdgeCache::Commentable::Bust).to receive(:call)
end
@ -19,6 +19,7 @@ RSpec.describe Comments::BustCacheWorker, type: :worker do
allow(comment).to receive(:commentable).and_return(commentable)
allow(Comment).to receive(:find_by).with(id: comment_id).and_return(comment)
allow(comment).to receive(:purge)
allow(commentable).to receive(:purge)
end
it "calls the service" do
@ -27,7 +28,24 @@ RSpec.describe Comments::BustCacheWorker, type: :worker do
expect(EdgeCache::Commentable::Bust).to have_received(:call).with(comment.commentable).once
end
it "does not call the service with a comment without a commentable" do
it "does not call purge on comment when commentable is not available" do
allow(comment).to receive(:commentable).and_return(nil)
worker.perform(comment_id)
expect(comment).not_to have_received(:purge)
expect(commentable).not_to have_received(:purge)
end
it "does not call purge on commentable when commentable is not available" do
allow(comment).to receive(:commentable).and_return(nil)
worker.perform(comment_id)
expect(commentable).not_to have_received(:purge)
end
it "does not call the service when commentable is not available" do
allow(comment).to receive(:commentable).and_return(nil)
worker.perform(comment_id)