Revert "FEATURE: Better thread reply counter cache (#21108)" (#21192)

This reverts commit 180e3e11d1.

Per internal discussions, this is a temporary revert, to investigate if this is causing a performance regression.
This commit is contained in:
Daniel Waterworth
2023-04-20 15:09:47 -05:00
committed by GitHub
parent a4d51810e2
commit 38cebd3ed5
22 changed files with 232 additions and 602 deletions
+2 -10
View File
@@ -4,8 +4,6 @@ module Chat
class Thread < ActiveRecord::Base
EXCERPT_LENGTH = 150
include Chat::ThreadCache
self.table_name = "chat_threads"
belongs_to :channel, foreign_key: "channel_id", class_name: "Chat::Channel"
@@ -13,11 +11,7 @@ module Chat
belongs_to :original_message, foreign_key: "original_message_id", class_name: "Chat::Message"
has_many :chat_messages,
-> {
where("deleted_at IS NULL").order(
"chat_messages.created_at ASC, chat_messages.id ASC",
)
},
-> { order("chat_messages.created_at ASC, chat_messages.id ASC") },
foreign_key: :thread_id,
primary_key: :id,
class_name: "Chat::Message"
@@ -67,7 +61,7 @@ module Chat
#
# It is updated eventually via Jobs::Chat::PeriodicalUpdates. In
# future we may want to update this more frequently.
updated_thread_ids = DB.query_single <<~SQL
DB.exec <<~SQL
UPDATE chat_threads threads
SET replies_count = subquery.replies_count
FROM (
@@ -78,9 +72,7 @@ module Chat
) subquery
WHERE threads.id = subquery.thread_id
AND subquery.replies_count != threads.replies_count
RETURNING threads.id AS thread_id;
SQL
self.clear_caches!(updated_thread_ids)
end
end
end
@@ -1,69 +0,0 @@
# frozen_string_literal: true
module Chat
module ThreadCache
extend ActiveSupport::Concern
class_methods do
def replies_count_cache_updated_at_redis_key(id)
"chat_thread:replies_count_cache_updated_at:#{id}"
end
def replies_count_cache_redis_key(id)
"chat_thread:replies_count_cache:#{id}"
end
def clear_caches!(ids = nil)
return Discourse.redis.delete_prefixed("chat_thread:") if ids.blank?
ids = Array.wrap(ids)
keys_to_delete =
ids
.map do |id|
[replies_count_cache_redis_key(id), replies_count_cache_updated_at_redis_key(id)]
end
.flatten
Discourse.redis.del(keys_to_delete)
end
end
def replies_count_cache_recently_updated?
replies_count_cache_updated_at.after?(5.minutes.ago)
end
def replies_count_cache_updated_at
Time.at(
Discourse.redis.get(Chat::Thread.replies_count_cache_updated_at_redis_key(self.id)).to_i,
in: Time.zone,
)
end
def replies_count_cache
redis_cache = Discourse.redis.get(Chat::Thread.replies_count_cache_redis_key(self.id))&.to_i
if redis_cache.present? && redis_cache != self.replies_count
redis_cache
else
self.replies_count
end
end
def set_replies_count_cache(value, update_db: false)
self.update!(replies_count: value) if update_db
Discourse.redis.setex(
Chat::Thread.replies_count_cache_redis_key(self.id),
5.minutes.from_now.to_i,
value,
)
Jobs.enqueue_in(5.seconds, Jobs::Chat::UpdateThreadReplyCount, thread_id: self.id)
::Chat::Publisher.publish_thread_original_message_metadata!(self)
end
def increment_replies_count_cache
self.set_replies_count_cache(self.replies_count_cache + 1)
end
def decrement_replies_count_cache
self.set_replies_count_cache(self.replies_count_cache - 1)
end
end
end