mirror of
https://github.com/discourse/discourse.git
synced 2024-11-27 11:53:44 +08:00
OPTIMIZATION: Don't run 12 queries every 15 mins for the Top section.
Instead, run the daily queries once every hour, and the reset of the queries once a day.
This commit is contained in:
parent
212d580745
commit
0a2036a99a
|
@ -18,9 +18,6 @@ module Jobs
|
|||
# Update the scores of posts
|
||||
ScoreCalculator.new.calculate(1.day.ago)
|
||||
|
||||
# Update the scores of topics
|
||||
TopTopic.refresh!
|
||||
|
||||
# Automatically close stuff that we missed
|
||||
Topic.auto_close
|
||||
|
||||
|
|
9
app/jobs/scheduled/top_refresh_older.rb
Normal file
9
app/jobs/scheduled/top_refresh_older.rb
Normal file
|
@ -0,0 +1,9 @@
|
|||
module Jobs
|
||||
class TopRefreshOlder < Jobs::Scheduled
|
||||
every 1.day
|
||||
|
||||
def execute(args)
|
||||
TopTopic.refresh_older!
|
||||
end
|
||||
end
|
||||
end
|
9
app/jobs/scheduled/top_refresh_today.rb
Normal file
9
app/jobs/scheduled/top_refresh_today.rb
Normal file
|
@ -0,0 +1,9 @@
|
|||
module Jobs
|
||||
class TopRefreshToday < Jobs::Scheduled
|
||||
every 1.hour
|
||||
|
||||
def execute(args)
|
||||
TopTopic.refresh_daily!
|
||||
end
|
||||
end
|
||||
end
|
|
@ -10,22 +10,38 @@ class TopTopic < ActiveRecord::Base
|
|||
@sort_orders ||= [:posts, :views, :likes]
|
||||
end
|
||||
|
||||
def self.refresh!
|
||||
# The top topics we want to refresh often
|
||||
def self.refresh_daily!
|
||||
transaction do
|
||||
# update the topics list
|
||||
remove_invisible_topics
|
||||
add_new_visible_topics
|
||||
# update the denormalized data
|
||||
TopTopic.periods.each do |period|
|
||||
|
||||
TopTopic.sort_orders.each do |sort|
|
||||
TopTopic.send("update_#{sort}_count_for", :daily)
|
||||
end
|
||||
TopTopic.compute_top_score_for(:daily)
|
||||
end
|
||||
end
|
||||
|
||||
# We don't have to refresh these as often
|
||||
def self.refresh_older!
|
||||
older = TopTopic.periods - [:daily]
|
||||
|
||||
transaction do
|
||||
older.each do |period|
|
||||
TopTopic.sort_orders.each do |sort|
|
||||
TopTopic.send("update_#{sort}_count_for", period)
|
||||
TopTopic.send("update_#{sort}_count_for", :daily)
|
||||
end
|
||||
# compute top score
|
||||
TopTopic.compute_top_score_for(period)
|
||||
TopTopic.compute_top_score_for(:daily)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def self.refresh!
|
||||
TopTopic.refresh_daily!
|
||||
TopTopic.refresh_older!
|
||||
end
|
||||
|
||||
def self.remove_invisible_topics
|
||||
exec_sql("WITH category_definition_topic_ids AS (
|
||||
SELECT COALESCE(topic_id, 0) AS id FROM categories
|
||||
|
|
Loading…
Reference in New Issue
Block a user