From f70af6625870db3613b411f5af8286154c19a1e1 Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 29 Dec 2020 03:53:15 -0500 Subject: [PATCH] Update bust to EdgeCache::Bust (#12052) * Update bust to EdgeCache::Bust * Update specs * Fix more specs * Fix more specs :) --- app/controllers/admin/tools_controller.rb | 8 ++++---- app/controllers/application_controller.rb | 12 ++++++------ app/controllers/profile_pins_controller.rb | 4 ++-- app/models/article.rb | 6 +++--- app/models/badge.rb | 4 ++-- app/models/comment.rb | 2 +- app/models/github_repo.rb | 6 +++--- app/models/user.rb | 4 ++-- app/services/moderator/banish_user.rb | 2 +- app/services/moderator/merge_user.rb | 2 +- app/services/users/delete.rb | 2 +- app/workers/articles/bust_multiple_caches_worker.rb | 4 ++-- app/workers/bust_cache_path_worker.rb | 2 +- app/workers/reactions/bust_homepage_cache_worker.rb | 9 ++++----- app/workers/reactions/bust_reactable_cache_worker.rb | 6 +++--- spec/models/badge_spec.rb | 6 +++--- spec/models/github_repo_spec.rb | 8 ++++---- spec/services/users/delete_spec.rb | 4 ++-- .../articles/bust_multiple_caches_worker_spec.rb | 6 +++--- spec/workers/bust_cache_path_worker_spec.rb | 8 +++++--- .../reactions/bust_homepage_cache_worker_spec.rb | 8 ++++---- .../reactions/bust_reactable_cache_worker_spec.rb | 10 +++++----- 22 files changed, 62 insertions(+), 61 deletions(-) diff --git a/app/controllers/admin/tools_controller.rb b/app/controllers/admin/tools_controller.rb index c6313a0c0..3963f5e12 100644 --- a/app/controllers/admin/tools_controller.rb +++ b/app/controllers/admin/tools_controller.rb @@ -43,10 +43,10 @@ module Admin if link.starts_with?(URL.url) link.sub!(URL.url, "") end - CacheBuster.bust(link) - CacheBuster.bust("#{link}/") - CacheBuster.bust("#{link}?i=i") - CacheBuster.bust("#{link}/?i=i") + EdgeCache::Bust.call(link) + EdgeCache::Bust.call("#{link}/") + EdgeCache::Bust.call("#{link}?i=i") + EdgeCache::Bust.call("#{link}/?i=i") end end end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 94326e27a..72d1587f4 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -178,12 +178,12 @@ class ApplicationController < ActionController::Base end def bust_content_change_caches - CacheBuster.bust("/tags/onboarding") # Needs to change when suggested_tags is edited. - CacheBuster.bust("/shell_top") # Cached at edge, sent to service worker. - CacheBuster.bust("/shell_bottom") # Cached at edge, sent to service worker. - CacheBuster.bust("/async_info/shell_version") # Checks if current users should be busted. - CacheBuster.bust("/onboarding") # Page is cached at edge. - CacheBuster.bust("/") # Page is cached at edge. + EdgeCache::Bust.call("/tags/onboarding") # Needs to change when suggested_tags is edited. + EdgeCache::Bust.call("/shell_top") # Cached at edge, sent to service worker. + EdgeCache::Bust.call("/shell_bottom") # Cached at edge, sent to service worker. + EdgeCache::Bust.call("/async_info/shell_version") # Checks if current users should be busted. + EdgeCache::Bust.call("/onboarding") # Page is cached at edge. + EdgeCache::Bust.call("/") # Page is cached at edge. SiteConfig.admin_action_taken_at = Time.current # Used as cache key end diff --git a/app/controllers/profile_pins_controller.rb b/app/controllers/profile_pins_controller.rb index 1d74855d2..1caa55bf3 100644 --- a/app/controllers/profile_pins_controller.rb +++ b/app/controllers/profile_pins_controller.rb @@ -31,7 +31,7 @@ class ProfilePinsController < ApplicationController end def bust_user_profile - CacheBuster.bust(current_user.path) - CacheBuster.bust("#{current_user.path}?i=i") + EdgeCache::Bust.call(current_user.path) + EdgeCache::Bust.call("#{current_user.path}?i=i") end end diff --git a/app/models/article.rb b/app/models/article.rb index 41c930d58..c797dae05 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -665,9 +665,9 @@ class Article < ApplicationRecord end def bust_cache - CacheBuster.bust(path) - CacheBuster.bust("#{path}?i=i") - CacheBuster.bust("#{path}?preview=#{password}") + EdgeCache::Bust.call(path) + EdgeCache::Bust.call("#{path}?i=i") + EdgeCache::Bust.call("#{path}?preview=#{password}") async_bust end diff --git a/app/models/badge.rb b/app/models/badge.rb index 2b6d79669..a7cf3fdf1 100644 --- a/app/models/badge.rb +++ b/app/models/badge.rb @@ -29,7 +29,7 @@ class Badge < ApplicationRecord end def bust_path - CacheBuster.bust(path) - CacheBuster.bust("#{path}?i=i") + EdgeCache::Bust.call(path) + EdgeCache::Bust.call("#{path}?i=i") end end diff --git a/app/models/comment.rb b/app/models/comment.rb index 1662d0a7b..de127f2b1 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -238,7 +238,7 @@ class Comment < ApplicationRecord def synchronous_bust commentable.touch(:last_comment_at) if commentable.respond_to?(:last_comment_at) user.touch(:last_comment_at) - CacheBuster.bust(commentable.path.to_s) if commentable + EdgeCache::Bust.call(commentable.path.to_s) if commentable expire_root_fragment end diff --git a/app/models/github_repo.rb b/app/models/github_repo.rb index 2d567d3a6..54dd70411 100644 --- a/app/models/github_repo.rb +++ b/app/models/github_repo.rb @@ -38,8 +38,8 @@ class GithubRepo < ApplicationRecord return if user.blank? user.touch - CacheBuster.bust(user.path) - CacheBuster.bust("#{user.path}?i=i") - CacheBuster.bust("#{user.path}/?i=i") + EdgeCache::Bust.call(user.path) + EdgeCache::Bust.call("#{user.path}?i=i") + EdgeCache::Bust.call("#{user.path}/?i=i") end end diff --git a/app/models/user.rb b/app/models/user.rb index d6d2e9614..351dd1b55 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -489,8 +489,8 @@ class User < ApplicationRecord def resave_articles articles.find_each do |article| if article.path - CacheBuster.bust(article.path) - CacheBuster.bust("#{article.path}?i=i") + EdgeCache::Bust.call(article.path) + EdgeCache::Bust.call("#{article.path}?i=i") end article.save end diff --git a/app/services/moderator/banish_user.rb b/app/services/moderator/banish_user.rb index 395b19023..c8b88c81b 100644 --- a/app/services/moderator/banish_user.rb +++ b/app/services/moderator/banish_user.rb @@ -37,7 +37,7 @@ module Moderator end user.update_columns(name: new_name, username: new_username, old_username: user.username, profile_updated_at: Time.current) - CacheBuster.bust("/#{user.old_username}") + EdgeCache::Bust.call("/#{user.old_username}") end def remove_profile_info diff --git a/app/services/moderator/merge_user.rb b/app/services/moderator/merge_user.rb index a8a868ea5..acc1a6d69 100644 --- a/app/services/moderator/merge_user.rb +++ b/app/services/moderator/merge_user.rb @@ -25,7 +25,7 @@ module Moderator @keep_user.touch(:profile_updated_at) Users::MergeSyncWorker.perform_async(@keep_user.id) - CacheBuster.bust("/#{@keep_user.username}") + EdgeCache::Bust.call("/#{@keep_user.username}") end private diff --git a/app/services/users/delete.rb b/app/services/users/delete.rb index 11f43caa4..f4bb6da42 100644 --- a/app/services/users/delete.rb +++ b/app/services/users/delete.rb @@ -9,7 +9,7 @@ module Users delete_articles delete_user_activity user.unsubscribe_from_newsletters - CacheBuster.bust("/#{user.username}") + EdgeCache::Bust.call("/#{user.username}") user.destroy Rails.cache.delete("user-destroy-token-#{user.id}") end diff --git a/app/workers/articles/bust_multiple_caches_worker.rb b/app/workers/articles/bust_multiple_caches_worker.rb index 5ecd7ef01..2f290cf81 100644 --- a/app/workers/articles/bust_multiple_caches_worker.rb +++ b/app/workers/articles/bust_multiple_caches_worker.rb @@ -5,8 +5,8 @@ module Articles def perform(article_ids) Article.select(:id, :path).where(id: article_ids).find_each do |article| - CacheBuster.bust(article.path) - CacheBuster.bust("#{article.path}?i=i") + EdgeCache::Bust.call(article.path) + EdgeCache::Bust.call("#{article.path}?i=i") end end end diff --git a/app/workers/bust_cache_path_worker.rb b/app/workers/bust_cache_path_worker.rb index 2e1b5270f..da1fc0566 100644 --- a/app/workers/bust_cache_path_worker.rb +++ b/app/workers/bust_cache_path_worker.rb @@ -1,5 +1,5 @@ class BustCachePathWorker < BustCacheBaseWorker def perform(path) - CacheBuster.bust(path) + EdgeCache::Bust.call(path) end end diff --git a/app/workers/reactions/bust_homepage_cache_worker.rb b/app/workers/reactions/bust_homepage_cache_worker.rb index 09de1ef3b..22c9ef150 100644 --- a/app/workers/reactions/bust_homepage_cache_worker.rb +++ b/app/workers/reactions/bust_homepage_cache_worker.rb @@ -5,7 +5,6 @@ module Reactions sidekiq_options queue: :high_priority, retry: 10 def perform(reaction_id) - cache_buster = CacheBuster reaction = Reaction.find_by(id: reaction_id, reactable_type: "Article") return unless reaction&.reactable @@ -13,10 +12,10 @@ module Reactions return unless featured_articles_ids.include?(reaction.reactable_id) reaction.reactable.touch - cache_buster.bust("/") - cache_buster.bust("/") - cache_buster.bust("/?i=i") - cache_buster.bust("?i=i") + EdgeCache::Bust.call("/") + EdgeCache::Bust.call("/") + EdgeCache::Bust.call("/?i=i") + EdgeCache::Bust.call("?i=i") end end end diff --git a/app/workers/reactions/bust_reactable_cache_worker.rb b/app/workers/reactions/bust_reactable_cache_worker.rb index 3d76a7b38..41b3cc31f 100644 --- a/app/workers/reactions/bust_reactable_cache_worker.rb +++ b/app/workers/reactions/bust_reactable_cache_worker.rb @@ -8,14 +8,14 @@ module Reactions reaction = Reaction.find_by(id: reaction_id) return unless reaction&.reactable - CacheBuster.bust(reaction.user.path) + EdgeCache::Bust.call(reaction.user.path) case reaction.reactable_type when "Article" - CacheBuster.bust("/reactions?article_id=#{reaction.reactable_id}") + EdgeCache::Bust.call("/reactions?article_id=#{reaction.reactable_id}") when "Comment" path = "/reactions?commentable_id=#{reaction.reactable.commentable_id}&" \ "commentable_type=#{reaction.reactable.commentable_type}" - CacheBuster.bust(path) + EdgeCache::Bust.call(path) end end end diff --git a/spec/models/badge_spec.rb b/spec/models/badge_spec.rb index 70aa259a8..a2dfb44da 100644 --- a/spec/models/badge_spec.rb +++ b/spec/models/badge_spec.rb @@ -35,19 +35,19 @@ RSpec.describe Badge, type: :model do describe "cache busting" do before do - allow(CacheBuster).to receive(:bust) + allow(EdgeCache::Bust).to receive(:bust) end it "calls the cache buster with the path" do badge.save - expect(CacheBuster).to have_received(:bust).with(badge.path) + expect(EdgeCache::Bust).to have_received(:bust).with(badge.path) end it "calls the cache buster with the internal path" do badge.save - expect(CacheBuster).to have_received(:bust).with("#{badge.path}?i=i") + expect(EdgeCache::Bust).to have_received(:bust).with("#{badge.path}?i=i") end end end diff --git a/spec/models/github_repo_spec.rb b/spec/models/github_repo_spec.rb index 8ffe533e4..9ac6b6b67 100644 --- a/spec/models/github_repo_spec.rb +++ b/spec/models/github_repo_spec.rb @@ -36,13 +36,13 @@ RSpec.describe GithubRepo, type: :model do end it "busts the correct caches" do - allow(CacheBuster).to receive(:bust) + allow(EdgeCache::Bust).to receive(:call) repo.save - expect(CacheBuster).to have_received(:bust).with(user.path) - expect(CacheBuster).to have_received(:bust).with("#{user.path}?i=i") - expect(CacheBuster).to have_received(:bust).with("#{user.path}/?i=i") + expect(EdgeCache::Bust).to have_received(:call).with(user.path) + expect(EdgeCache::Bust).to have_received(:call).with("#{user.path}?i=i") + expect(EdgeCache::Bust).to have_received(:call).with("#{user.path}/?i=i") end end end diff --git a/spec/services/users/delete_spec.rb b/spec/services/users/delete_spec.rb index 6af2c90a0..287b85752 100644 --- a/spec/services/users/delete_spec.rb +++ b/spec/services/users/delete_spec.rb @@ -14,9 +14,9 @@ RSpec.describe Users::Delete, type: :service do end it "busts user profile page" do - allow(CacheBuster).to receive(:bust) + allow(EdgeCache::Bust).to receive(:call).with("/#{user.username}") described_class.new(user).call - expect(CacheBuster).to have_received(:bust).with("/#{user.username}") + expect(EdgeCache::Bust).to have_received(:call).with("/#{user.username}") end it "deletes user's follows" do diff --git a/spec/workers/articles/bust_multiple_caches_worker_spec.rb b/spec/workers/articles/bust_multiple_caches_worker_spec.rb index 1ca019d85..72c5231a6 100644 --- a/spec/workers/articles/bust_multiple_caches_worker_spec.rb +++ b/spec/workers/articles/bust_multiple_caches_worker_spec.rb @@ -9,12 +9,12 @@ RSpec.describe Articles::BustMultipleCachesWorker, type: :worker do let(:worker) { subject } it "busts cache" do - allow(CacheBuster).to receive(:bust) + allow(EdgeCache::Bust).to receive(:call) worker.perform([article.id]) - expect(CacheBuster).to have_received(:bust).with(path).once - expect(CacheBuster).to have_received(:bust).with("#{path}?i=i").once + expect(EdgeCache::Bust).to have_received(:call).with(path).once + expect(EdgeCache::Bust).to have_received(:call).with("#{path}?i=i").once end end end diff --git a/spec/workers/bust_cache_path_worker_spec.rb b/spec/workers/bust_cache_path_worker_spec.rb index 482bd16a8..00bd68367 100644 --- a/spec/workers/bust_cache_path_worker_spec.rb +++ b/spec/workers/bust_cache_path_worker_spec.rb @@ -6,10 +6,12 @@ RSpec.describe BustCachePathWorker, type: :worker do include_examples "#enqueues_on_correct_queue", "high_priority" describe "#perform" do + let(:path) { "/foo" } + it "busts cache for given path" do - allow(CacheBuster).to receive(:bust) - worker.perform("/foo") - expect(CacheBuster).to have_received(:bust).with("/foo") + allow(EdgeCache::Bust).to receive(:call).with(path) + worker.perform(path) + expect(EdgeCache::Bust).to have_received(:call).with(path) end end end diff --git a/spec/workers/reactions/bust_homepage_cache_worker_spec.rb b/spec/workers/reactions/bust_homepage_cache_worker_spec.rb index 94dc8e913..20350d2f3 100644 --- a/spec/workers/reactions/bust_homepage_cache_worker_spec.rb +++ b/spec/workers/reactions/bust_homepage_cache_worker_spec.rb @@ -8,21 +8,21 @@ RSpec.describe Reactions::BustHomepageCacheWorker, type: :worker do it "busts the homepage cache when reactable is an Article" do reaction = create(:reaction, reactable: article, user: user) - allow(CacheBuster).to receive(:bust) + allow(EdgeCache::Bust).to receive(:call) worker.perform(reaction.id) - expect(CacheBuster).to have_received(:bust).exactly(4) + expect(EdgeCache::Bust).to have_received(:call).exactly(4) end it "doesn't bust the homepage cache when reactable is a Comment" do comment = create(:comment, commentable: article) comment_reaction = create(:reaction, reactable: comment, user: user) - allow(CacheBuster).to receive(:bust) + allow(EdgeCache::Bust).to receive(:call) worker.perform(comment_reaction.id) - expect(CacheBuster).not_to have_received(:bust) + expect(EdgeCache::Bust).not_to have_received(:call) end it "doesn't fail if a reaction doesn't exist" do diff --git a/spec/workers/reactions/bust_reactable_cache_worker_spec.rb b/spec/workers/reactions/bust_reactable_cache_worker_spec.rb index df5c4ffef..95af75b1d 100644 --- a/spec/workers/reactions/bust_reactable_cache_worker_spec.rb +++ b/spec/workers/reactions/bust_reactable_cache_worker_spec.rb @@ -10,20 +10,20 @@ RSpec.describe Reactions::BustReactableCacheWorker, type: :worker do let(:worker) { subject } before do - allow(CacheBuster).to receive(:bust) + allow(EdgeCache::Bust).to receive(:call) end it "busts the reactable article cache" do worker.perform(reaction.id) - expect(CacheBuster).to have_received(:bust).with(user.path).once - expect(CacheBuster).to have_received(:bust).with("/reactions?article_id=#{article.id}").once + expect(EdgeCache::Bust).to have_received(:call).with(user.path).once + expect(EdgeCache::Bust).to have_received(:call).with("/reactions?article_id=#{article.id}").once end it "busts the reactable comment cache" do worker.perform(comment_reaction.id) - expect(CacheBuster).to have_received(:bust).with(user.path).once + expect(EdgeCache::Bust).to have_received(:call).with(user.path).once param = "/reactions?commentable_id=#{article.id}&commentable_type=Article" - expect(CacheBuster).to have_received(:bust).with(param).once + expect(EdgeCache::Bust).to have_received(:call).with(param).once end it "doesn't fail if a reaction doesn't exist" do