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:
Robin Ward 2014-08-01 16:24:48 -04:00
parent 212d580745
commit 0a2036a99a
4 changed files with 41 additions and 10 deletions

View File

@ -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

View File

@ -0,0 +1,9 @@
module Jobs
class TopRefreshOlder < Jobs::Scheduled
every 1.day
def execute(args)
TopTopic.refresh_older!
end
end
end

View File

@ -0,0 +1,9 @@
module Jobs
class TopRefreshToday < Jobs::Scheduled
every 1.hour
def execute(args)
TopTopic.refresh_daily!
end
end
end

View File

@ -10,20 +10,36 @@ 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", 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
# 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", :daily)
end
TopTopic.compute_top_score_for(:daily)
end
end
end
def self.refresh!
TopTopic.refresh_daily!
TopTopic.refresh_older!
end
def self.remove_invisible_topics