Skip to content

Commit

Permalink
Remove redundant cache purge request (forem#18832)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mac Siri authored Dec 20, 2022
1 parent 6cb97ee commit e9c3f82
Show file tree
Hide file tree
Showing 12 changed files with 13 additions and 46 deletions.
9 changes: 5 additions & 4 deletions app/services/edge_cache/bust/fastly.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ def self.fastly_purge(path)
private_class_method :fastly_purge

def self.urls(path)
[
URL.url(path),
URL.url("#{path}?i=i"),
]
[URL.url(path)] << if path.include?("?")
URL.url("#{path}&i=i")
else
URL.url("#{path}?i=i")
end
end
private_class_method :urls
end
Expand Down
15 changes: 2 additions & 13 deletions app/services/edge_cache/bust_article.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,10 @@ def self.call(article)
def self.paths_for(article, &block)
paths = [
article.path,
"/#{article.user.username}",
"#{article.path}/",
"#{article.path}?i=i",
"#{article.path}/?i=i",
"/#{article.user.username}",
"#{article.path}/comments",
"#{article.path}?preview=#{article.password}",
"#{article.path}?preview=#{article.password}&i=i",
"/api/articles/#{article.id}",
]

Expand All @@ -47,27 +44,23 @@ def self.paths_for(article, &block)

def self.bust_home_pages(cache_bust, article)
if article.published_at.to_i > Time.current.to_i
# TODO
cache_bust.call("/")
cache_bust.call("?i=i")
end

if article.video.present? && article.published_at.to_i > 10.days.ago.to_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)

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
cache_bust.call("/latest")
cache_bust.call("/latest?i=i")
end

cache_bust.call("/") if Article.published.order(hotness_score: :desc).limit(4).ids.include?(article.id)
Expand All @@ -81,16 +74,13 @@ def self.bust_tag_pages(cache_bust, article)
article.tag_list.each do |tag|
if article.published_at.to_i > 2.minutes.ago.to_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).cached_tagged_with_any(tag)
.order(public_reactions_count: :desc).limit(3).ids.include?(article.id)

cache_bust.call("/top/#{interval}")
cache_bust.call("/top/#{interval}?i=i")
cache_bust.call("/top/#{interval}/?i=i")
12.times do |i|
cache_bust.call("/api/articles?tag=#{tag}&top=#{i}")
end
Expand All @@ -101,7 +91,6 @@ def self.bust_tag_pages(cache_bust, article)
.order(hotness_score: :desc).limit(2).ids.include?(article.id)

cache_bust.call("/t/#{tag}")
cache_bust.call("/t/#{tag}?i=i")
end
end

Expand Down
3 changes: 0 additions & 3 deletions app/services/edge_cache/bust_comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ def self.call(commentable)

commentable.comments.includes(:user).find_each do |comment|
cache_bust.call(comment.path)
cache_bust.call("#{comment.path}?i=i")
end

cache_bust.call("#{commentable.path}/comments/*")
Expand All @@ -25,8 +24,6 @@ def self.bust_article_comment(cache_bust, article)
return unless article.decorate.discussion?

cache_bust.call("/")
cache_bust.call("/?i=i")
cache_bust.call("?i=i")
end

private_class_method :bust_article_comment
Expand Down
2 changes: 0 additions & 2 deletions app/services/edge_cache/bust_listings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ def self.call(listing)

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
Expand Down
2 changes: 0 additions & 2 deletions app/services/edge_cache/bust_page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ def self.call(slug)

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
2 changes: 0 additions & 2 deletions app/services/edge_cache/bust_tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ def self.call(tag)

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
Expand Down
4 changes: 0 additions & 4 deletions app/services/edge_cache/bust_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@ def self.call(user)
user_id = user.id
paths = [
"/#{username}",
"/#{username}?i=i",
"/#{username}/comments",
"/#{username}/comments?i=i",
"/#{username}/comments/?i=i",
"/live/#{username}",
"/live/#{username}?i=i",
"/feed/#{username}",
"/api/users/#{user_id}",
]
Expand Down
2 changes: 1 addition & 1 deletion spec/services/edge_cache/bust_article_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
it "busts the cache" do
described_class.call(article)
expect(article).to have_received(:purge).once
expect(cache_bust).to have_received(:call).exactly(9).times
expect(cache_bust).to have_received(:call).exactly(6).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
Expand Down
6 changes: 2 additions & 4 deletions spec/services/edge_cache/bust_listings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
let(:paths) do
[
"/listings",
"/listings?i=i",
"/listings/#{listing.category}/#{listing.slug}",
"/listings/#{listing.category}/#{listing.slug}?i=i",
"/listings/#{listing.category}",
"/listings/#{listing.category}/#{listing.slug}",
]
end

Expand All @@ -23,7 +21,7 @@
end
end

it "busts the cache" do
it "busts the cache", :aggregate_failures do
described_class.call(listing)

expect(listing).to have_received(:purge_all)
Expand Down
4 changes: 1 addition & 3 deletions spec/services/edge_cache/bust_page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
let(:paths) do
[
"/page/#{slug}",
"/page/#{slug}?i=i",
"/#{slug}",
"/#{slug}?i=i",
]
end

Expand All @@ -20,7 +18,7 @@
end
end

it "busts the cache" do
it "busts the cache", :aggregate_failures do
described_class.call(slug)

paths.each do |path|
Expand Down
4 changes: 1 addition & 3 deletions spec/services/edge_cache/bust_tag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
let(:paths) do
[
"/t/#{tag.name}",
"/t/#{tag.name}?i=i",
"/t/#{tag.name}/?i=i",
"/t/#{tag.name}/",
"/tags",
]
Expand All @@ -23,7 +21,7 @@
allow(tag).to receive(:purge).once
end

it "busts the cache" do
it "busts the cache", :aggregate_failures do
described_class.call(tag)

paths.each do |path|
Expand Down
6 changes: 1 addition & 5 deletions spec/services/edge_cache/bust_user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@

[
"/#{username}",
"/#{username}?i=i",
"/#{username}/comments",
"/#{username}/comments?i=i",
"/#{username}/comments/?i=i",
"/live/#{username}",
"/live/#{username}?i=i",
"/feed/#{username}",
"/api/users/#{user.id}",
]
Expand All @@ -28,7 +24,7 @@
end
end

it "busts the cache" do
it "busts the cache", :aggregate_failures do
described_class.call(user)

paths.each do |path|
Expand Down

0 comments on commit e9c3f82

Please sign in to comment.