11#include <QDirIterator>
22DiscCache::DiscCache(
const QString &cacheDirectory)
23 : m_CacheDirectory(cacheDirectory)
24 , m_CacheLimit(300 * 1024 * 1024)
25 , m_CurrentCacheSize(0)
27 Q_ASSERT(!m_CacheDirectory.isEmpty() &&
"Passed empty cache directory!");
29 QFile file(indexFileName(m_CacheDirectory));
37 s >> m_CurrentCacheSize;
41 qWarning(
"Unable to open cache directory %s", qPrintable(m_CacheDirectory));
46DiscCache::~DiscCache()
48 QFile file(indexFileName(m_CacheDirectory));
55 s << m_CurrentCacheSize;
62quint64 DiscCache::cacheLimit()
const
67void DiscCache::clear()
69 QDirIterator it(m_CacheDirectory);
72 while (it.hasNext()) {
75 if (it.fileName() == indexFileName(m_CacheDirectory))
85 m_CurrentCacheSize = 0;
88bool DiscCache::exists(
const QString &key)
const
90 return m_Entries.contains(key);
93bool DiscCache::find(
const QString &key, QByteArray &data)
96 if (!m_Entries.contains(key))
100 QFile file(keyToFileName(key));
102 data = file.readAll();
111bool DiscCache::insert(
const QString &key,
const QByteArray &data)
114 QFile file(keyToFileName(key));
119 if (m_Entries.contains(key))
120 m_CurrentCacheSize -= m_Entries.value(key).second;
129 m_CurrentCacheSize += data.
length();
136void DiscCache::remove(
const QString &key)
139 if (!m_Entries.contains(key))
148 m_CurrentCacheSize -= m_Entries.value(key).second;
151 m_Entries.remove(key);
154void DiscCache::setCacheLimit(quint64 n)
161QString DiscCache::keyToFileName(
const QString &key)
const
163 QString fileName(key);
164 fileName.replace(QLatin1Char(
'/'), QLatin1Char(
'_'));
166 return m_CacheDirectory + QLatin1Char(
'/') + fileName;
169void DiscCache::cleanup()
172 auto fivePercent = quint64(m_CacheLimit * 0.05);
174 while (m_CurrentCacheSize > (m_CacheLimit - fivePercent)) {
178 QMapIterator<QString, QPair<QDateTime, quint64>> it(m_Entries);
179 while (it.hasNext()) {
182 if (it.value().first < oldestDate) {
183 oldestDate = it.value().first;
184 oldestKey = it.key();
Binds a QML item to a specific geodetic location in screen coordinates.
QByteArray first(qsizetype n) const const
qsizetype length() const const
QDateTime currentDateTime()
bool isEmpty() const const