1 #include "localcache.h"
3 LocalCache *LocalCache::instance(const char *name) {
4 static QMap<QByteArray, LocalCache *> instances;
5 auto i = instances.constFind(QByteArray::fromRawData(name, strlen(name)));
6 if (i != instances.constEnd()) return i.value();
7 LocalCache *instance = new LocalCache(name);
8 instances.insert(instance->getName(), instance);
12 LocalCache::LocalCache(const QByteArray &name)
13 : name(name), maxSeconds(86400 * 30), maxSize(1024 * 1024 * 100), size(0), insertCount(0) {
14 directory = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + QLatin1Char('/') +
15 QLatin1String(name) + QLatin1Char('/');
16 #ifndef QT_NO_DEBUG_OUTPUT
22 LocalCache::~LocalCache() {
23 #ifndef QT_NO_DEBUG_OUTPUT
28 QByteArray LocalCache::hash(const QByteArray &s) {
29 QCryptographicHash hash(QCryptographicHash::Sha1);
31 const QByteArray h = QByteArray::number(*(qlonglong *)hash.result().constData(), 36);
32 static const char sep('/');
34 p.reserve(h.length() + 2);
39 p.append(h.constData() + 2, strlen(h.constData()) - 2); // p.append(h.mid(2));
43 bool LocalCache::isCached(const QString &path) {
44 bool cached = QFile::exists(path) &&
45 (maxSeconds == 0 || QFileInfo(path).birthTime().secsTo(
46 QDateTime::currentDateTimeUtc()) < maxSeconds);
47 #ifndef QT_NO_DEBUG_OUTPUT
48 if (!cached) misses++;
53 QByteArray LocalCache::value(const QByteArray &key) {
54 const QString path = cachePath(key);
55 if (!isCached(path)) return QByteArray();
58 if (!file.open(QIODevice::ReadOnly)) {
59 qWarning() << __PRETTY_FUNCTION__ << file.fileName() << file.errorString();
60 #ifndef QT_NO_DEBUG_OUTPUT
65 #ifndef QT_NO_DEBUG_OUTPUT
68 return file.readAll();
71 void LocalCache::insert(const QByteArray &key, const QByteArray &value) {
72 qDebug() << "Inserting" << key;
73 const QString path = cachePath(key);
74 const QString parentDir = path.left(path.lastIndexOf(QLatin1Char('/')));
75 if (!QFile::exists(parentDir)) {
76 QDir().mkpath(parentDir);
79 if (!file.open(QIODevice::WriteOnly)) {
80 qWarning() << "Cannot create" << path;
85 if (size > 0) size += value.size();
87 // expire cache every n inserts
88 if (maxSize > 0 && ++insertCount % 100 == 0) {
89 if (size == 0 || size > maxSize) expire();
93 void LocalCache::clear() {
94 #ifndef QT_NO_DEBUG_OUTPUT
101 QDir(directory).removeRecursively();
105 QString LocalCache::cachePath(const QByteArray &key) const {
106 return directory + QLatin1String(key);
109 void LocalCache::expire() {
110 if (!mutex.tryLock()) return;
112 QDir::Filters filters = QDir::AllDirs | QDir::Files | QDir::NoDotAndDotDot;
113 QDirIterator it(directory, filters, QDirIterator::Subdirectories);
115 QMultiMap<QDateTime, QString> cacheItems;
116 qint64 totalSize = 0;
117 while (it.hasNext()) {
118 QString path = it.next();
119 QFileInfo info = it.fileInfo();
120 cacheItems.insert(info.birthTime(), path);
121 totalSize += info.size();
122 qApp->processEvents();
125 int removedFiles = 0;
126 qint64 goal = (maxSize * 9) / 10;
127 auto i = cacheItems.constBegin();
128 while (i != cacheItems.constEnd()) {
129 if (totalSize < goal) break;
130 QString name = i.value();
132 qint64 size = file.size();
137 qApp->processEvents();
139 #ifndef QT_NO_DEBUG_OUTPUT
141 if (removedFiles > 0) {
142 qDebug() << "Removed:" << removedFiles << "Kept:" << cacheItems.count() - removedFiles
143 << "New Size:" << totalSize;
151 #ifndef QT_NO_DEBUG_OUTPUT
152 void LocalCache::debugStats() {
153 int total = hits + misses;
155 qDebug() << "Cache:" << name << '\n'
156 << "Inserts:" << insertCount << '\n'
157 << "Requests:" << total << '\n'
158 << "Hits:" << hits << (hits * 100) / total << "%\n"
159 << "Misses:" << misses << (misses * 100) / total << "%";