Only keep the last 20 cover images

This commit is contained in:
Martchus 2019-06-14 18:08:05 +02:00
parent 66a45f7578
commit 94c77deb40
4 changed files with 25 additions and 7 deletions

View File

@ -24,7 +24,8 @@ SongDescription::SongDescription(const QString &songId)
{
}
map<QString, QByteArray> QueryResultsModel::m_coverData = map<QString, QByteArray>();
std::list<QString> QueryResultsModel::s_coverNames = std::list<QString>();
map<QString, QByteArray> QueryResultsModel::s_coverData = map<QString, QByteArray>();
QueryResultsModel::QueryResultsModel(QObject *parent)
: QAbstractTableModel(parent)
@ -369,7 +370,23 @@ void HttpResultsModel::parseCoverResults(const QString &albumId, int row, const
return;
}
if (!data.isEmpty()) {
m_coverData[albumId] = data;
// cache the fetched cover
const auto currentCachedCoverCount = s_coverData.size();
s_coverData[albumId] = data;
if (s_coverData.size() > currentCachedCoverCount) {
s_coverNames.emplace_back(albumId);
// keep only the last 20 cover images around
while (s_coverNames.size() > 20) {
s_coverData.erase(s_coverNames.front());
s_coverNames.pop_front();
}
} else if (s_coverNames.back() != albumId) {
s_coverNames.remove(albumId);
s_coverNames.emplace_back(albumId);
}
// add the cover to the results
m_results[row].cover = data;
emit coverAvailable(index(row, 0));
}

View File

@ -86,7 +86,8 @@ protected:
QStringList m_errorList;
bool m_resultsAvailable;
bool m_fetchingCover;
static std::map<QString, QByteArray> m_coverData;
static std::list<QString> s_coverNames;
static std::map<QString, QByteArray> s_coverData;
};
inline const QList<SongDescription> &QueryResultsModel::results() const

View File

@ -54,8 +54,8 @@ bool LyricsWikiaResultsModel::fetchCover(const QModelIndex &index)
}
// skip if the item belongs to an album which cover has already been fetched
const auto coverData = m_coverData.find(desc.albumId);
if (coverData != m_coverData.end()) {
const auto coverData = s_coverData.find(desc.albumId);
if (coverData != s_coverData.end()) {
desc.cover = coverData->second;
return true;
}

View File

@ -59,8 +59,8 @@ bool MusicBrainzResultsModel::fetchCover(const QModelIndex &index)
}
// skip if the item belongs to an album which cover has already been fetched
const auto coverData = m_coverData.find(desc.albumId);
if (coverData != m_coverData.end()) {
const auto coverData = s_coverData.find(desc.albumId);
if (coverData != s_coverData.end()) {
desc.cover = coverData->second;
return true;
}