From bc55a054806eb0b96f809d42068a7ced837dbea8 Mon Sep 17 00:00:00 2001 From: fourtf Date: Mon, 7 Oct 2019 22:14:00 +0200 Subject: [PATCH] clears cache files > 14 days 1 min after start --- src/RunGui.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/RunGui.cpp b/src/RunGui.cpp index f4a11b19..a53f79a9 100644 --- a/src/RunGui.cpp +++ b/src/RunGui.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include "Application.hpp" @@ -137,6 +138,28 @@ namespace { signal(SIGSEGV, handleSignal); #endif } + + // We delete cache files that haven't been modified in 14 days. This strategy may be + // improved in the future. + void clearCache(const QDir &dir) + { + qDebug() << "[Cache] cleared cache"; + + QStringList toBeRemoved; + + for (auto &&info : dir.entryInfoList(QDir::Files)) + { + if (info.lastModified().addDays(14) < QDateTime::currentDateTime()) + { + toBeRemoved << info.absoluteFilePath(); + } + } + + for (auto &&path : toBeRemoved) + { + qDebug() << path << QFile(path).remove(); + } + } } // namespace void runGui(QApplication &a, Paths &paths, Settings &settings) @@ -165,6 +188,11 @@ void runGui(QApplication &a, Paths &paths, Settings &settings) } }); + // Clear the cache 1 minute after start. + QTimer::singleShot(60 * 1000, [cachePath = paths.cacheDirectory()] { + QtConcurrent::run([cachePath]() { clearCache(cachePath); }); + }); + chatterino::NetworkManager::init(); chatterino::Updates::getInstance().checkForUpdates();