diff options
| author | Mike Skec <skec@protonmail.ch> | 2021-01-08 15:34:54 +1100 |
|---|---|---|
| committer | Felix Queißner <felix@ib-queissner.de> | 2021-01-08 11:12:14 +0100 |
| commit | 35756f7a1b49f8d7aa603dd3bbe3c0a2c2d234a6 (patch) | |
| tree | c9a8fd3bc483623cd1fa294ee8eb866dd189b93c /src | |
| parent | 443da5e34a3b858a2a32c6ca6846e351fa11f389 (diff) | |
| download | kristall-35756f7a1b49f8d7aa603dd3bbe3c0a2c2d234a6.tar.gz | |
cache: item expiry and max item size
currently no total limit yet
Diffstat (limited to 'src')
| -rw-r--r-- | src/browsertab.cpp | 1 | ||||
| -rw-r--r-- | src/cachehandler.cpp | 65 | ||||
| -rw-r--r-- | src/cachehandler.hpp | 17 | ||||
| -rw-r--r-- | src/dialogs/settingsdialog.cpp | 22 | ||||
| -rw-r--r-- | src/dialogs/settingsdialog.hpp | 6 | ||||
| -rw-r--r-- | src/dialogs/settingsdialog.ui | 73 | ||||
| -rw-r--r-- | src/kristall.hpp | 5 | ||||
| -rw-r--r-- | src/main.cpp | 8 |
8 files changed, 185 insertions, 12 deletions
diff --git a/src/browsertab.cpp b/src/browsertab.cpp index 873165f..0568ee3 100644 --- a/src/browsertab.cpp +++ b/src/browsertab.cpp @@ -1471,6 +1471,7 @@ bool BrowserTab::startRequest(const QUrl &url, ProtocolHandler::RequestOptions o } // Check if we have the page in our cache. + kristall::cache.clean(); if (auto pg = kristall::cache.find(url); pg != nullptr) { qDebug() << "Reading page from cache"; diff --git a/src/cachehandler.cpp b/src/cachehandler.cpp index 5003b1d..33e6940 100644 --- a/src/cachehandler.cpp +++ b/src/cachehandler.cpp @@ -6,21 +6,39 @@ void CacheHandler::push(const QUrl &u, const QByteArray &body, const MimeType &mime) { + // Skip if this item is above the cached item size threshold + int bodysize = body.size(); + if (bodysize > (kristall::options.cache_threshold * 1024)) + { + qDebug() << "cache: item exceeds threshold (" << IoUtil::size_human(body.size()) << ")"; + return; + } + + // 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"; + //} + QUrl url = IoUtil::uniformUrl(u); QString urlstr = url.toString(QUrl::FullyEncoded);; if (this->page_cache.find(urlstr) != this->page_cache.end()) { - qDebug() << "Updating cached page"; + qDebug() << "cache: updating page"; auto pg = this->page_cache[urlstr]; pg->body = body; pg->mime = mime; + pg->time_cached = QDateTime::currentDateTime(); return; } - this->page_cache[urlstr] = std::make_shared<CachedPage>(u, body, mime); + this->page_cache[urlstr] = std::make_shared<CachedPage>( + u, body, mime, QDateTime::currentDateTime()); - qDebug() << "Pushed page to cache: " << url; + qDebug() << "cache: pushing url " << url; return; } @@ -29,7 +47,7 @@ std::shared_ptr<CachedPage> CacheHandler::find(const QString &url) { if (this->page_cache.find(url) != this->page_cache.end()) { - return page_cache[url]; + return this->page_cache[url]; } return nullptr; } @@ -39,16 +57,51 @@ std::shared_ptr<CachedPage> CacheHandler::find(const QUrl &url) return this->find(IoUtil::uniformUrlString(url)); } -bool CacheHandler::contains(const QString &url) const +bool CacheHandler::contains(const QString &url) { return this->page_cache.find(url) != this->page_cache.end(); } -bool CacheHandler::contains(const QUrl &url) const +bool CacheHandler::contains(const QUrl &url) { return this->contains(IoUtil::uniformUrlString(url)); } +int CacheHandler::size() +{ + int s = 0; + + for (auto& i : this->page_cache) + s += i.second->body.size(); + + return s; +} + +// Clears expired pages out of cache +void CacheHandler::clean() +{ + // Find list of keys to delete + std::vector<QString> vec; + for (auto&& i : this->page_cache) + { + if (QDateTime::currentDateTime() > i.second->time_cached + .addSecs(kristall::options.cache_life * 60)) + { + vec.emplace_back(std::move(i.first)); + } + } + + // Delete them + int count = 0; + for (auto&& key : vec) + { + this->page_cache.erase(key); + ++count; + } + + if (count) qDebug() << "cache: cleaned " << count << " expired pages out of cache"; +} + CacheMap const& CacheHandler::getPages() const { return this->page_cache; diff --git a/src/cachehandler.hpp b/src/cachehandler.hpp index 5aab2b6..a8bdb0f 100644 --- a/src/cachehandler.hpp +++ b/src/cachehandler.hpp @@ -9,6 +9,7 @@ #include <QString> #include <QByteArray> #include <QtGlobal> +#include <QDateTime> // Need a QString hash implementation for Qt versions below 5.14 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0) @@ -36,14 +37,18 @@ struct CachedPage int scroll_pos; + QDateTime time_cached; + // also: maybe compress page contents? May test // to see if it's worth it - CachedPage(const QUrl &url, const QByteArray &body, const MimeType &mime) - : url(url), body(body), mime(mime), scroll_pos(-1) + CachedPage(const QUrl &url, const QByteArray &body, + const MimeType &mime, const QDateTime &cached) + : url(url), body(body), mime(mime), scroll_pos(-1), time_cached(cached) {} }; +// TODO: move away from the 'unordered_map' type? typedef std::unordered_map<QString, std::shared_ptr<CachedPage>> CacheMap; class CacheHandler @@ -53,14 +58,18 @@ public: std::shared_ptr<CachedPage> find(QUrl const &url); - bool contains(QUrl const & url) const; + bool contains(QUrl const & url); + + int size(); + + void clean(); CacheMap const& getPages() const; private: std::shared_ptr<CachedPage> find(QString const &url); - bool contains(QString const & url) const; + bool contains(QString const & url); private: // In-memory cache storage. diff --git a/src/dialogs/settingsdialog.cpp b/src/dialogs/settingsdialog.cpp index f18065f..0925578 100644 --- a/src/dialogs/settingsdialog.cpp +++ b/src/dialogs/settingsdialog.cpp @@ -250,6 +250,10 @@ void SettingsDialog::setOptions(const GenericSettings &options) this->ui->network_timeout->setValue(this->current_options.network_timeout); this->ui->enable_home_btn->setChecked(this->current_options.enable_home_btn); + + this->ui->cache_limit->setValue(this->current_options.cache_limit); + this->ui->cache_threshold->setValue(this->current_options.cache_threshold); + this->ui->cache_life->setValue(this->current_options.cache_life); } GenericSettings SettingsDialog::options() const @@ -660,8 +664,7 @@ void SettingsDialog::on_redirection_mode_currentIndexChanged(int index) this->current_options.redirection_policy = GenericSettings::RedirectionWarning(this->ui->redirection_mode->itemData(index).toInt()); } -void SettingsDialog::on_max_redirects_valueChanged(int max_redirections) -{ +void SettingsDialog::on_max_redirects_valueChanged(int max_redirections) { this->current_options.max_redirections = max_redirections; } @@ -674,3 +677,18 @@ void SettingsDialog::on_enable_home_btn_clicked(bool checked) { this->current_options.enable_home_btn = checked; } + +void SettingsDialog::on_cache_limit_valueChanged(int limit) +{ + this->current_options.cache_limit = limit; +} + +void SettingsDialog::on_cache_threshold_valueChanged(int thres) +{ + this->current_options.cache_threshold = thres; +} + +void SettingsDialog::on_cache_life_valueChanged(int life) +{ + this->current_options.cache_life = life; +} diff --git a/src/dialogs/settingsdialog.hpp b/src/dialogs/settingsdialog.hpp index fd1d7cb..dac7a10 100644 --- a/src/dialogs/settingsdialog.hpp +++ b/src/dialogs/settingsdialog.hpp @@ -132,6 +132,12 @@ private slots: void on_enable_home_btn_clicked(bool arg1); + void on_cache_limit_valueChanged(int limit); + + void on_cache_threshold_valueChanged(int thres); + + void on_cache_life_valueChanged(int life); + private: void reloadStylePreview(); diff --git a/src/dialogs/settingsdialog.ui b/src/dialogs/settingsdialog.ui index c61b62a..a194f40 100644 --- a/src/dialogs/settingsdialog.ui +++ b/src/dialogs/settingsdialog.ui @@ -371,6 +371,79 @@ </property> </widget> </item> + + <item row="14" column="0"> + <widget class="QLabel" name="label_30"> + <property name="text"> + <string>Total cache size limit</string> + </property> + <property name="toolTip"> + <string>The total amount of memory that can be occupied by cached items. Set to zero to disable in-memory caching.</string> + </property> + </widget> + </item> + <item row="14" column="1"> + <widget class="QSpinBox" name="cache_limit"> + <property name="suffix"> + <string> MiB</string> + </property> + <property name="minimum"> + <number>0</number> + </property> + <property name="maximum"> + <number>4000</number> + </property> + </widget> + </item> + + <item row="15" column="0"> + <widget class="QLabel" name="label_31"> + <property name="text"> + <string>Cached item size threshold</string> + </property> + <property name="toolTip"> + <string>Items which are below this threshold are cached in memory. Any above are simply discarded.</string> + </property> + </widget> + </item> + <item row="15" column="1"> + <widget class="QSpinBox" name="cache_threshold"> + <property name="suffix"> + <string> KiB</string> + </property> + <property name="minimum"> + <number>0</number> + </property> + <property name="maximum"> + <number>102400</number> + </property> + </widget> + </item> + + <item row="16" column="0"> + <widget class="QLabel" name="label_31"> + <property name="text"> + <string>Cached item life</string> + </property> + <property name="toolTip"> + <string>How long cached items last before they are expired and require a reload.</string> + </property> + </widget> + </item> + <item row="16" column="1"> + <widget class="QSpinBox" name="cache_life"> + <property name="suffix"> + <string> minutes</string> + </property> + <property name="minimum"> + <number>0</number> + </property> + <property name="maximum"> + <number>8760</number> + </property> + </widget> + </item> + </layout> </widget> <widget class="QWidget" name="style_tab"> diff --git a/src/kristall.hpp b/src/kristall.hpp index cd863dd..ceefa29 100644 --- a/src/kristall.hpp +++ b/src/kristall.hpp @@ -62,6 +62,11 @@ struct GenericSettings // Additional toolbar items bool enable_home_btn = false; + // In-memory caching + int cache_limit = 100; + int cache_threshold = 50; + int cache_life = 10; + void load(QSettings & settings); void save(QSettings & settings) const; }; diff --git a/src/main.cpp b/src/main.cpp index 5200ce3..a776a80 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -368,6 +368,10 @@ void GenericSettings::load(QSettings &settings) redirection_policy = RedirectionWarning(settings.value("redirection_policy ", WarnOnHostChange).toInt()); 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_life = settings.value("cache_life", 15).toInt(); } void GenericSettings::save(QSettings &settings) const @@ -398,6 +402,10 @@ void GenericSettings::save(QSettings &settings) const settings.setValue("redirection_policy", int(redirection_policy)); settings.setValue("network_timeout", network_timeout); settings.setValue("enable_home_btn", enable_home_btn); + + settings.setValue("cache_limit", cache_limit); + settings.setValue("cache_threshold", cache_threshold); + settings.setValue("cache_life", cache_life); } |
