diff options
| author | Mike Skec <skec@protonmail.ch> | 2021-01-08 20:46:56 +1100 |
|---|---|---|
| committer | Felix Queißner <felix@ib-queissner.de> | 2021-01-08 11:12:14 +0100 |
| commit | b6bd6f442ac95376d78a14b3f503cb065781a5a5 (patch) | |
| tree | b33b7ff82c4079ceadca65fe6c2c5da0b0c62cb8 /src/cachehandler.cpp | |
| parent | 35756f7a1b49f8d7aa603dd3bbe3c0a2c2d234a6 (diff) | |
Functional cache limit, albeit a little inefficient
Diffstat (limited to 'src/cachehandler.cpp')
| -rw-r--r-- | src/cachehandler.cpp | 39 |
1 files changed, 33 insertions, 6 deletions
diff --git a/src/cachehandler.cpp b/src/cachehandler.cpp index 33e6940..3559fd4 100644 --- a/src/cachehandler.cpp +++ b/src/cachehandler.cpp @@ -15,12 +15,10 @@ void CacheHandler::push(const QUrl &u, const QByteArray &body, const MimeType &m } // Pop cached items until we are below the cache limit - // TODO - //if ((bodysize + this->size()) > (kristall::options.cache_limit * 1024 * 1024)) - //while ((bodysize + this->size()) > (kristall::options.cache_limit * 1024 * 1024)) - //{ - // qDebug() << "cache: adding item will exceed cache limit"; - //} + while ((bodysize + this->size()) > (kristall::options.cache_limit * 1024)) + { + this->popOldest(); + } QUrl url = IoUtil::uniformUrl(u); QString urlstr = url.toString(QUrl::FullyEncoded);; @@ -106,3 +104,32 @@ CacheMap const& CacheHandler::getPages() const { return this->page_cache; } + +void CacheHandler::popOldest() +{ + if (this->page_cache.size() == 0) + { + return; + } + + // This will iterate over the cache map, + // find the key with the oldest timestamp, + // and erase it from the map. + // + // (TODO: make this more efficient somehow?) + + QDateTime oldest = QDateTime::currentDateTime(); + QString oldest_key; + for (auto it = this->page_cache.begin(); it != this->page_cache.end(); ++it) + { + if (it->second->time_cached < oldest) + { + oldest = it->second->time_cached; + oldest_key = it->first; + } + } + + // Erase it from the map + qDebug() << "cache: popping " << oldest_key; + this->page_cache.erase(oldest_key); +} |
