]> git.sur5r.net Git - minitube/blobdiff - lib/http/src/localcache.cpp
New upstream version 3.4
[minitube] / lib / http / src / localcache.cpp
index 8bee99ea782c91463efcc1a496e1415d0dd70060..99b93150797282b636c3a97469b4f104643206e0 100644 (file)
@@ -10,8 +10,7 @@ LocalCache *LocalCache::instance(const char *name) {
 }
 
 LocalCache::LocalCache(const QByteArray &name)
-    : name(name), maxSeconds(86400 * 30), maxSize(1024 * 1024 * 100), size(0), expiring(false),
-      insertCount(0) {
+    : name(name), maxSeconds(86400 * 30), maxSize(1024 * 1024 * 100), size(0), insertCount(0) {
     directory = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + QLatin1Char('/') +
                 QLatin1String(name) + QLatin1Char('/');
 #ifndef QT_NO_DEBUG_OUTPUT
@@ -42,10 +41,9 @@ QByteArray LocalCache::hash(const QByteArray &s) {
 }
 
 bool LocalCache::isCached(const QString &path) {
-    bool cached = (QFile::exists(path) &&
-                   (maxSeconds == 0 || QDateTime::currentDateTimeUtc().toTime_t() -
-                                                       QFileInfo(path).created().toTime_t() <
-                                               maxSeconds));
+    bool cached = QFile::exists(path) &&
+                  (maxSeconds == 0 || QFileInfo(path).birthTime().secsTo(
+                                              QDateTime::currentDateTimeUtc()) < maxSeconds);
 #ifndef QT_NO_DEBUG_OUTPUT
     if (!cached) misses++;
 #endif
@@ -71,51 +69,45 @@ QByteArray LocalCache::value(const QByteArray &key) {
 }
 
 void LocalCache::insert(const QByteArray &key, const QByteArray &value) {
-    const QueueItem item = {key, value};
-    insertQueue.append(item);
-    QTimer::singleShot(0, [this]() {
-        if (insertQueue.isEmpty()) return;
-        for (const auto &item : insertQueue) {
-            const QString path = cachePath(item.key);
-            const QString parentDir = path.left(path.lastIndexOf('/'));
-            if (!QFile::exists(parentDir)) {
-                QDir().mkpath(parentDir);
-            }
-            QFile file(path);
-            if (!file.open(QIODevice::WriteOnly)) {
-                qWarning() << "Cannot create" << path;
-                continue;
-            }
-            file.write(item.value);
-            file.close();
-            if (size > 0) size += item.value.size();
-        }
-        insertQueue.clear();
-
-        // expire cache every n inserts
-        if (maxSize > 0 && ++insertCount % 100 == 0) {
-            if (size == 0 || size > maxSize) size = expire();
-        }
-    });
+    qDebug() << "Inserting" << key;
+    const QString path = cachePath(key);
+    const QString parentDir = path.left(path.lastIndexOf(QLatin1Char('/')));
+    if (!QFile::exists(parentDir)) {
+        QDir().mkpath(parentDir);
+    }
+    QFile file(path);
+    if (!file.open(QIODevice::WriteOnly)) {
+        qWarning() << "Cannot create" << path;
+        return;
+    }
+    file.write(value);
+    file.close();
+    if (size > 0) size += value.size();
+
+    // expire cache every n inserts
+    if (maxSize > 0 && ++insertCount % 100 == 0) {
+        if (size == 0 || size > maxSize) expire();
+    }
 }
 
-bool LocalCache::clear() {
+void LocalCache::clear() {
 #ifndef QT_NO_DEBUG_OUTPUT
     hits = 0;
     misses = 0;
 #endif
     size = 0;
     insertCount = 0;
-    return QDir(directory).removeRecursively();
+    mutex.lock();
+    QDir(directory).removeRecursively();
+    mutex.unlock();
 }
 
 QString LocalCache::cachePath(const QByteArray &key) const {
-    return directory + QLatin1String(key.constData());
+    return directory + QLatin1String(key);
 }
 
-qint64 LocalCache::expire() {
-    if (expiring) return size;
-    expiring = true;
+void LocalCache::expire() {
+    if (!mutex.tryLock()) return;
 
     QDir::Filters filters = QDir::AllDirs | QDir::Files | QDir::NoDotAndDotDot;
     QDirIterator it(directory, filters, QDirIterator::Subdirectories);
@@ -125,7 +117,7 @@ qint64 LocalCache::expire() {
     while (it.hasNext()) {
         QString path = it.next();
         QFileInfo info = it.fileInfo();
-        cacheItems.insert(info.created(), path);
+        cacheItems.insert(info.birthTime(), path);
         totalSize += info.size();
         qApp->processEvents();
     }
@@ -152,9 +144,8 @@ qint64 LocalCache::expire() {
     }
 #endif
 
-    expiring = false;
-
-    return totalSize;
+    size = totalSize;
+    mutex.unlock();
 }
 
 #ifndef QT_NO_DEBUG_OUTPUT