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 | |
| parent | 35756f7a1b49f8d7aa603dd3bbe3c0a2c2d234a6 (diff) | |
| download | kristall-b6bd6f442ac95376d78a14b3f503cb065781a5a5.tar.gz | |
Functional cache limit, albeit a little inefficient
Diffstat (limited to 'src')
| -rw-r--r-- | src/browsertab.cpp | 5 | ||||
| -rw-r--r-- | src/cachehandler.cpp | 39 | ||||
| -rw-r--r-- | src/cachehandler.hpp | 4 | ||||
| -rw-r--r-- | src/dialogs/settingsdialog.ui | 4 | ||||
| -rw-r--r-- | src/kristall.hpp | 6 | ||||
| -rw-r--r-- | src/main.cpp | 4 |
6 files changed, 43 insertions, 19 deletions
diff --git a/src/browsertab.cpp b/src/browsertab.cpp index 0568ee3..e870fe0 100644 --- a/src/browsertab.cpp +++ b/src/browsertab.cpp @@ -587,11 +587,6 @@ void BrowserTab::renderPage(const QByteArray &data, const MimeType &mime) document->setHtml(page_html); page_title = document->metaInformation(QTextDocument::DocumentTitle); - - // For now we don't cache HTML pages, because they will most - // of the time clog up the cache. - // TODO: preference for this? protocol-specific cache limits? - will_cache = false; } else if (not plaintext_only and mime.is("text","x-kristall-theme")) { 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); +} diff --git a/src/cachehandler.hpp b/src/cachehandler.hpp index a8bdb0f..0d20962 100644 --- a/src/cachehandler.hpp +++ b/src/cachehandler.hpp @@ -48,7 +48,7 @@ struct CachedPage {} }; -// TODO: move away from the 'unordered_map' type? +// Maybe unordered_map isn't the best type for this? typedef std::unordered_map<QString, std::shared_ptr<CachedPage>> CacheMap; class CacheHandler @@ -71,6 +71,8 @@ private: bool contains(QString const & url); + void popOldest(); + private: // In-memory cache storage. CacheMap page_cache; diff --git a/src/dialogs/settingsdialog.ui b/src/dialogs/settingsdialog.ui index a194f40..2eb04da 100644 --- a/src/dialogs/settingsdialog.ui +++ b/src/dialogs/settingsdialog.ui @@ -385,13 +385,13 @@ <item row="14" column="1"> <widget class="QSpinBox" name="cache_limit"> <property name="suffix"> - <string> MiB</string> + <string> KiB</string> </property> <property name="minimum"> <number>0</number> </property> <property name="maximum"> - <number>4000</number> + <number>4000000</number> </property> </widget> </item> diff --git a/src/kristall.hpp b/src/kristall.hpp index ceefa29..2be1065 100644 --- a/src/kristall.hpp +++ b/src/kristall.hpp @@ -63,9 +63,9 @@ struct GenericSettings bool enable_home_btn = false; // In-memory caching - int cache_limit = 100; - int cache_threshold = 50; - int cache_life = 10; + int cache_limit = 1000; + int cache_threshold = 125; + int cache_life = 15; void load(QSettings & settings); void save(QSettings & settings) const; diff --git a/src/main.cpp b/src/main.cpp index a776a80..19326e2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -369,8 +369,8 @@ void GenericSettings::load(QSettings &settings) enable_home_btn = settings.value("enable_home_btn", false).toBool(); - cache_limit = settings.value("cache_limit", 100).toInt(); - cache_threshold = settings.value("cache_threshold", 200).toInt(); + cache_limit = settings.value("cache_limit", 1000).toInt(); + cache_threshold = settings.value("cache_threshold", 125).toInt(); cache_life = settings.value("cache_life", 15).toInt(); } |
