PERF: Reduce number of topics to filter while querying for unread.
This commit is contained in:
parent
c37598ddbc
commit
c06b782cab
|
@ -288,8 +288,9 @@ class TopicQuery
|
||||||
|
|
||||||
list
|
list
|
||||||
.where("tu.last_read_post_number < topics.#{col_name}")
|
.where("tu.last_read_post_number < topics.#{col_name}")
|
||||||
.where("COALESCE(tu.notification_level, :regular) >= :tracking",
|
.where("tu.notification_level >= :tracking",
|
||||||
regular: TopicUser.notification_levels[:regular], tracking: TopicUser.notification_levels[:tracking])
|
tracking: TopicUser.notification_levels[:tracking]
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def prioritize_pinned_topics(topics, options)
|
def prioritize_pinned_topics(topics, options)
|
||||||
|
@ -369,6 +370,10 @@ class TopicQuery
|
||||||
result
|
result
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def unread_results_redis_key
|
||||||
|
"last_unread_result_bumped_at:#{@user.id}"
|
||||||
|
end
|
||||||
|
|
||||||
def unread_results(options = {})
|
def unread_results(options = {})
|
||||||
result = TopicQuery.unread_filter(
|
result = TopicQuery.unread_filter(
|
||||||
default_results(options.reverse_merge(unordered: true)),
|
default_results(options.reverse_merge(unordered: true)),
|
||||||
|
@ -380,6 +385,17 @@ class TopicQuery
|
||||||
result = filter_callback.call(:unread, result, @user, options)
|
result = filter_callback.call(:unread, result, @user, options)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if !(last_bumped_at = $redis.get(unread_results_redis_key))
|
||||||
|
last_bumped_at = result.unscope(:limit, :order).order(:bumped_at).first&.bumped_at
|
||||||
|
|
||||||
|
$redis.setex(
|
||||||
|
unread_results_redis_key,
|
||||||
|
1.hour.to_i,
|
||||||
|
(last_bumped_at || Time.zone.now).to_s
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
result = result.where("topics.bumped_at >= ?", last_bumped_at) if last_bumped_at
|
||||||
suggested_ordering(result, options)
|
suggested_ordering(result, options)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -443,6 +443,9 @@ describe TopicQuery do
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'unread / read topics' do
|
context 'unread / read topics' do
|
||||||
|
after do
|
||||||
|
$redis.flushall
|
||||||
|
end
|
||||||
|
|
||||||
context 'with no data' do
|
context 'with no data' do
|
||||||
it "has no unread topics" do
|
it "has no unread topics" do
|
||||||
|
@ -484,10 +487,15 @@ describe TopicQuery do
|
||||||
|
|
||||||
context 'list_unread' do
|
context 'list_unread' do
|
||||||
it 'lists topics correctly' do
|
it 'lists topics correctly' do
|
||||||
new_topic = Fabricate(:post, user: creator).topic
|
freeze_time do
|
||||||
|
new_topic = Fabricate(:post, user: creator).topic
|
||||||
|
|
||||||
expect(topic_query.list_unread.topics).to eq([])
|
expect(topic_query.list_unread.topics).to eq([])
|
||||||
expect(topic_query.list_read.topics).to match_array([fully_read, partially_read])
|
expect(topic_query.list_read.topics).to match_array([fully_read, partially_read])
|
||||||
|
|
||||||
|
expect($redis.get(topic_query.unread_results_redis_key))
|
||||||
|
.to eq(Time.zone.now.to_s)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -495,10 +503,16 @@ describe TopicQuery do
|
||||||
before do
|
before do
|
||||||
user.user_option.auto_track_topics_after_msecs = 0
|
user.user_option.auto_track_topics_after_msecs = 0
|
||||||
user.user_option.save
|
user.user_option.save
|
||||||
|
partially_read.update!(bumped_at: 2.days.ago)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'only contains the partially read topic' do
|
it 'only contains the partially read topic' do
|
||||||
expect(topic_query.list_unread.topics).to eq([partially_read])
|
freeze_time do
|
||||||
|
expect(topic_query.list_unread.topics).to eq([partially_read])
|
||||||
|
|
||||||
|
expect($redis.get(topic_query.unread_results_redis_key))
|
||||||
|
.to eq(partially_read.bumped_at.to_s)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue