]> git.sur5r.net Git - minitube/blob - lib/http/src/localcache.cpp
99b93150797282b636c3a97469b4f104643206e0
[minitube] / lib / http / src / localcache.cpp
1 #include "localcache.h"
2
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);
9     return instance;
10 }
11
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
17     hits = 0;
18     misses = 0;
19 #endif
20 }
21
22 LocalCache::~LocalCache() {
23 #ifndef QT_NO_DEBUG_OUTPUT
24     debugStats();
25 #endif
26 }
27
28 QByteArray LocalCache::hash(const QByteArray &s) {
29     QCryptographicHash hash(QCryptographicHash::Sha1);
30     hash.addData(s);
31     const QByteArray h = QByteArray::number(*(qlonglong *)hash.result().constData(), 36);
32     static const char sep('/');
33     QByteArray p;
34     p.reserve(h.length() + 2);
35     p.append(h.at(0));
36     p.append(sep);
37     p.append(h.at(1));
38     p.append(sep);
39     p.append(h.constData() + 2, strlen(h.constData()) - 2); // p.append(h.mid(2));
40     return p;
41 }
42
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++;
49 #endif
50     return cached;
51 }
52
53 QByteArray LocalCache::value(const QByteArray &key) {
54     const QString path = cachePath(key);
55     if (!isCached(path)) return QByteArray();
56
57     QFile file(path);
58     if (!file.open(QIODevice::ReadOnly)) {
59         qWarning() << __PRETTY_FUNCTION__ << file.fileName() << file.errorString();
60 #ifndef QT_NO_DEBUG_OUTPUT
61         misses++;
62 #endif
63         return QByteArray();
64     }
65 #ifndef QT_NO_DEBUG_OUTPUT
66     hits++;
67 #endif
68     return file.readAll();
69 }
70
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);
77     }
78     QFile file(path);
79     if (!file.open(QIODevice::WriteOnly)) {
80         qWarning() << "Cannot create" << path;
81         return;
82     }
83     file.write(value);
84     file.close();
85     if (size > 0) size += value.size();
86
87     // expire cache every n inserts
88     if (maxSize > 0 && ++insertCount % 100 == 0) {
89         if (size == 0 || size > maxSize) expire();
90     }
91 }
92
93 void LocalCache::clear() {
94 #ifndef QT_NO_DEBUG_OUTPUT
95     hits = 0;
96     misses = 0;
97 #endif
98     size = 0;
99     insertCount = 0;
100     mutex.lock();
101     QDir(directory).removeRecursively();
102     mutex.unlock();
103 }
104
105 QString LocalCache::cachePath(const QByteArray &key) const {
106     return directory + QLatin1String(key);
107 }
108
109 void LocalCache::expire() {
110     if (!mutex.tryLock()) return;
111
112     QDir::Filters filters = QDir::AllDirs | QDir::Files | QDir::NoDotAndDotDot;
113     QDirIterator it(directory, filters, QDirIterator::Subdirectories);
114
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();
123     }
124
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();
131         QFile file(name);
132         qint64 size = file.size();
133         file.remove();
134         totalSize -= size;
135         ++removedFiles;
136         ++i;
137         qApp->processEvents();
138     }
139 #ifndef QT_NO_DEBUG_OUTPUT
140     debugStats();
141     if (removedFiles > 0) {
142         qDebug() << "Removed:" << removedFiles << "Kept:" << cacheItems.count() - removedFiles
143                  << "New Size:" << totalSize;
144     }
145 #endif
146
147     size = totalSize;
148     mutex.unlock();
149 }
150
151 #ifndef QT_NO_DEBUG_OUTPUT
152 void LocalCache::debugStats() {
153     int total = hits + misses;
154     if (total > 0) {
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 << "%";
160     }
161 }
162 #endif