2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-02-25 14:19:12 +08:00
|
|
|
class RandomTopicSelector
|
|
|
|
|
|
|
|
BACKFILL_SIZE = 3000
|
|
|
|
BACKFILL_LOW_WATER_MARK = 500
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def self.backfill(category = nil)
|
2017-09-08 09:54:13 +08:00
|
|
|
exclude = category&.topic_id
|
2015-02-25 14:19:12 +08:00
|
|
|
|
|
|
|
options = {
|
2017-03-02 01:03:12 +08:00
|
|
|
per_page: category ? category.num_featured_topics : 3,
|
2015-02-25 14:19:12 +08:00
|
|
|
visible: true,
|
|
|
|
no_definitions: true
|
|
|
|
}
|
|
|
|
|
|
|
|
options[:except_topic_ids] = [category.topic_id] if exclude
|
|
|
|
|
2018-01-15 13:13:29 +08:00
|
|
|
if category
|
|
|
|
options[:category] = category.id
|
|
|
|
# NOTE: at the moment this site setting scopes tightly to a category (excluding subcats)
|
|
|
|
# this is done so we don't populate a junk cache
|
|
|
|
if SiteSetting.limit_suggested_to_category
|
|
|
|
options[:no_subcategories] = true
|
|
|
|
end
|
|
|
|
|
|
|
|
# don't leak private categories into the "everything" group
|
|
|
|
options[:guardian] = Guardian.new(CategoryFeaturedTopic.fake_admin)
|
|
|
|
end
|
|
|
|
|
|
|
|
query = TopicQuery.new(nil, options)
|
2016-07-04 08:34:54 +08:00
|
|
|
|
2015-02-25 14:19:12 +08:00
|
|
|
results = query.latest_results.order('RANDOM()')
|
2017-07-28 09:20:09 +08:00
|
|
|
.where(closed: false, archived: false)
|
|
|
|
.where("topics.created_at > ?", SiteSetting.suggested_topics_max_days_old.days.ago)
|
|
|
|
.limit(BACKFILL_SIZE)
|
|
|
|
.reorder('RANDOM()')
|
|
|
|
.pluck(:id)
|
2015-02-25 14:19:12 +08:00
|
|
|
|
|
|
|
key = cache_key(category)
|
2017-09-08 09:54:13 +08:00
|
|
|
|
|
|
|
if results.present?
|
2019-12-03 17:05:53 +08:00
|
|
|
Discourse.redis.multi do
|
|
|
|
Discourse.redis.rpush(key, results)
|
|
|
|
Discourse.redis.expire(key, 2.days)
|
2017-09-08 09:54:13 +08:00
|
|
|
end
|
2015-02-25 14:19:12 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
results
|
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def self.next(count, category = nil)
|
2015-02-25 14:19:12 +08:00
|
|
|
key = cache_key(category)
|
|
|
|
|
|
|
|
results = []
|
|
|
|
|
2015-10-02 13:00:51 +08:00
|
|
|
return results if count < 1
|
2015-02-25 14:19:12 +08:00
|
|
|
|
2019-12-03 17:05:53 +08:00
|
|
|
results = Discourse.redis.multi do
|
|
|
|
Discourse.redis.lrange(key, 0, count - 1)
|
|
|
|
Discourse.redis.ltrim(key, count, -1)
|
2015-02-25 14:19:12 +08:00
|
|
|
end
|
|
|
|
|
2016-03-03 08:26:45 +08:00
|
|
|
if !results.is_a?(Array) # Redis is in readonly mode
|
2019-12-03 17:05:53 +08:00
|
|
|
results = Discourse.redis.lrange(key, 0, count - 1)
|
2016-03-03 08:26:45 +08:00
|
|
|
else
|
|
|
|
results = results[0]
|
|
|
|
end
|
|
|
|
|
2015-10-02 13:00:51 +08:00
|
|
|
results.map!(&:to_i)
|
|
|
|
|
|
|
|
left = count - results.length
|
|
|
|
|
2015-02-25 14:19:12 +08:00
|
|
|
backfilled = false
|
|
|
|
if left > 0
|
|
|
|
ids = backfill(category)
|
|
|
|
backfilled = true
|
|
|
|
results += ids[0...count]
|
|
|
|
results.uniq!
|
|
|
|
results = results[0...count]
|
|
|
|
end
|
|
|
|
|
2019-12-03 17:05:53 +08:00
|
|
|
if !backfilled && Discourse.redis.llen(key) < BACKFILL_LOW_WATER_MARK
|
2015-02-25 14:19:12 +08:00
|
|
|
Scheduler::Defer.later("backfill") do
|
|
|
|
backfill(category)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
results
|
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def self.cache_key(category = nil)
|
2017-09-08 09:54:13 +08:00
|
|
|
"random_topic_cache_#{category&.id}"
|
2015-02-25 14:19:12 +08:00
|
|
|
end
|
|
|
|
|
2018-05-23 06:39:15 +08:00
|
|
|
def self.clear_cache!
|
2019-12-03 17:05:53 +08:00
|
|
|
Discourse.redis.delete_prefixed(cache_key)
|
2018-05-23 06:39:15 +08:00
|
|
|
end
|
|
|
|
|
2015-02-25 14:19:12 +08:00
|
|
|
end
|