Refactor cache purging (#12811)

* Refactor caching

* Bring back old bust class

* Refactor with PR feedback

* Remove useless return values
This commit is contained in:
Alex 2021-03-02 10:09:05 -05:00 committed by GitHub
parent 2d4644d258
commit d109b6824e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
43 changed files with 340 additions and 219 deletions

View file

@ -43,10 +43,15 @@ module Admin
if link.starts_with?(URL.url)
link.sub!(URL.url, "")
end
EdgeCache::Bust.call(link)
EdgeCache::Bust.call("#{link}/")
EdgeCache::Bust.call("#{link}?i=i")
EdgeCache::Bust.call("#{link}/?i=i")
paths = [
link,
"#{link}/",
"#{link}?i=i",
"#{link}/?i=i",
]
EdgeCache::Bust.call(paths)
end
end
end

View file

@ -41,6 +41,19 @@ class ApplicationController < ActionController::Base
health_checks].freeze
private_constant :PUBLIC_CONTROLLERS
# TODO: Remove the "shell" endpoints, because they are for service worker
# functionality we no longer need. We are keeping these around mid-March
# 2021 because previously-installed service workers may still expect them.
CONTENT_CHANGE_PATHS = [
"/tags/onboarding", # Needs to change when suggested_tags is edited.
"/shell_top", # Cached at edge, sent to service worker.
"/shell_bottom", # Cached at edge, sent to service worker.
"/async_info/shell_version", # Checks if current users should be busted.
"/onboarding", # Page is cached at edge.
"/", # Page is cached at edge.
].freeze
private_constant :CONTENT_CHANGE_PATHS
def verify_private_forem
return if controller_name.in?(PUBLIC_CONTROLLERS)
return if self.class.module_parent.to_s == "Admin"
@ -185,14 +198,7 @@ class ApplicationController < ActionController::Base
end
def bust_content_change_caches
EdgeCache::Bust.call("/tags/onboarding") # Needs to change when suggested_tags is edited.
# TODO: Remove these "shell" endpoints, because they are for service worker functionality we no longer need.
# We are keeping these around mid-March 2021 because previously-installed service workers may still expect them.
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.
EdgeCache::Bust.call(CONTENT_CHANGE_PATHS)
SiteConfig.admin_action_taken_at = Time.current # Used as cache key
end

View file

@ -31,7 +31,8 @@ class ProfilePinsController < ApplicationController
end
def bust_user_profile
EdgeCache::Bust.call(current_user.path)
EdgeCache::Bust.call("#{current_user.path}?i=i")
cache_bust = EdgeCache::Bust.new
cache_bust.call(current_user.path)
cache_bust.call("#{current_user.path}?i=i")
end
end

View file

@ -664,9 +664,10 @@ class Article < ApplicationRecord
end
def bust_cache
EdgeCache::Bust.call(path)
EdgeCache::Bust.call("#{path}?i=i")
EdgeCache::Bust.call("#{path}?preview=#{password}")
cache_bust = EdgeCache::Bust.new
cache_bust.call(path)
cache_bust.call("#{path}?i=i")
cache_bust.call("#{path}?preview=#{password}")
async_bust
touch_actor_latest_article_updated_at
end

View file

@ -29,7 +29,8 @@ class Badge < ApplicationRecord
end
def bust_path
EdgeCache::Bust.call(path)
EdgeCache::Bust.call("#{path}?i=i")
cache_bust = EdgeCache::Bust.new
cache_bust.call(path)
cache_bust.call("#{path}?i=i")
end
end

View file

@ -38,8 +38,9 @@ class GithubRepo < ApplicationRecord
return if user.blank?
user.touch
EdgeCache::Bust.call(user.path)
EdgeCache::Bust.call("#{user.path}?i=i")
EdgeCache::Bust.call("#{user.path}/?i=i")
cache_bust = EdgeCache::Bust.new
cache_bust.call(user.path)
cache_bust.call("#{user.path}?i=i")
cache_bust.call("#{user.path}/?i=i")
end
end

View file

@ -481,8 +481,9 @@ class User < ApplicationRecord
def resave_articles
articles.find_each do |article|
if article.path
EdgeCache::Bust.call(article.path)
EdgeCache::Bust.call("#{article.path}?i=i")
cache_bust = EdgeCache::Bust.new
cache_bust.call(article.path)
cache_bust.call("#{article.path}?i=i")
end
article.save
end

View file

@ -1,62 +1,64 @@
module EdgeCache
class Bust
TIMEFRAMES = [
[-> { 1.week.ago }, "week"],
[-> { 1.month.ago }, "month"],
[-> { 1.year.ago }, "year"],
[-> { 5.years.ago }, "infinity"],
].freeze
def self.call(path)
bust(path)
def initialize
@provider_class = determine_provider_class
end
class << self
protected
def self.call(*paths)
new.call(*paths)
end
def bust(path)
provider_class = determine_provider_class
def call(paths)
return unless @provider_class
return unless provider_class
if provider_class.respond_to?(:call)
provider_class.call(path)
true
else
Rails.logger.warn("#{provider_class} cannot be used without a #call implementation!")
ForemStatsClient.increment("edgecache_bust.invalid_provider_class",
tags: ["provider_class:#{provider_class}"])
false
end
paths = Array.wrap(paths)
paths.each do |path|
@provider_class.call(path)
rescue StandardError => e
Honeybadger.notify(e)
ForemStatsClient.increment(
"edgecache_bust.provider_error",
tags: ["provider_class:#{@provider_class}", "error_class:#{e.class}"],
)
end
end
def self.determine_provider_class
private
def determine_provider_class
provider =
if fastly_enabled?
"fastly"
elsif nginx_enabled?
elsif nginx_enabled_and_available?
"nginx"
end
return unless provider
const_get(provider.capitalize)
self.class.const_get(provider.capitalize)
end
private_class_method :determine_provider_class
def self.fastly_enabled?
def fastly_enabled?
ApplicationConfig["FASTLY_API_KEY"].present? && ApplicationConfig["FASTLY_SERVICE_ID"].present?
end
private_class_method :fastly_enabled?
def nginx_enabled_and_available?
return false if ApplicationConfig["OPENRESTY_URL"].blank?
def self.nginx_enabled?
ApplicationConfig["OPENRESTY_URL"].present?
uri = URI.parse(ApplicationConfig["OPENRESTY_URL"])
http = Net::HTTP.new(uri.host, uri.port)
response = http.get(uri.request_uri)
return true if response.is_a?(Net::HTTPSuccess)
rescue StandardError
# If we can't connect to OpenResty, alert ourselves that it is
# unavailable and return false.
Rails.logger.error("Could not connect to OpenResty via #{ApplicationConfig['OPENRESTY_URL']}!")
Honeybadger.notify(e)
ForemStatsClient.increment("edgecache_bust.service_unavailable",
tags: ["path:#{ApplicationConfig['OPENRESTY_URL']}"])
end
private_class_method :nginx_enabled?
false
end
end

View file

@ -1,78 +1,87 @@
module EdgeCache
class BustArticle < Bust
class BustArticle
TIMEFRAMES = [
[-> { 1.week.ago }, "week"],
[-> { 1.month.ago }, "month"],
[-> { 1.year.ago }, "year"],
[-> { 5.years.ago }, "infinity"],
].freeze
def self.call(article)
return unless article
article.purge
bust(article.path)
bust("/#{article.user.username}")
bust("#{article.path}/")
bust("#{article.path}?i=i")
bust("#{article.path}/?i=i")
bust("#{article.path}/comments")
bust("#{article.path}?preview=#{article.password}")
bust("#{article.path}?preview=#{article.password}&i=i")
bust("/#{article.organization.slug}") if article.organization.present?
bust_home_pages(article)
bust_tag_pages(article)
bust("/api/articles/#{article.id}")
cache_bust = EdgeCache::Bust.new
cache_bust.call(article.path)
cache_bust.call("/#{article.user.username}")
cache_bust.call("#{article.path}/")
cache_bust.call("#{article.path}?i=i")
cache_bust.call("#{article.path}/?i=i")
cache_bust.call("#{article.path}/comments")
cache_bust.call("#{article.path}?preview=#{article.password}")
cache_bust.call("#{article.path}?preview=#{article.password}&i=i")
cache_bust.call("/#{article.organization.slug}") if article.organization.present?
bust_home_pages(cache_bust, article)
bust_tag_pages(cache_bust, article)
cache_bust.call("/api/articles/#{article.id}")
return unless article.collection_id
article.collection.articles.find_each do |collection_article|
bust(collection_article.path)
cache_bust.call(collection_article.path)
end
end
def self.bust_home_pages(article)
def self.bust_home_pages(cache_bust, article)
if article.featured_number.to_i > Time.current.to_i
bust("/")
bust("?i=i")
cache_bust.call("/")
cache_bust.call("?i=i")
end
if article.video.present? && article.featured_number.to_i > 10.days.ago.to_i
bust("/videos")
bust("/videos?i=i")
cache_bust.call("/videos")
cache_bust.call("/videos?i=i")
end
TIMEFRAMES.each do |timestamp, interval|
next unless Article.published.where("published_at > ?", timestamp.call)
.order(public_reactions_count: :desc).limit(3).ids.include?(article.id)
bust("/top/#{interval}")
bust("/top/#{interval}?i=i")
bust("/top/#{interval}/?i=i")
cache_bust.call("/top/#{interval}")
cache_bust.call("/top/#{interval}?i=i")
cache_bust.call("/top/#{interval}/?i=i")
end
if article.published && article.published_at > 1.hour.ago
bust("/latest")
bust("/latest?i=i")
cache_bust.call("/latest")
cache_bust.call("/latest?i=i")
end
bust("/") if Article.published.order(hotness_score: :desc).limit(4).ids.include?(article.id)
cache_bust.call("/") if Article.published.order(hotness_score: :desc).limit(4).ids.include?(article.id)
end
private_class_method :bust_home_pages
def self.bust_tag_pages(article)
def self.bust_tag_pages(cache_bust, article)
return unless article.published
article.tag_list.each do |tag|
if article.published_at.to_i > 2.minutes.ago.to_i
bust("/t/#{tag}/latest")
bust("/t/#{tag}/latest?i=i")
cache_bust.call("/t/#{tag}/latest")
cache_bust.call("/t/#{tag}/latest?i=i")
end
TIMEFRAMES.each do |timestamp, interval|
next unless Article.published.where("published_at > ?", timestamp.call).tagged_with(tag)
.order(public_reactions_count: :desc).limit(3).ids.include?(article.id)
bust("/top/#{interval}")
bust("/top/#{interval}?i=i")
bust("/top/#{interval}/?i=i")
cache_bust.call("/top/#{interval}")
cache_bust.call("/top/#{interval}?i=i")
cache_bust.call("/top/#{interval}/?i=i")
12.times do |i|
bust("/api/articles?tag=#{tag}&top=#{i}")
cache_bust.call("/api/articles?tag=#{tag}&top=#{i}")
end
end
@ -80,8 +89,8 @@ module EdgeCache
Article.published.tagged_with(tag)
.order(hotness_score: :desc).limit(2).ids.include?(article.id)
bust("/t/#{tag}")
bust("/t/#{tag}?i=i")
cache_bust.call("/t/#{tag}")
cache_bust.call("/t/#{tag}?i=i")
end
end

View file

@ -1,31 +1,32 @@
module EdgeCache
class BustComment < Bust
class BustComment
def self.call(commentable)
return unless commentable
bust_article_comment(commentable) if commentable.is_a?(Article)
cache_bust = EdgeCache::Bust.new
bust_article_comment(cache_bust, commentable) if commentable.is_a?(Article)
commentable.touch(:last_comment_at) if commentable.respond_to?(:last_comment_at)
bust("#{commentable.path}/comments/")
bust(commentable.path.to_s)
cache_bust.call("#{commentable.path}/comments/")
cache_bust.call(commentable.path.to_s)
commentable.comments.includes(:user).find_each do |comment|
bust(comment.path)
bust("#{comment.path}?i=i")
cache_bust.call(comment.path)
cache_bust.call("#{comment.path}?i=i")
end
bust("#{commentable.path}/comments/*")
cache_bust.call("#{commentable.path}/comments/*")
end
# bust commentable if it's an article
def self.bust_article_comment(article)
bust("/") if Article.published.order(hotness_score: :desc).limit(3).ids.include?(article.id)
def self.bust_article_comment(cache_bust, article)
cache_bust.call("/") if Article.published.order(hotness_score: :desc).limit(3).ids.include?(article.id)
return unless article.decorate.discussion?
bust("/")
bust("/?i=i")
bust("?i=i")
cache_bust.call("/")
cache_bust.call("/?i=i")
cache_bust.call("?i=i")
end
private_class_method :bust_article_comment

View file

@ -1,10 +1,12 @@
module EdgeCache
class BustCommentable < Bust
class BustCommentable
def self.call(commentable)
return unless commentable
EdgeCache::BustComment.call(commentable)
bust("#{commentable.path}/comments")
cache_bust = EdgeCache::Bust.new
cache_bust.call("#{commentable.path}/comments")
commentable.index_to_elasticsearch_inline
end
end

View file

@ -1,8 +1,9 @@
module EdgeCache
class BustEvents < Bust
class BustEvents
def self.call
bust("/events")
bust("/events?i=i")
cache_bust = EdgeCache::Bust.new
cache_bust.call("/events")
cache_bust.call("/events?i=i")
end
end
end

View file

@ -1,16 +1,17 @@
module EdgeCache
class BustListings < Bust
class BustListings
def self.call(listing)
return unless listing
# we purge all listings as it's the wanted behavior with the following URL purging
listing.purge_all
bust("/listings")
bust("/listings?i=i")
bust("/listings/#{listing.category}/#{listing.slug}")
bust("/listings/#{listing.category}/#{listing.slug}?i=i")
bust("/listings/#{listing.category}")
cache_bust = EdgeCache::Bust.new
cache_bust.call("/listings")
cache_bust.call("/listings?i=i")
cache_bust.call("/listings/#{listing.category}/#{listing.slug}")
cache_bust.call("/listings/#{listing.category}/#{listing.slug}?i=i")
cache_bust.call("/listings/#{listing.category}")
end
end
end

View file

@ -1,13 +1,15 @@
module EdgeCache
class BustOrganization < Bust
class BustOrganization
def self.call(organization, slug)
return unless organization && slug
bust("/#{slug}")
cache_bust = EdgeCache::Bust.new
cache_bust.call("/#{slug}")
begin
organization.articles.find_each do |article|
bust(article.path)
cache_bust.call(article.path)
end
rescue StandardError => e
Rails.logger.error("Tag issue: #{e}")

View file

@ -1,12 +1,13 @@
module EdgeCache
class BustPage < Bust
class BustPage
def self.call(slug)
return unless slug
bust("/page/#{slug}")
bust("/page/#{slug}?i=i")
bust("/#{slug}")
bust("/#{slug}?i=i")
cache_bust = EdgeCache::Bust.new
cache_bust.call("/page/#{slug}")
cache_bust.call("/page/#{slug}?i=i")
cache_bust.call("/#{slug}")
cache_bust.call("/#{slug}?i=i")
end
end
end

View file

@ -1,9 +1,10 @@
module EdgeCache
class BustPodcast < Bust
class BustPodcast
def self.call(path)
return unless path
bust("/#{path}")
cache_bust = EdgeCache::Bust.new
cache_bust.call("/#{path}")
end
end
end

View file

@ -1,5 +1,5 @@
module EdgeCache
class BustPodcastEpisode < Bust
class BustPodcastEpisode
def self.call(podcast_episode, path, podcast_slug)
return unless podcast_episode && path && podcast_slug
@ -7,9 +7,10 @@ module EdgeCache
podcast_episode.purge_all
begin
bust(path)
bust("/#{podcast_slug}")
bust("/pod")
cache_bust = EdgeCache::Bust.new
cache_bust.call(path)
cache_bust.call("/#{podcast_slug}")
cache_bust.call("/pod")
rescue StandardError => e
Rails.logger.warn(e)
end

View file

@ -1,7 +1,8 @@
module EdgeCache
class BustSidebar < Bust
class BustSidebar
def self.call
bust("/sidebars/home")
cache_bust = EdgeCache::Bust.new
cache_bust.call("/sidebars/home")
end
end
end

View file

@ -1,15 +1,16 @@
module EdgeCache
class BustTag < Bust
class BustTag
def self.call(tag)
return unless tag
tag.purge
bust("/t/#{tag.name}")
bust("/t/#{tag.name}?i=i")
bust("/t/#{tag.name}/?i=i")
bust("/t/#{tag.name}/")
bust("/tags")
cache_bust = EdgeCache::Bust.new
cache_bust.call("/t/#{tag.name}")
cache_bust.call("/t/#{tag.name}?i=i")
cache_bust.call("/t/#{tag.name}/?i=i")
cache_bust.call("/t/#{tag.name}/")
cache_bust.call("/tags")
end
end
end

View file

@ -1,5 +1,5 @@
module EdgeCache
class BustUser < Bust
class BustUser
def self.call(user)
return unless user
@ -16,7 +16,8 @@ module EdgeCache
"/feed/#{username}",
]
paths.each { |path| bust(path) }
cache_bust = EdgeCache::Bust.new
paths.each { |path| cache_bust.call(path) }
end
end
end

View file

@ -5,8 +5,9 @@ module Articles
def perform(article_ids)
Article.select(:id, :path).where(id: article_ids).find_each do |article|
EdgeCache::Bust.call(article.path)
EdgeCache::Bust.call("#{article.path}?i=i")
cache_bust = EdgeCache::Bust.new
cache_bust.call(article.path)
cache_bust.call("#{article.path}?i=i")
end
end
end

View file

@ -12,10 +12,11 @@ module Reactions
return unless featured_articles_ids.include?(reaction.reactable_id)
reaction.reactable.touch
EdgeCache::Bust.call("/")
EdgeCache::Bust.call("/")
EdgeCache::Bust.call("/?i=i")
EdgeCache::Bust.call("?i=i")
cache_bust = EdgeCache::Bust.new
cache_bust.call("/")
cache_bust.call("/")
cache_bust.call("/?i=i")
cache_bust.call("?i=i")
end
end
end

View file

@ -8,14 +8,16 @@ module Reactions
reaction = Reaction.find_by(id: reaction_id)
return unless reaction&.reactable
EdgeCache::Bust.call(reaction.user.path)
cache_bust = EdgeCache::Bust.new
cache_bust.call(reaction.user.path)
case reaction.reactable_type
when "Article"
EdgeCache::Bust.call("/reactions?article_id=#{reaction.reactable_id}")
cache_bust.call("/reactions?article_id=#{reaction.reactable_id}")
when "Comment"
path = "/reactions?commentable_id=#{reaction.reactable.commentable_id}&" \
"commentable_type=#{reaction.reactable.commentable_type}"
EdgeCache::Bust.call(path)
cache_bust.call(path)
end
end
end

View file

@ -2,6 +2,7 @@ require "rails_helper"
RSpec.describe Badge, type: :model do
let(:badge) { create(:badge) }
let(:cache_bust) { instance_double(EdgeCache::Bust) }
describe "validations" do
describe "builtin validations" do
@ -35,19 +36,20 @@ RSpec.describe Badge, type: :model do
describe "cache busting" do
before do
allow(EdgeCache::Bust).to receive(:bust)
allow(EdgeCache::Bust).to receive(:new).and_return(cache_bust)
allow(cache_bust).to receive(:call)
end
it "calls the cache buster with the path" do
badge.save
expect(EdgeCache::Bust).to have_received(:bust).with(badge.path)
expect(cache_bust).to have_received(:call).with(badge.path)
end
it "calls the cache buster with the internal path" do
badge.save
expect(EdgeCache::Bust).to have_received(:bust).with("#{badge.path}?i=i")
expect(cache_bust).to have_received(:call).with("#{badge.path}?i=i")
end
end
end

View file

@ -3,9 +3,12 @@ require "rails_helper"
RSpec.describe GithubRepo, type: :model do
let(:user) { create(:user, :with_identity, identities: ["github"]) }
let(:repo) { create(:github_repo, user: user) }
let(:cache_bust) { instance_double(EdgeCache::Bust) }
before do
omniauth_mock_github_payload
allow(EdgeCache::Bust).to receive(:new).and_return(cache_bust)
allow(cache_bust).to receive(:call)
end
describe "validations" do
@ -36,13 +39,11 @@ RSpec.describe GithubRepo, type: :model do
end
it "busts the correct caches" do
allow(EdgeCache::Bust).to receive(:call)
repo.save
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")
expect(cache_bust).to have_received(:call).with(user.path)
expect(cache_bust).to have_received(:call).with("#{user.path}?i=i")
expect(cache_bust).to have_received(:call).with("#{user.path}/?i=i")
end
end
end

View file

@ -2,21 +2,36 @@ require "rails_helper"
RSpec.describe EdgeCache::BustArticle, type: :service do
let(:article) { create(:article) }
let(:cache_bust) { instance_double(EdgeCache::Bust) }
before do
allow(article).to receive(:purge)
allow(described_class).to receive(:bust)
allow(EdgeCache::Bust).to receive(:new).and_return(cache_bust)
allow(cache_bust).to receive(:call)
allow(described_class).to receive(:bust_home_pages)
allow(described_class).to receive(:bust_tag_pages)
end
it "defines TIMEFRAMES" do
expect(described_class.const_defined?(:TIMEFRAMES)).to be true
end
it "adjusts TIMEFRAMES according to the current time" do
current_year = Time.current.year
Timecop.freeze(3.years.ago) do
timestamp, _interval = described_class::TIMEFRAMES.first
expect(timestamp.call.year).to be <= current_year - 3
end
end
it "busts the cache" do
described_class.call(article)
expect(article).to have_received(:purge).once
expect(described_class).to have_received(:bust).exactly(9).times
expect(described_class).to have_received(:bust).with(article.path).once
expect(described_class).to have_received(:bust_home_pages).with(article).once
expect(described_class).to have_received(:bust_tag_pages).with(article).once
expect(cache_bust).to have_received(:call).exactly(9).times
expect(cache_bust).to have_received(:call).with(article.path).once
expect(described_class).to have_received(:bust_home_pages).with(cache_bust, article).once
expect(described_class).to have_received(:bust_tag_pages).with(cache_bust, article).once
end
context "when an article is part of an organization" do
@ -25,7 +40,7 @@ RSpec.describe EdgeCache::BustArticle, type: :service do
article.organization = organization
described_class.call(article)
expect(described_class).to have_received(:bust).with("/#{article.organization.slug}").once
expect(cache_bust).to have_received(:call).with("/#{article.organization.slug}").once
end
end
end

View file

@ -2,21 +2,23 @@ require "rails_helper"
RSpec.describe EdgeCache::BustComment, type: :service do
let(:commentable) { create(:article) }
let(:cache_bust) { instance_double(EdgeCache::Bust) }
before do
allow(described_class).to receive(:bust_article_comment).with(commentable)
allow(described_class).to receive(:bust).and_call_original
allow(EdgeCache::Bust).to receive(:new).and_return(cache_bust)
allow(cache_bust).to receive(:call)
allow(described_class).to receive(:bust_article_comment).with(cache_bust, commentable)
end
it "busts the cache" do
described_class.call(commentable)
expect(described_class).to have_received(:bust).with("#{commentable.path}/comments/*").once
expect(cache_bust).to have_received(:call).with("#{commentable.path}/comments/*").once
end
context "when commentable is an article" do
it "bust article comments" do
described_class.call(commentable)
expect(described_class).to have_received(:bust_article_comment).with(commentable)
expect(described_class).to have_received(:bust_article_comment).with(cache_bust, commentable)
end
end
@ -24,7 +26,7 @@ RSpec.describe EdgeCache::BustComment, type: :service do
it "doesn't bust article comments" do
podcast_episode = create(:podcast_episode)
described_class.call(podcast_episode)
expect(described_class).not_to have_received(:bust_article_comment).with(commentable)
expect(described_class).not_to have_received(:bust_article_comment).with(cache_bust, commentable)
end
end
end

View file

@ -1,17 +1,19 @@
require "rails_helper"
RSpec.describe EdgeCache::BustCommentable, type: :service do
let(:cache_bust) { instance_double(EdgeCache::Bust) }
let(:commentable) { create(:article) }
before do
allow(EdgeCache::BustComment).to receive(:call)
allow(described_class).to receive(:bust).with("#{commentable.path}/comments")
allow(EdgeCache::Bust).to receive(:new).and_return(cache_bust)
allow(cache_bust).to receive(:call)
end
it "busts the cache" do
described_class.call(commentable)
expect(EdgeCache::BustComment).to have_received(:call).with(commentable).once
expect(described_class).to have_received(:bust).with("#{commentable.path}/comments").once
expect(cache_bust).to have_received(:call).with("#{commentable.path}/comments").once
end
it "indexes commentable to Elasticsearch" do

View file

@ -1,6 +1,7 @@
require "rails_helper"
RSpec.describe EdgeCache::BustEvents, type: :service do
let(:cache_bust) { instance_double(EdgeCache::Bust) }
let(:paths) do
[
"/events",
@ -9,8 +10,9 @@ RSpec.describe EdgeCache::BustEvents, type: :service do
end
before do
allow(EdgeCache::Bust).to receive(:new).and_return(cache_bust)
paths.each do |path|
allow(described_class).to receive(:bust).with(path).once
allow(cache_bust).to receive(:call).with(path).once
end
end
@ -18,7 +20,7 @@ RSpec.describe EdgeCache::BustEvents, type: :service do
described_class.call
paths.each do |path|
expect(described_class).to have_received(:bust).with(path).once
expect(cache_bust).to have_received(:call).with(path).once
end
end
end

View file

@ -2,6 +2,7 @@ require "rails_helper"
RSpec.describe EdgeCache::BustListings, type: :service do
let(:listing) { create(:listing) }
let(:cache_bust) { instance_double(EdgeCache::Bust) }
let(:paths) do
[
@ -15,9 +16,10 @@ RSpec.describe EdgeCache::BustListings, type: :service do
before do
allow(listing).to receive(:purge_all)
allow(EdgeCache::Bust).to receive(:new).and_return(cache_bust)
paths.each do |path|
allow(described_class).to receive(:bust).with(path).once
allow(cache_bust).to receive(:call).with(path).once
end
end
@ -27,7 +29,7 @@ RSpec.describe EdgeCache::BustListings, type: :service do
expect(listing).to have_received(:purge_all)
paths.each do |path|
expect(described_class).to have_received(:bust).with(path).once
expect(cache_bust).to have_received(:call).with(path).once
end
end
end

View file

@ -4,21 +4,22 @@ RSpec.describe EdgeCache::BustOrganization, type: :service do
let(:organization) { create(:organization) }
let(:article) { create(:article, organization: organization) }
let(:slug) { "slug" }
let(:cache_bust) { instance_double(EdgeCache::Bust) }
before do
allow(described_class).to receive(:bust).with("/#{slug}").once
allow(described_class).to receive(:bust).with(article.path).once
allow(EdgeCache::Bust).to receive(:new).and_return(cache_bust)
allow(cache_bust).to receive(:call)
end
it "busts the cache" do
described_class.call(organization, slug)
expect(described_class).to have_received(:bust).with("/#{slug}").once
expect(described_class).to have_received(:bust).with(article.path).once
expect(cache_bust).to have_received(:call).with("/#{slug}").once
expect(cache_bust).to have_received(:call).with(article.path).once
end
it "logs an error" do
allow(described_class).to receive(:bust).with("/5").once
allow(cache_bust).to receive(:call).with("/5").once
allow(Rails.logger).to receive(:error)
described_class.call(4, 5)
expect(Rails.logger).to have_received(:error).once

View file

@ -1,6 +1,7 @@
require "rails_helper"
RSpec.describe EdgeCache::BustPage, type: :service do
let(:cache_bust) { instance_double(EdgeCache::Bust) }
let(:slug) { "slug" }
let(:paths) do
[
@ -12,8 +13,10 @@ RSpec.describe EdgeCache::BustPage, type: :service do
end
before do
allow(EdgeCache::Bust).to receive(:new).and_return(cache_bust)
paths.each do |path|
allow(described_class).to receive(:bust).with(path).once
allow(cache_bust).to receive(:call).with(path).once
end
end
@ -21,7 +24,7 @@ RSpec.describe EdgeCache::BustPage, type: :service do
described_class.call(slug)
paths.each do |path|
expect(described_class).to have_received(:bust).with(path).once
expect(cache_bust).to have_received(:call).with(path).once
end
end
end

View file

@ -1,6 +1,7 @@
require "rails_helper"
RSpec.describe EdgeCache::BustPodcastEpisode, type: :service do
let(:cache_bust) { instance_double(EdgeCache::Bust) }
let(:podcast) { create(:podcast) }
let(:podcast_episode) { create(:podcast_episode, podcast_id: podcast.id) }
let(:podcast_path) { "/cfp" }
@ -15,8 +16,10 @@ RSpec.describe EdgeCache::BustPodcastEpisode, type: :service do
end
before do
allow(EdgeCache::Bust).to receive(:new).and_return(cache_bust)
paths.each do |path|
allow(described_class).to receive(:bust).with(path).once
allow(cache_bust).to receive(:call).with(path).once
end
allow(podcast_episode).to receive(:purge)
@ -27,7 +30,7 @@ RSpec.describe EdgeCache::BustPodcastEpisode, type: :service do
described_class.call(podcast_episode, podcast_path, podcast_slug)
paths.each do |path|
expect(described_class).to have_received(:bust).with(path).once
expect(cache_bust).to have_received(:call).with(path).once
end
expect(podcast_episode).to have_received(:purge)
@ -36,7 +39,7 @@ RSpec.describe EdgeCache::BustPodcastEpisode, type: :service do
it "logs an error" do
allow(Rails.logger).to receive(:warn)
allow(described_class).to receive(:bust).and_raise(StandardError)
allow(cache_bust).to receive(:call).and_raise(StandardError)
described_class.call(podcast_episode, 12, podcast_slug)
expect(Rails.logger).to have_received(:warn).once
end

View file

@ -1,6 +1,7 @@
require "rails_helper"
RSpec.describe EdgeCache::BustPodcast, type: :service do
let(:cache_bust) { instance_double(EdgeCache::Bust) }
let(:podcast_path) { "podcast_path" }
let(:paths) do
[
@ -9,8 +10,10 @@ RSpec.describe EdgeCache::BustPodcast, type: :service do
end
before do
allow(EdgeCache::Bust).to receive(:new).and_return(cache_bust)
paths.each do |path|
allow(described_class).to receive(:bust).with(path).once
allow(cache_bust).to receive(:call).with(path).once
end
end
@ -18,7 +21,7 @@ RSpec.describe EdgeCache::BustPodcast, type: :service do
described_class.call(podcast_path)
paths.each do |path|
expect(described_class).to have_received(:bust).with(path).once
expect(cache_bust).to have_received(:call).with(path).once
end
end
end

View file

@ -1,12 +1,15 @@
require "rails_helper"
RSpec.describe EdgeCache::BustSidebar, type: :service do
let(:cache_bust) { instance_double(EdgeCache::Bust) }
before do
allow(described_class).to receive(:bust).with("/sidebars/home").once
allow(EdgeCache::Bust).to receive(:new).and_return(cache_bust)
allow(cache_bust).to receive(:call).with("/sidebars/home").once
end
it "busts the cache" do
described_class.call
expect(described_class).to have_received(:bust).with("/sidebars/home").once
expect(cache_bust).to have_received(:call).with("/sidebars/home").once
end
end

View file

@ -4,16 +4,26 @@ RSpec.describe EdgeCache::Bust, type: :service do
let(:user) { create(:user) }
let(:path) { "/#{user.username}" }
it "defines TIMEFRAMES" do
expect(described_class.const_defined?(:TIMEFRAMES)).to be true
end
context "when passing an Array of paths" do
let(:fastly_provider_class) { EdgeCache::Bust::Fastly }
it "adjusts TIMEFRAMES according to the current time" do
current_year = Time.current.year
before do
configure_fastly
stub_nginx
end
Timecop.freeze(3.years.ago) do
timestamp, _interval = described_class::TIMEFRAMES.first
expect(timestamp.call.year).to be <= current_year - 3
it "busts each path" do
bust_paths = ["/path1", "/path2", "/path3"]
bust_paths.each do |bust_path|
allow(fastly_provider_class).to receive(:call).with(bust_path)
end
described_class.call(bust_paths)
bust_paths.each do |bust_path|
expect(fastly_provider_class).to have_received(:call).with(bust_path)
end
end
end

View file

@ -1,6 +1,7 @@
require "rails_helper"
RSpec.describe EdgeCache::BustTag, type: :service do
let(:cache_bust) { instance_double(EdgeCache::Bust) }
let(:tag) { create(:tag) }
let(:paths) do
[
@ -13,8 +14,10 @@ RSpec.describe EdgeCache::BustTag, type: :service do
end
before do
allow(EdgeCache::Bust).to receive(:new).and_return(cache_bust)
paths.each do |path|
allow(described_class).to receive(:bust).with(path).once
allow(cache_bust).to receive(:call).with(path).once
end
allow(tag).to receive(:purge).once
@ -24,7 +27,7 @@ RSpec.describe EdgeCache::BustTag, type: :service do
described_class.call(tag)
paths.each do |path|
expect(described_class).to have_received(:bust).with(path).once
expect(cache_bust).to have_received(:call).with(path).once
end
expect(tag).to have_received(:purge).once

View file

@ -1,6 +1,7 @@
require "rails_helper"
RSpec.describe EdgeCache::BustUser, type: :service do
let(:cache_bust) { instance_double(EdgeCache::Bust) }
let(:user) { create(:user) }
let(:paths) do
@ -19,8 +20,10 @@ RSpec.describe EdgeCache::BustUser, type: :service do
end
before do
allow(EdgeCache::Bust).to receive(:new).and_return(cache_bust)
paths.each do |path|
allow(described_class).to receive(:bust).with(path).once
allow(cache_bust).to receive(:call).with(path).once
end
end
@ -28,7 +31,7 @@ RSpec.describe EdgeCache::BustUser, type: :service do
described_class.call(user)
paths.each do |path|
expect(described_class).to have_received(:bust).with(path).once
expect(cache_bust).to have_received(:call).with(path).once
end
end
end

View file

@ -1,22 +1,24 @@
require "rails_helper"
RSpec.describe Users::Delete, type: :service do
let(:cache_bust) { instance_double(EdgeCache::Bust) }
let(:user) { create(:user, :trusted, :with_identity, identities: ["github"]) }
before do
omniauth_mock_github_payload
allow(SiteConfig).to receive(:authentication_providers).and_return(Authentication::Providers.available)
allow(EdgeCache::Bust).to receive(:new).and_return(cache_bust)
allow(cache_bust).to receive(:call)
end
let(:user) { create(:user, :trusted, :with_identity, identities: ["github"]) }
it "deletes user" do
described_class.call(user)
expect(User.find_by(id: user.id)).to be_nil
end
it "busts user profile page" do
allow(EdgeCache::Bust).to receive(:call).with("/#{user.username}")
described_class.new(user).call
expect(EdgeCache::Bust).to have_received(:call).with("/#{user.username}")
expect(cache_bust).to have_received(:call).with("/#{user.username}")
end
it "deletes user's follows" do

View file

@ -7,14 +7,19 @@ RSpec.describe Articles::BustMultipleCachesWorker, type: :worker do
let!(:article) { create(:article) }
let(:path) { article.path }
let(:worker) { subject }
let(:cache_bust) { instance_double(EdgeCache::Bust) }
before do
allow(EdgeCache::Bust).to receive(:new).and_return(cache_bust)
allow(cache_bust).to receive(:call).with(path)
allow(cache_bust).to receive(:call).with("#{path}?i=i")
end
it "busts cache" do
allow(EdgeCache::Bust).to receive(:call)
worker.perform([article.id])
expect(EdgeCache::Bust).to have_received(:call).with(path).once
expect(EdgeCache::Bust).to have_received(:call).with("#{path}?i=i").once
expect(cache_bust).to have_received(:call).with(path).once
expect(cache_bust).to have_received(:call).with("#{path}?i=i").once
end
end
end

View file

@ -2,16 +2,21 @@ require "rails_helper"
RSpec.describe BustCachePathWorker, type: :worker do
let(:worker) { subject }
let(:cache_bust) { instance_double(EdgeCache::Bust) }
include_examples "#enqueues_on_correct_queue", "high_priority"
describe "#perform" do
let(:path) { "/foo" }
before do
allow(EdgeCache::Bust).to receive(:new).and_return(cache_bust)
allow(cache_bust).to receive(:call).with(path)
end
it "busts cache for given path" do
allow(EdgeCache::Bust).to receive(:call).with(path)
worker.perform(path)
expect(EdgeCache::Bust).to have_received(:call).with(path)
expect(cache_bust).to have_received(:call).with(path)
end
end
end

View file

@ -5,24 +5,32 @@ RSpec.describe Reactions::BustHomepageCacheWorker, type: :worker do
let(:user) { create(:user) }
let(:article) { create(:article, featured: true) }
let(:worker) { subject }
let(:cache_bust) { instance_double(EdgeCache::Bust) }
before do
allow(EdgeCache::Bust).to receive(:new).and_return(cache_bust)
allow(cache_bust).to receive(:call)
end
it "busts the homepage cache when reactable is an Article" do
reaction = create(:reaction, reactable: article, user: user)
allow(EdgeCache::Bust).to receive(:call)
worker.perform(reaction.id)
expect(EdgeCache::Bust).to have_received(:call).exactly(4)
expect(cache_bust).to have_received(:call).with("/").exactly(2).times
expect(cache_bust).to have_received(:call).with("/?i=i")
expect(cache_bust).to have_received(:call).with("?i=i")
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(EdgeCache::Bust).to receive(:call)
worker.perform(comment_reaction.id)
expect(EdgeCache::Bust).not_to have_received(:call)
expect(cache_bust).not_to have_received(:call).with("/")
expect(cache_bust).not_to have_received(:call).with("/?i=i")
expect(cache_bust).not_to have_received(:call).with("?i=i")
end
it "doesn't fail if a reaction doesn't exist" do

View file

@ -8,22 +8,24 @@ RSpec.describe Reactions::BustReactableCacheWorker, type: :worker do
let(:comment) { create(:comment, commentable: article) }
let(:comment_reaction) { create(:reaction, reactable: comment, user: user) }
let(:worker) { subject }
let(:cache_bust) { instance_double(EdgeCache::Bust) }
before do
allow(EdgeCache::Bust).to receive(:call)
allow(EdgeCache::Bust).to receive(:new).and_return(cache_bust)
allow(cache_bust).to receive(:call)
end
it "busts the reactable article cache" do
worker.perform(reaction.id)
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
expect(cache_bust).to have_received(:call).with(user.path).once
expect(cache_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(EdgeCache::Bust).to have_received(:call).with(user.path).once
expect(cache_bust).to have_received(:call).with(user.path).once
param = "/reactions?commentable_id=#{article.id}&commentable_type=Article"
expect(EdgeCache::Bust).to have_received(:call).with(param).once
expect(cache_bust).to have_received(:call).with(param).once
end
it "doesn't fail if a reaction doesn't exist" do