diff options
author | Joas Schilling <coding@schilljs.com> | 2021-07-02 16:38:38 +0300 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2021-07-02 16:54:47 +0300 |
commit | ca8a3516b36beec9c3d5acf5fb577d28236ec366 (patch) | |
tree | d7108a98ab5082c7c59cabd206a0310f14a60a93 /lib/Chat/ChatManager.php | |
parent | 127b54fe57e9c6ea2ba4a19a90bc9531aad3ae7e (diff) |
Use memcache to save the query for unread message count
For a given message id $lastReadMessage we cache the number of messages
that exist past that message, which happen to also be the number of
unread messages, because this is expensive to query per room and user repeatedly
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'lib/Chat/ChatManager.php')
-rw-r--r-- | lib/Chat/ChatManager.php | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/lib/Chat/ChatManager.php b/lib/Chat/ChatManager.php index ff1dc862f..63a0b3ab6 100644 --- a/lib/Chat/ChatManager.php +++ b/lib/Chat/ChatManager.php @@ -77,6 +77,8 @@ class ChatManager { protected $timeFactory; /** @var ICache */ protected $cache; + /** @var ICache */ + protected $unreadCountCache; public function __construct(CommentsManager $commentsManager, IEventDispatcher $dispatcher, @@ -93,6 +95,7 @@ class ChatManager { $this->participantService = $participantService; $this->notifier = $notifier; $this->cache = $cacheFactory->createDistributed('talk/lastmsgid'); + $this->unreadCountCache = $cacheFactory->createDistributed('talk/unreadcount'); $this->timeFactory = $timeFactory; } @@ -140,6 +143,7 @@ class ChatManager { // Update last_message $chat->setLastMessage($comment); + $this->unreadCountCache->clear($chat->getId() . '-'); if ($sendNotifications) { $this->notifier->notifyOtherParticipant($chat, $comment, []); @@ -174,6 +178,7 @@ class ChatManager { // Update last_message $chat->setLastMessage($comment); + $this->unreadCountCache->clear($chat->getId() . '-'); $this->dispatcher->dispatch(self::EVENT_AFTER_SYSTEM_MESSAGE_SEND, $event); } catch (NotFoundException $e) { @@ -223,6 +228,7 @@ class ChatManager { // Update last_message if ($comment->getActorType() !== 'bots' || $comment->getActorId() === 'changelog') { $chat->setLastMessage($comment); + $this->unreadCountCache->clear($chat->getId() . '-'); } $alreadyNotifiedUsers = []; @@ -315,7 +321,18 @@ class ChatManager { } public function getUnreadCount(Room $chat, int $lastReadMessage): int { - return $this->commentsManager->getNumberOfCommentsForObjectSinceComment('chat', (string) $chat->getId(), $lastReadMessage, 'comment'); + /** + * for a given message id $lastReadMessage we cache the number of messages + * that exist past that message, which happen to also be the number of + * unread messages, because this is expensive to query per room and user repeatedly + */ + $key = $chat->getId() . '-' . $lastReadMessage; + $unreadCount = $this->unreadCountCache->get($key); + if ($unreadCount === null) { + $unreadCount = $this->commentsManager->getNumberOfCommentsForObjectSinceComment('chat', (string) $chat->getId(), $lastReadMessage, 'comment'); + $this->unreadCountCache->set($key, $unreadCount, 1800); + } + return $unreadCount; } /** |