diff --git a/app/jobs/articles/resave_job.rb b/app/jobs/articles/bust_cache_job.rb similarity index 56% rename from app/jobs/articles/resave_job.rb rename to app/jobs/articles/bust_cache_job.rb index d049b56e0..de25b1b1b 100644 --- a/app/jobs/articles/resave_job.rb +++ b/app/jobs/articles/bust_cache_job.rb @@ -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 diff --git a/app/models/article.rb b/app/models/article.rb index f6b05f92f..5677a3ba9 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -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) diff --git a/app/models/user.rb b/app/models/user.rb index a1e349296..975ed2933 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/spec/jobs/articles/resave_job_spec.rb b/spec/jobs/articles/bust_cache_job_spec.rb similarity index 81% rename from spec/jobs/articles/resave_job_spec.rb rename to spec/jobs/articles/bust_cache_job_spec.rb index 4d522e2fb..b559b4383 100644 --- a/spec/jobs/articles/resave_job_spec.rb +++ b/spec/jobs/articles/bust_cache_job_spec.rb @@ -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 diff --git a/spec/models/article_destroy_spec.rb b/spec/models/article_destroy_spec.rb index 98278ba49..b4e81c704 100644 --- a/spec/models/article_destroy_spec.rb +++ b/spec/models/article_destroy_spec.rb @@ -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