Bust cache on article destroy instead of resave articles #1621 (#1801)

* Bust cache on article destroy instead of resave articles

* Change articles delayed job queue
This commit is contained in:
Anna Buianova 2019-02-24 06:41:06 +03:00 committed by Ben Halpern
parent 58b2013377
commit e45c61a308
5 changed files with 24 additions and 21 deletions

View file

@ -1,12 +1,11 @@
module Articles
class ResaveJob < ApplicationJob
queue_as :articles_resave
class BustCacheJob < ApplicationJob
queue_as :articles_bust_cache
def perform(article_ids, cache_buster = CacheBuster.new)
Article.where(id: article_ids).find_each do |article|
Article.select(:id, :path).where(id: article_ids).find_each do |article|
cache_buster.bust(article.path)
cache_buster.bust(article.path + "?i=i")
article.save
end
end
end

View file

@ -426,8 +426,15 @@ class Article < ApplicationRecord
def before_destroy_actions
bust_cache
remove_algolia_index
user.cache_bust_all_articles
Articles::ResaveJob.perform_later(organization.article_ids - [id]) if organization
article_ids = user.article_ids.dup
if organization
organization.touch(:last_article_at)
article_ids.concat organization.article_ids
end
# perform busting cache in chunks in case there're a lot of articles
(article_ids.uniq.sort - [id]).each_slice(10) do |ids|
Articles::BustCacheJob.perform_later(ids)
end
end
def evaluate_front_matter(front_matter)

View file

@ -371,14 +371,6 @@ class User < ApplicationRecord
end
end
def cache_bust_all_articles
cache_buster = CacheBuster.new
articles.find_each do |article|
cache_buster.bust(article.path)
cache_buster.bust(article.path + "?i=i")
end
end
def settings_tab_list
tab_list = %w(
Profile

View file

@ -1,12 +1,12 @@
require "rails_helper"
RSpec.describe Articles::ResaveJob, type: :job do
RSpec.describe Articles::BustCacheJob, type: :job do
describe "#perform_later" do
it "enqueues the job" do
ActiveJob::Base.queue_adapter = :test
expect do
described_class.perform_later([1, 2])
end.to have_enqueued_job.with([1, 2]).on_queue("articles_resave")
end.to have_enqueued_job.with([1, 2]).on_queue("articles_bust_cache")
end
it "busts cache" do

View file

@ -14,13 +14,18 @@ RSpec.describe Article, type: :model do
end
context "with organization" do
let(:user) { create(:user) }
let(:organization) { create(:organization) }
let!(:article) { create(:article, organization: organization) }
let!(:article) { create(:article, organization: organization, user: user) }
let!(:org_article) { create(:article, organization: organization) }
let!(:user_article) { create(:article, user: user) }
let!(:org_user_article) { create(:article, user: user, organization: organization) }
before { create(:article, organization: organization) }
it "creates Articles::ResaveJob for organization articles on destroy" do
expect { article.destroy }.to have_enqueued_job(Articles::ResaveJob).exactly(:once)
it "queues BustCacheJob with user and organization article_ids" do
expect do
article.destroy
end.to have_enqueued_job(Articles::BustCacheJob).exactly(:once).
with([user_article.id, org_user_article.id, org_article.id].sort)
end
end
end