Commit 266bb2ef authored by Ronan's avatar Ronan

feat(MainDb): add a LruCache to increase fetch performance of unread message count

parent d5006fc6
......@@ -25,6 +25,7 @@
#include "linphone/utils/utils.h"
#include "abstract/abstract-db-p.h"
#include "containers/lru-cache.h"
#include "event-log/event-log.h"
#include "main-db.h"
......@@ -182,6 +183,10 @@ private:
void importLegacyFriends (DbSession &inDbSession);
void importLegacyHistory (DbSession &inDbSession);
// ---------------------------------------------------------------------------
mutable LruCache<ChatRoomId, int> unreadChatMessageCountCache;
L_DECLARE_PUBLIC(MainDb);
};
......
......@@ -1903,6 +1903,12 @@ int MainDb::getChatMessageCount (const ChatRoomId &chatRoomId) const {
}
int MainDb::getUnreadChatMessageCount (const ChatRoomId &chatRoomId) const {
L_D();
const int *count = d->unreadChatMessageCountCache[chatRoomId];
if (count)
return *count;
string query = "SELECT COUNT(*) FROM conference_chat_message_event WHERE";
if (chatRoomId.isValid())
query += " event_id IN ("
......@@ -1918,8 +1924,6 @@ int MainDb::getUnreadChatMessageCount (const ChatRoomId &chatRoomId) const {
);
return L_DB_TRANSACTION {
L_D();
int count = 0;
soci::session *session = d->dbSession.getBackendSession();
......@@ -1931,6 +1935,7 @@ int MainDb::getUnreadChatMessageCount (const ChatRoomId &chatRoomId) const {
*session << query, soci::use(dbChatRoomId), soci::into(count);
}
d->unreadChatMessageCountCache.insert(chatRoomId, count);
return count;
};
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment