Prepare for storing caches.

This commit is contained in:
Martin Rotter 2017-09-09 13:03:31 +02:00
parent 0d94254e6c
commit 2d8fbab362
3 changed files with 45 additions and 4 deletions

View file

@ -21,7 +21,7 @@
--close-templates --close-templates
--max-code-length=140 --max-code-length=140
--lineend=linux --lineend=linux
--delete-empty-lines #--delete-empty-lines
--mode=c --mode=c
-t -p -t -p
-M60Ucv -M60Ucv

View file

@ -33,35 +33,68 @@ CacheForServiceRoot::~CacheForServiceRoot() {
void CacheForServiceRoot::addMessageStatesToCache(const QList<Message>& ids_of_messages, RootItem::Importance importance) { void CacheForServiceRoot::addMessageStatesToCache(const QList<Message>& ids_of_messages, RootItem::Importance importance) {
m_cacheSaveMutex->lock(); m_cacheSaveMutex->lock();
QList<Message>& list_act = m_cachedStatesImportant[importance]; QList<Message>& list_act = m_cachedStatesImportant[importance];
QList<Message>& list_other = m_cachedStatesImportant[importance == RootItem::Important ? RootItem::NotImportant : RootItem::Important]; QList<Message>& list_other = m_cachedStatesImportant[importance == RootItem::Important ? RootItem::NotImportant : RootItem::Important];
// Store changes, they will be sent to server later. // Store changes, they will be sent to server later.
list_act.append(ids_of_messages); list_act.append(ids_of_messages);
QSet<Message> set_act = list_act.toSet(); QSet<Message> set_act = list_act.toSet();
QSet<Message> set_other = list_other.toSet(); QSet<Message> set_other = list_other.toSet();
// Now, we want to remove all IDS from list_other, which are contained in list. // Now, we want to remove all IDS from list_other, which are contained in list.
set_other -= set_act; set_other -= set_act;
list_act.clear(); list_act.clear();
list_act.append(set_act.toList()); list_act.append(set_act.toList());
list_other.clear(); list_other.clear();
list_other.append(set_other.toList()); list_other.append(set_other.toList());
m_cacheSaveMutex->unlock(); m_cacheSaveMutex->unlock();
} }
void CacheForServiceRoot::addMessageStatesToCache(const QStringList& ids_of_messages, RootItem::ReadStatus read) { void CacheForServiceRoot::addMessageStatesToCache(const QStringList& ids_of_messages, RootItem::ReadStatus read) {
m_cacheSaveMutex->lock(); m_cacheSaveMutex->lock();
QStringList& list_act = m_cachedStatesRead[read]; QStringList& list_act = m_cachedStatesRead[read];
QStringList& list_other = m_cachedStatesRead[read == RootItem::Read ? RootItem::Unread : RootItem::Read]; QStringList& list_other = m_cachedStatesRead[read == RootItem::Read ? RootItem::Unread : RootItem::Read];
// Store changes, they will be sent to server later. // Store changes, they will be sent to server later.
list_act.append(ids_of_messages); list_act.append(ids_of_messages);
QSet<QString> set_act = list_act.toSet(); QSet<QString> set_act = list_act.toSet();
QSet<QString> set_other = list_other.toSet(); QSet<QString> set_other = list_other.toSet();
// Now, we want to remove all IDS from list_other, which are contained in list. // Now, we want to remove all IDS from list_other, which are contained in list.
set_other -= set_act; set_other -= set_act;
list_act.clear(); list_act.clear();
list_act.append(set_act.toList()); list_act.append(set_act.toList());
list_other.clear(); list_other.clear();
list_other.append(set_other.toList()); list_other.append(set_other.toList());
m_cacheSaveMutex->unlock();
}
void CacheForServiceRoot::saveCacheToFile(int accId) {
m_cacheSaveMutex->lock();
// Save to file.
clearCache();
m_cacheSaveMutex->unlock();
}
void CacheForServiceRoot::clearCache() {
m_cachedStatesRead.clear();
m_cachedStatesImportant.clear();
}
void CacheForServiceRoot::loadCacheFromFile(int accId) {
m_cacheSaveMutex->lock();
clearCache();
// Load from file.
m_cacheSaveMutex->unlock(); m_cacheSaveMutex->unlock();
} }
@ -71,16 +104,19 @@ QPair<QMap<RootItem::ReadStatus, QStringList>, QMap<RootItem::Importance, QList<
if (m_cachedStatesRead.isEmpty() && m_cachedStatesImportant.isEmpty()) { if (m_cachedStatesRead.isEmpty() && m_cachedStatesImportant.isEmpty()) {
// No cached changes. // No cached changes.
m_cacheSaveMutex->unlock(); m_cacheSaveMutex->unlock();
return QPair<QMap<RootItem::ReadStatus, QStringList>, QMap<RootItem::Importance, QList<Message>>>(); return QPair<QMap<RootItem::ReadStatus, QStringList>, QMap<RootItem::Importance, QList<Message>>>();
} }
// Make copy of changes. // Make copy of changes.
QMap<RootItem::ReadStatus, QStringList> cached_data_read = m_cachedStatesRead; QMap<RootItem::ReadStatus, QStringList> cached_data_read = m_cachedStatesRead;
cached_data_read.detach(); cached_data_read.detach();
QMap<RootItem::Importance, QList<Message>> cached_data_imp = m_cachedStatesImportant; QMap<RootItem::Importance, QList<Message>> cached_data_imp = m_cachedStatesImportant;
cached_data_imp.detach(); cached_data_imp.detach();
m_cachedStatesRead.clear();
m_cachedStatesImportant.clear(); clearCache();
m_cacheSaveMutex->unlock(); m_cacheSaveMutex->unlock();
return QPair<QMap<RootItem::ReadStatus, QStringList>, QMap<RootItem::Importance, QList<Message>>>(cached_data_read, cached_data_imp); return QPair<QMap<RootItem::ReadStatus, QStringList>, QMap<RootItem::Importance, QList<Message>>>(cached_data_read, cached_data_imp);
} }

View file

@ -35,6 +35,11 @@ class CacheForServiceRoot {
void addMessageStatesToCache(const QList<Message>& ids_of_messages, RootItem::Importance importance); void addMessageStatesToCache(const QList<Message>& ids_of_messages, RootItem::Importance importance);
void addMessageStatesToCache(const QStringList& ids_of_messages, RootItem::ReadStatus read); void addMessageStatesToCache(const QStringList& ids_of_messages, RootItem::ReadStatus read);
// Persistently saves/loads cached changes to/from file.
// NOTE: The whole cache is cleared after save is done and before load is done.
void saveCacheToFile(int accId);
void loadCacheFromFile(int accId);
protected: protected:
QPair<QMap<RootItem::ReadStatus, QStringList>, QMap<RootItem::Importance, QList<Message>>> takeMessageCache(); QPair<QMap<RootItem::ReadStatus, QStringList>, QMap<RootItem::Importance, QList<Message>>> takeMessageCache();