}
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
}
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
}
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);
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();
}
}
#endif
- expiring = false;
-
- return totalSize;
+ size = totalSize;
+ mutex.unlock();
}
#ifndef QT_NO_DEBUG_OUTPUT