From eda45fcd34189e6844babde1ebc60c083b1b09da Mon Sep 17 00:00:00 2001 From: Jaakko Keränen Date: Wed, 27 Jan 2021 21:28:25 +0200 Subject: Added preference for maximum cache size Rather than simply limiting each tab's cache to 50 most recent URLs, there is now a user-configurable maximum size. If more content is cached, the oldest/largest responses will be removed from memory. The default maximum cache size is 10 MB. IssueID #109 --- src/history.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'src/history.c') diff --git a/src/history.c b/src/history.c index 202549a9..59d515dc 100644 --- a/src/history.c +++ b/src/history.c @@ -27,6 +27,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #include #include +#include static const size_t maxStack_History_ = 50; /* back/forward navigable items */ @@ -285,6 +286,48 @@ void setCachedResponse_History(iHistory *d, const iGmResponse *response) { unlock_Mutex(d->mtx); } +size_t cacheSize_History(const iHistory *d) { + size_t cached = 0; + lock_Mutex(d->mtx); + iConstForEach(Array, i, &d->recent) { + const iRecentUrl *url = i.value; + if (url->cachedResponse) { + cached += size_Block(&url->cachedResponse->body); + } + } + unlock_Mutex(d->mtx); + return cached; +} + +size_t pruneLeastImportant_History(iHistory *d) { + size_t delta = 0; + size_t chosen = iInvalidPos; + double score = 0.0f; + iTime now; + initCurrent_Time(&now); + lock_Mutex(d->mtx); + iConstForEach(Array, i, &d->recent) { + const iRecentUrl *url = i.value; + if (url->cachedResponse) { + const double urlScore = + size_Block(&url->cachedResponse->body) * + pow(secondsSince_Time(&now, &url->cachedResponse->when) / 60.0, 1.25); + if (urlScore > score) { + chosen = index_ArrayConstIterator(&i); + score = urlScore; + } + } + } + if (chosen != iInvalidPos) { + iRecentUrl *url = at_Array(&d->recent, chosen); + delta = size_Block(&url->cachedResponse->body); + delete_GmResponse(url->cachedResponse); + url->cachedResponse = NULL; + } + unlock_Mutex(d->mtx); + return delta; +} + const iStringArray *searchContents_History(const iHistory *d, const iRegExp *pattern) { iStringArray *urls = iClob(new_StringArray()); lock_Mutex(d->mtx); -- cgit v1.2.3