Reduce number of Redis calls.

This commit is contained in:
Guo Xiang Tan 2017-09-08 09:54:13 +08:00
parent 661ae38a03
commit b7ac33464f
1 changed files with 8 additions and 5 deletions

View File

@ -4,7 +4,7 @@ class RandomTopicSelector
BACKFILL_LOW_WATER_MARK = 500 BACKFILL_LOW_WATER_MARK = 500
def self.backfill(category = nil) def self.backfill(category = nil)
exclude = category.try(:topic_id) exclude = category&.topic_id
# don't leak private categories into the "everything" group # don't leak private categories into the "everything" group
user = category ? CategoryFeaturedTopic.fake_admin : nil user = category ? CategoryFeaturedTopic.fake_admin : nil
@ -28,10 +28,13 @@ class RandomTopicSelector
.pluck(:id) .pluck(:id)
key = cache_key(category) key = cache_key(category)
results.each do |id|
$redis.rpush(key, id) if results.present?
$redis.multi do
$redis.rpush(key, results)
$redis.expire(key, 2.days)
end
end end
$redis.expire(key, 2.days)
results results
end end
@ -77,7 +80,7 @@ class RandomTopicSelector
end end
def self.cache_key(category = nil) def self.cache_key(category = nil)
"random_topic_cache_#{category.try(:id)}" "random_topic_cache_#{category&.id}"
end end
end end