Create an LRU cache for linkpreviewers

Create an LRU cache for linkpreviewers to stop the storage growing continuously as new links are made.
This commit is contained in:
James Graham
2024-08-27 08:15:03 +00:00
parent cc7f783b50
commit 6d77ed1e0e
2 changed files with 7 additions and 6 deletions

View File

@@ -9,7 +9,6 @@
#include "controller.h" #include "controller.h"
#include "jobs/neochatchangepasswordjob.h" #include "jobs/neochatchangepasswordjob.h"
#include "jobs/neochatdeactivateaccountjob.h" #include "jobs/neochatdeactivateaccountjob.h"
#include "linkpreviewer.h"
#include "neochatconfig.h" #include "neochatconfig.h"
#include "neochatroom.h" #include "neochatroom.h"
#include "notificationsmanager.h" #include "notificationsmanager.h"
@@ -45,6 +44,7 @@ NeoChatConnection::NeoChatConnection(QObject *parent)
: Connection(parent) : Connection(parent)
, m_threePIdModel(new ThreePIdModel(this)) , m_threePIdModel(new ThreePIdModel(this))
{ {
m_linkPreviewers.setMaxCost(20);
connectSignals(); connectSignals();
} }
@@ -52,6 +52,7 @@ NeoChatConnection::NeoChatConnection(const QUrl &server, QObject *parent)
: Connection(server, parent) : Connection(server, parent)
, m_threePIdModel(new ThreePIdModel(this)) , m_threePIdModel(new ThreePIdModel(this))
{ {
m_linkPreviewers.setMaxCost(20);
connectSignals(); connectSignals();
} }
@@ -563,13 +564,13 @@ LinkPreviewer *NeoChatConnection::previewerForLink(const QUrl &link)
return nullptr; return nullptr;
} }
auto previewer = m_linkPreviewers.value(link, nullptr); auto previewer = m_linkPreviewers.object(link);
if (previewer != nullptr) { if (previewer != nullptr) {
return previewer; return previewer;
} }
previewer = new LinkPreviewer(link, this); previewer = new LinkPreviewer(link, this);
m_linkPreviewers[link] = previewer; m_linkPreviewers.insert(link, previewer);
return previewer; return previewer;
} }

View File

@@ -3,6 +3,7 @@
#pragma once #pragma once
#include <QCache>
#include <QObject> #include <QObject>
#include <QQmlEngine> #include <QQmlEngine>
@@ -13,10 +14,9 @@
#include <Quotient/keyimport.h> #include <Quotient/keyimport.h>
#endif #endif
#include "linkpreviewer.h"
#include "models/threepidmodel.h" #include "models/threepidmodel.h"
class LinkPreviewer;
class NeoChatConnection : public Quotient::Connection class NeoChatConnection : public Quotient::Connection
{ {
Q_OBJECT Q_OBJECT
@@ -222,7 +222,7 @@ private:
int m_badgeNotificationCount = 0; int m_badgeNotificationCount = 0;
QHash<QUrl, LinkPreviewer *> m_linkPreviewers; QCache<QUrl, LinkPreviewer> m_linkPreviewers;
bool m_canCheckMutualRooms = false; bool m_canCheckMutualRooms = false;
}; };