Skip to content

Commit

Permalink
Add article's record key to Fastly Surrogate-Key to separate caches (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
rhymes authored and benhalpern committed Jan 17, 2020
1 parent 4e41e4a commit 1bc0469
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/controllers/api/v0/comments_controller.rb
Expand Up @@ -10,7 +10,7 @@ def index

@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
Expand Down
2 changes: 2 additions & 0 deletions app/jobs/comments/bust_cache_job.rb
Expand Up @@ -7,6 +7,8 @@ def perform(comment_id, service = EdgeCache::Commentable::Bust)
return unless comment&.commentable

comment.purge
comment.commentable.purge

service.call(comment.commentable)
end
end
Expand Down
2 changes: 2 additions & 0 deletions app/workers/comments/bust_cache_worker.rb
Expand Up @@ -9,6 +9,8 @@ def perform(comment_id)
return unless comment&.commentable

comment.purge
comment.commentable.purge

EdgeCache::Commentable::Bust.call(comment.commentable)
end
end
Expand Down
20 changes: 19 additions & 1 deletion spec/jobs/comments/bust_cache_job_spec.rb
Expand Up @@ -18,6 +18,7 @@
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

Expand All @@ -27,7 +28,24 @@
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)
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/api/v0/comments_spec.rb
Expand Up @@ -55,7 +55,7 @@ def json_response
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
Expand Down
22 changes: 20 additions & 2 deletions spec/workers/comments/bust_cache_worker_spec.rb
Expand Up @@ -6,7 +6,7 @@
describe "#perform" do
let(:worker) { subject }

before(:each) do
before do
allow(EdgeCache::Commentable::Bust).to receive(:call)
end

Expand All @@ -19,6 +19,7 @@
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
Expand All @@ -27,7 +28,24 @@
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)
Expand Down

0 comments on commit 1bc0469

Please sign in to comment.