aboutsummaryrefslogtreecommitdiff
path: root/connection.cpp
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2017-11-16 13:01:25 +0900
committerKitsune Ral <Kitsune-Ral@users.sf.net>2017-11-16 13:01:25 +0900
commite8ced4bb607084a3771f853bd7dcb6a4396aef59 (patch)
tree8cb93971ee225f7b96451177cf7c0f6163f30d49 /connection.cpp
parent92ede101bbde3b853a3eed20f6c097cd7a137b9c (diff)
downloadlibquotient-e8ced4bb607084a3771f853bd7dcb6a4396aef59.tar.gz
libquotient-e8ced4bb607084a3771f853bd7dcb6a4396aef59.zip
Automate cache resets
The idea is simple: store a version in the cache; if, upon the next load, the (major) version is too old, the cache is discarded. The currently used version values (0.0) do not discard the cache; but the next commit will bump the (major) version.
Diffstat (limited to 'connection.cpp')
-rw-r--r--connection.cpp24
1 files changed, 23 insertions, 1 deletions
diff --git a/connection.cpp b/connection.cpp
index c940c767..d15067dd 100644
--- a/connection.cpp
+++ b/connection.cpp
@@ -402,6 +402,9 @@ QByteArray Connection::generateTxnId()
return d->data->generateTxnId();
}
+static constexpr int CACHE_VERSION_MAJOR = 0;
+static constexpr int CACHE_VERSION_MINOR = 0;
+
void Connection::saveState(const QUrl &toFile) const
{
if (!d->cacheState)
@@ -447,6 +450,11 @@ void Connection::saveState(const QUrl &toFile) const
rootObj.insert("next_batch", d->data->lastEvent());
rootObj.insert("rooms", roomObj);
+ QJsonObject versionObj;
+ versionObj.insert("major", CACHE_VERSION_MAJOR);
+ versionObj.insert("minor", CACHE_VERSION_MINOR);
+ rootObj.insert("cache_version", versionObj);
+
QByteArray data = QJsonDocument(rootObj).toJson(QJsonDocument::Compact);
qCDebug(MAIN) << "Writing state to file" << outfile.fileName();
@@ -472,8 +480,22 @@ void Connection::loadState(const QUrl &fromFile)
file.open(QFile::ReadOnly);
QByteArray data = file.readAll();
+ auto jsonDoc = QJsonDocument::fromJson(data);
+ auto actualCacheVersionMajor =
+ jsonDoc.object()
+ .value("cache_version").toObject()
+ .value("major").toInt();
+ if (actualCacheVersionMajor < CACHE_VERSION_MAJOR)
+ {
+ qCWarning(MAIN) << "Major version of the cache file is"
+ << actualCacheVersionMajor << "but"
+ << CACHE_VERSION_MAJOR
+ << "required; discarding the cache";
+ return;
+ }
+
SyncData sync;
- sync.parseJson(QJsonDocument::fromJson(data));
+ sync.parseJson(jsonDoc);
onSyncSuccess(std::move(sync));
qCDebug(PROFILER) << "*** Cached state for" << userId()
<< "loaded in" << et.elapsed() << "ms";