summaryrefslogtreecommitdiff
path: root/src/history.c
diff options
context:
space:
mode:
authorJaakko Keränen <jaakko.keranen@iki.fi>2021-01-27 21:28:25 +0200
committerJaakko Keränen <jaakko.keranen@iki.fi>2021-01-27 21:28:25 +0200
commiteda45fcd34189e6844babde1ebc60c083b1b09da (patch)
tree40a5aa0671a59a4180913cee518735a3c72f3f5c /src/history.c
parent688fe6c5882e7493f0e2750f0ff8f20d3613e270 (diff)
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
Diffstat (limited to 'src/history.c')
-rw-r--r--src/history.c43
1 files changed, 43 insertions, 0 deletions
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. */
27#include <the_Foundation/mutex.h> 27#include <the_Foundation/mutex.h>
28#include <the_Foundation/path.h> 28#include <the_Foundation/path.h>
29#include <the_Foundation/stringset.h> 29#include <the_Foundation/stringset.h>
30#include <math.h>
30 31
31static const size_t maxStack_History_ = 50; /* back/forward navigable items */ 32static const size_t maxStack_History_ = 50; /* back/forward navigable items */
32 33
@@ -285,6 +286,48 @@ void setCachedResponse_History(iHistory *d, const iGmResponse *response) {
285 unlock_Mutex(d->mtx); 286 unlock_Mutex(d->mtx);
286} 287}
287 288
289size_t cacheSize_History(const iHistory *d) {
290 size_t cached = 0;
291 lock_Mutex(d->mtx);
292 iConstForEach(Array, i, &d->recent) {
293 const iRecentUrl *url = i.value;
294 if (url->cachedResponse) {
295 cached += size_Block(&url->cachedResponse->body);
296 }
297 }
298 unlock_Mutex(d->mtx);
299 return cached;
300}
301
302size_t pruneLeastImportant_History(iHistory *d) {
303 size_t delta = 0;
304 size_t chosen = iInvalidPos;
305 double score = 0.0f;
306 iTime now;
307 initCurrent_Time(&now);
308 lock_Mutex(d->mtx);
309 iConstForEach(Array, i, &d->recent) {
310 const iRecentUrl *url = i.value;
311 if (url->cachedResponse) {
312 const double urlScore =
313 size_Block(&url->cachedResponse->body) *
314 pow(secondsSince_Time(&now, &url->cachedResponse->when) / 60.0, 1.25);
315 if (urlScore > score) {
316 chosen = index_ArrayConstIterator(&i);
317 score = urlScore;
318 }
319 }
320 }
321 if (chosen != iInvalidPos) {
322 iRecentUrl *url = at_Array(&d->recent, chosen);
323 delta = size_Block(&url->cachedResponse->body);
324 delete_GmResponse(url->cachedResponse);
325 url->cachedResponse = NULL;
326 }
327 unlock_Mutex(d->mtx);
328 return delta;
329}
330
288const iStringArray *searchContents_History(const iHistory *d, const iRegExp *pattern) { 331const iStringArray *searchContents_History(const iHistory *d, const iRegExp *pattern) {
289 iStringArray *urls = iClob(new_StringArray()); 332 iStringArray *urls = iClob(new_StringArray());
290 lock_Mutex(d->mtx); 333 lock_Mutex(d->mtx);