Store CDataCacheSerializer as a pointer for correct lifetime management

This commit is contained in:
Mat Sutcliffe
2020-03-28 22:04:14 +00:00
parent 0ee522426a
commit e8380173c9
2 changed files with 22 additions and 14 deletions

View File

@@ -68,7 +68,7 @@ namespace BlackMisc
CDataCacheRevision *m_rev = nullptr; CDataCacheRevision *m_rev = nullptr;
}; };
CDataCache::CDataCache() : CValueCache(1) CDataCache::CDataCache() : CValueCache(1), m_serializer(new CDataCacheSerializer { this, revisionFileName() })
{ {
if (! QDir::root().mkpath(persistentStore())) if (! QDir::root().mkpath(persistentStore()))
{ {
@@ -82,18 +82,23 @@ namespace BlackMisc
changeValuesFromRemote(values, CIdentifier()); changeValuesFromRemote(values, CIdentifier());
}); });
connect(&m_watcher, &QFileSystemWatcher::fileChanged, this, &CDataCache::loadFromStoreAsync); connect(&m_watcher, &QFileSystemWatcher::fileChanged, this, &CDataCache::loadFromStoreAsync);
connect(&m_serializer, &CDataCacheSerializer::valuesLoadedFromStore, this, &CDataCache::changeValuesFromRemote, Qt::DirectConnection); connect(m_serializer, &CDataCacheSerializer::valuesLoadedFromStore, this, &CDataCache::changeValuesFromRemote, Qt::DirectConnection);
if (! QFile::exists(revisionFileName())) { QFile(revisionFileName()).open(QFile::WriteOnly); } if (! QFile::exists(revisionFileName())) { QFile(revisionFileName()).open(QFile::WriteOnly); }
m_serializer.loadFromStore({}, false, true); // load pinned values m_serializer->loadFromStore({}, false, true); // load pinned values
singleShot(0, this, [this] // only start the serializer if the main thread event loop runs singleShot(0, this, [this] // only start the serializer if the main thread event loop runs
{ {
m_serializer.start(); m_serializer->start();
m_watcher.addPath(revisionFileName()); m_watcher.addPath(revisionFileName());
loadFromStoreAsync(); loadFromStoreAsync();
}); });
} }
CDataCache::~CDataCache()
{
m_serializer->quitAndWait();
}
CDataCache *CDataCache::instance() CDataCache *CDataCache::instance()
{ {
static CDataCache cache; static CDataCache cache;
@@ -149,12 +154,12 @@ namespace BlackMisc
void CDataCache::setTimeToLive(const QString &key, int ttl) void CDataCache::setTimeToLive(const QString &key, int ttl)
{ {
singleShot(0, &m_serializer, [this, key, ttl] { m_revision.setTimeToLive(key, ttl); }); singleShot(0, m_serializer, [this, key, ttl] { m_revision.setTimeToLive(key, ttl); });
} }
void CDataCache::renewTimestamp(const QString &key, qint64 timestamp) void CDataCache::renewTimestamp(const QString &key, qint64 timestamp)
{ {
singleShot(0, &m_serializer, [this, key, timestamp] { m_revision.overrideTimestamp(key, timestamp); }); singleShot(0, m_serializer, [this, key, timestamp] { m_revision.overrideTimestamp(key, timestamp); });
} }
qint64 CDataCache::getTimestampOnDisk(const QString &key) qint64 CDataCache::getTimestampOnDisk(const QString &key)
@@ -164,12 +169,12 @@ namespace BlackMisc
void CDataCache::pinValue(const QString &key) void CDataCache::pinValue(const QString &key)
{ {
singleShot(0, &m_serializer, [this, key] { m_revision.pinValue(key); }); singleShot(0, m_serializer, [this, key] { m_revision.pinValue(key); });
} }
void CDataCache::deferValue(const QString &key) void CDataCache::deferValue(const QString &key)
{ {
singleShot(0, &m_serializer, [this, key] { m_revision.deferValue(key); }); singleShot(0, m_serializer, [this, key] { m_revision.deferValue(key); });
} }
void CDataCache::admitValue(const QString &key, bool triggerLoad) void CDataCache::admitValue(const QString &key, bool triggerLoad)
@@ -180,7 +185,7 @@ namespace BlackMisc
void CDataCache::sessionValue(const QString &key) void CDataCache::sessionValue(const QString &key)
{ {
singleShot(0, &m_serializer, [this, key] { m_revision.sessionValue(key); }); singleShot(0, m_serializer, [this, key] { m_revision.sessionValue(key); });
} }
const QString CDataCache::relativeFilePath() const QString CDataCache::relativeFilePath()
@@ -191,17 +196,17 @@ namespace BlackMisc
void CDataCache::saveToStoreAsync(const BlackMisc::CValueCachePacket &values) void CDataCache::saveToStoreAsync(const BlackMisc::CValueCachePacket &values)
{ {
singleShot(0, &m_serializer, [this, values] singleShot(0, m_serializer, [this, values]
{ {
m_serializer.saveToStore(values.toVariantMap(), getAllValuesWithTimestamps()); m_serializer->saveToStore(values.toVariantMap(), getAllValuesWithTimestamps());
}); });
} }
void CDataCache::loadFromStoreAsync() void CDataCache::loadFromStoreAsync()
{ {
singleShot(0, &m_serializer, [this] singleShot(0, m_serializer, [this]
{ {
m_serializer.loadFromStore(getAllValuesWithTimestamps()); m_serializer->loadFromStore(getAllValuesWithTimestamps());
}); });
} }

View File

@@ -250,6 +250,9 @@ namespace BlackMisc
Q_OBJECT Q_OBJECT
public: public:
//! Destructor.
virtual ~CDataCache() override;
//! Return the singleton instance. //! Return the singleton instance.
static CDataCache *instance(); static CDataCache *instance();
@@ -302,7 +305,7 @@ namespace BlackMisc
QFileSystemWatcher m_watcher; QFileSystemWatcher m_watcher;
CDataCacheSerializer m_serializer { this, revisionFileName() }; CDataCacheSerializer *m_serializer = nullptr;
CDataCacheRevision m_revision { persistentStore() + "/" }; CDataCacheRevision m_revision { persistentStore() + "/" };
friend class CDataCacheSerializer; // to access m_revision and protected members of CValueCache friend class CDataCacheSerializer; // to access m_revision and protected members of CValueCache
}; };