Optimize StoriesController's tagged article count (#12581)

This commit is contained in:
Mac Siri 2021-02-04 15:59:46 -05:00 committed by GitHub
parent 82b7f6519d
commit 677203d225
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 9 deletions

View file

@ -131,12 +131,13 @@ class StoriesController < ApplicationController
end
@num_published_articles = if @tag_model.requires_approval?
Article.published.cached_tagged_by_approval_with(@tag).size
@tag_model.articles.published.where(approved: true).count
elsif SiteConfig.feed_strategy == "basic"
Article.published.cached_tagged_with(@tag)
.where("score >= ?", SiteConfig.tag_feed_minimum_score).size
tagged_count
else
cached_tagged_count
Rails.cache.fetch("article-cached-tagged-count-#{@tag}", expires_in: 2.hours) do
tagged_count
end
end
@number_of_articles = user_signed_in? ? 5 : SIGNED_OUT_RECORD_COUNT
@stories = Articles::Feeds::LargeForemExperimental
@ -484,9 +485,7 @@ class StoriesController < ApplicationController
params[:sort_direction] == "desc" ? :newest : :oldest
end
def cached_tagged_count
Rails.cache.fetch("article-cached-tagged-count-#{@tag}", expires_in: 2.hours) do
Article.published.cached_tagged_with(@tag).where("score >= ?", SiteConfig.tag_feed_minimum_score).size
end
def tagged_count
@tag_model.articles.published.where("score >= ?", SiteConfig.tag_feed_minimum_score).count
end
end

View file

@ -132,7 +132,7 @@ describe Rack::Attack, type: :request, throttle: true do
# rubocop:disable RSpec/AnyInstance, RSpec/ExampleLength
it "throttles viewing tags" do
allow_any_instance_of(StoriesController).to receive(:cached_tagged_count).and_return(0)
allow_any_instance_of(StoriesController).to receive(:tagged_count).and_return(0)
allow_any_instance_of(StoriesController).to receive(:stories_by_timeframe).and_return(Article.none)
allow_any_instance_of(Articles::Feeds::LargeForemExperimental).to receive(
:published_articles_by_tag,