Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: Dynamic size for hot topic IDs cache (#32175)

Roman Rizzia57925b6dbd9
diff --git a/app/models/topic_hot_score.rb b/app/models/topic_hot_score.rb
index 1783e0d0aa5..e3ed71df229 100644
--- a/app/models/topic_hot_score.rb
+++ b/app/models/topic_hot_score.rb
@@ -4,19 +4,29 @@ class TopicHotScore < ActiveRecord::Base
belongs_to :topic
DEFAULT_BATCH_SIZE = 1000
-
- def self.hot_topic_ids_cache
- @hot_topic_ids_cache ||= DistributedCache.new("hot_topic_ids_cache")
- end
+ CACHE_KEY = "hot_topics_ids"
def self.hottest_topic_ids
- hot_topic_ids_cache["ids"].presence || Set.new
+ Discourse.cache.read(CACHE_KEY).presence || Set.new
end
def self.recreate_hottest_topic_ids_cache
- hot_topic_ids_cache.defer_set(
- "ids",
- DB.query_single("SELECT topic_id FROM topic_hot_scores ORDER BY score DESC LIMIT 100").to_set,
+ hot_topics_limit =
+ (
+ Topic
+ .where(archetype: Archetype.default)
+ .where("last_posted_at > ?", SiteSetting.hot_topics_recent_days.days.ago)
+ .count * 0.1
+ ).to_i # 10% of the topics with activity since the hot topics cutoff.
+
+ hot_topics_limit = [hot_topics_limit, 100].min # Capped at 100 IDs.
+
+ Discourse.cache.write(
+ CACHE_KEY,
+ DB.query_single(
+ "SELECT topic_id FROM topic_hot_scores ORDER BY score DESC LIMIT :limit",
+ limit: hot_topics_limit,
+ ).to_set,
)
end
diff --git a/spec/models/topic_hot_scores_spec.rb b/spec/models/topic_hot_scores_spec.rb
index 3acf9db5d6e..03b2a367cea 100644
--- a/spec/models/topic_hot_scores_spec.rb
+++ b/spec/models/topic_hot_scores_spec.rb
@@ -6,7 +6,7 @@ RSpec.describe TopicHotScore do
fab!(:user2) { Fabricate(:user) }
fab!(:user3) { Fabricate(:user) }
- after { TopicHotScore.hot_topic_ids_cache.clear }
+ after { Discourse.cache.delete(described_class::CACHE_KEY) }
it "also always updates based on recent activity" do
freeze_time
@@ -114,19 +114,19 @@ RSpec.describe TopicHotScore do
expect(TopicHotScore.find_by(topic_id: topic2.id).score).to be_within(0.001).of(0.001)
end
- it "caches top 100 topic IDs" do
- topic1 = Fabricate(:topic, like_count: 3)
- topic2 = Fabricate(:topic, like_count: 10)
+ it "caches 10% of the hottest topic IDs" do
+ 9.times { Fabricate(:topic, like_count: 3, last_posted_at: 10.minutes.ago) }
+ hottest_topic = Fabricate(:topic, like_count: 10, last_posted_at: 10.minutes.ago)
TopicHotScore.update_scores
- expect(TopicHotScore.hottest_topic_ids).to contain_exactly(topic2.id, topic1.id)
+ expect(TopicHotScore.hottest_topic_ids).to contain_exactly(hottest_topic.id)
- topic3 = Fabricate(:topic, like_count: 100)
+ hottest_topic_2 = Fabricate(:topic, like_count: 100, last_posted_at: 10.minutes.ago)
TopicHotScore.update_scores
- expect(TopicHotScore.hottest_topic_ids).to contain_exactly(topic3.id, topic2.id, topic1.id)
+ expect(TopicHotScore.hottest_topic_ids).to contain_exactly(hottest_topic_2.id)
end
it "ignores topics in the future" do
diff --git a/spec/serializers/topic_list_item_serializer_spec.rb b/spec/serializers/topic_list_item_serializer_spec.rb
index ad3697c8fdf..6fd509e2945 100644
--- a/spec/serializers/topic_list_item_serializer_spec.rb
+++ b/spec/serializers/topic_list_item_serializer_spec.rb
@@ -210,10 +210,11 @@ RSpec.describe TopicListItemSerializer do
describe "#is_hot" do
describe "including the attr based on theme modifier or plugin registry" do
- fab!(:hot_topic) { Fabricate(:topic, like_count: 10) }
+ fab!(:hot_topic) { Fabricate(:topic) }
- before { TopicHotScore.update_scores }
- after { TopicHotScore.hot_topic_ids_cache.clear }
+ # Caching this directly to workaround the limit heuristic.
+ before { Discourse.cache.write(TopicHotScore::CACHE_KEY, Set.new([hot_topic.id])) }
+ after { Discourse.cache.delete(TopicHotScore::CACHE_KEY) }
context "without opt-in" do
before do