diff options
author | Jaakko Keränen <jaakko.keranen@iki.fi> | 2021-01-27 21:28:25 +0200 |
---|---|---|
committer | Jaakko Keränen <jaakko.keranen@iki.fi> | 2021-01-27 21:28:25 +0200 |
commit | eda45fcd34189e6844babde1ebc60c083b1b09da (patch) | |
tree | 40a5aa0671a59a4180913cee518735a3c72f3f5c /src/app.c | |
parent | 688fe6c5882e7493f0e2750f0ff8f20d3613e270 (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/app.c')
-rw-r--r-- | src/app.c | 42 |
1 files changed, 41 insertions, 1 deletions
@@ -194,6 +194,7 @@ static iString *serializePrefs_App_(const iApp *d) { | |||
194 | appendFormat_String(str, "zoom.set arg:%d\n", d->prefs.zoomPercent); | 194 | appendFormat_String(str, "zoom.set arg:%d\n", d->prefs.zoomPercent); |
195 | appendFormat_String(str, "smoothscroll arg:%d\n", d->prefs.smoothScrolling); | 195 | appendFormat_String(str, "smoothscroll arg:%d\n", d->prefs.smoothScrolling); |
196 | appendFormat_String(str, "imageloadscroll arg:%d\n", d->prefs.loadImageInsteadOfScrolling); | 196 | appendFormat_String(str, "imageloadscroll arg:%d\n", d->prefs.loadImageInsteadOfScrolling); |
197 | appendFormat_String(str, "cachesize.set arg:%d\n", d->prefs.maxCacheSize); | ||
197 | appendFormat_String(str, "decodeurls arg:%d\n", d->prefs.decodeUserVisibleURLs); | 198 | appendFormat_String(str, "decodeurls arg:%d\n", d->prefs.decodeUserVisibleURLs); |
198 | appendFormat_String(str, "linewidth.set arg:%d\n", d->prefs.lineWidth); | 199 | appendFormat_String(str, "linewidth.set arg:%d\n", d->prefs.lineWidth); |
199 | appendFormat_String(str, "prefs.biglede.changed arg:%d\n", d->prefs.bigFirstParagraph); | 200 | appendFormat_String(str, "prefs.biglede.changed arg:%d\n", d->prefs.bigFirstParagraph); |
@@ -321,6 +322,7 @@ iObjectList *listDocuments_App(void) { | |||
321 | 322 | ||
322 | static void saveState_App_(const iApp *d) { | 323 | static void saveState_App_(const iApp *d) { |
323 | iUnused(d); | 324 | iUnused(d); |
325 | trimCache_App(); | ||
324 | iFile *f = newCStr_File(concatPath_CStr(dataDir_App_, stateFileName_App_)); | 326 | iFile *f = newCStr_File(concatPath_CStr(dataDir_App_, stateFileName_App_)); |
325 | if (open_File(f, writeOnly_FileMode)) { | 327 | if (open_File(f, writeOnly_FileMode)) { |
326 | writeData_File(f, magicState_App_, 4); | 328 | writeData_File(f, magicState_App_, 4); |
@@ -503,7 +505,7 @@ const iString *debugInfo_App(void) { | |||
503 | iString *msg = collectNew_String(); | 505 | iString *msg = collectNew_String(); |
504 | format_String(msg, "# Debug information\n"); | 506 | format_String(msg, "# Debug information\n"); |
505 | appendFormat_String(msg, "## Documents\n"); | 507 | appendFormat_String(msg, "## Documents\n"); |
506 | iForEach(ObjectList, k, listDocuments_App()) { | 508 | iForEach(ObjectList, k, iClob(listDocuments_App())) { |
507 | iDocumentWidget *doc = k.object; | 509 | iDocumentWidget *doc = k.object; |
508 | appendFormat_String(msg, "### Tab %zu: %s\n", | 510 | appendFormat_String(msg, "### Tab %zu: %s\n", |
509 | childIndex_Widget(constAs_Widget(doc)->parent, k.object), | 511 | childIndex_Widget(constAs_Widget(doc)->parent, k.object), |
@@ -857,6 +859,8 @@ static iBool handlePrefsCommands_(iWidget *d, const char *cmd) { | |||
857 | isSelected_Widget(findChild_Widget(d, "prefs.ostheme"))); | 859 | isSelected_Widget(findChild_Widget(d, "prefs.ostheme"))); |
858 | postCommandf_App("decodeurls arg:%d", | 860 | postCommandf_App("decodeurls arg:%d", |
859 | isSelected_Widget(findChild_Widget(d, "prefs.decodeurls"))); | 861 | isSelected_Widget(findChild_Widget(d, "prefs.decodeurls"))); |
862 | postCommandf_App("cachesize.set arg:%d", | ||
863 | toInt_String(text_InputWidget(findChild_Widget(d, "prefs.cachesize")))); | ||
860 | postCommandf_App("proxy.gemini address:%s", | 864 | postCommandf_App("proxy.gemini address:%s", |
861 | cstr_String(text_InputWidget(findChild_Widget(d, "prefs.proxy.gemini")))); | 865 | cstr_String(text_InputWidget(findChild_Widget(d, "prefs.proxy.gemini")))); |
862 | postCommandf_App("proxy.gopher address:%s", | 866 | postCommandf_App("proxy.gopher address:%s", |
@@ -940,6 +944,33 @@ iDocumentWidget *newTab_App(const iDocumentWidget *duplicateOf, iBool switchToNe | |||
940 | return doc; | 944 | return doc; |
941 | } | 945 | } |
942 | 946 | ||
947 | void trimCache_App(void) { | ||
948 | iApp *d = &app_; | ||
949 | size_t cacheSize = 0; | ||
950 | const size_t limit = d->prefs.maxCacheSize * 1000000; | ||
951 | iObjectList *docs = listDocuments_App(); | ||
952 | iForEach(ObjectList, i, docs) { | ||
953 | cacheSize += cacheSize_History(history_DocumentWidget(i.object)); | ||
954 | } | ||
955 | init_ObjectListIterator(&i, docs); | ||
956 | iBool wasPruned = iFalse; | ||
957 | while (cacheSize > limit) { | ||
958 | iDocumentWidget *doc = i.object; | ||
959 | const size_t pruned = pruneLeastImportant_History(history_DocumentWidget(doc)); | ||
960 | if (pruned) { | ||
961 | cacheSize -= pruned; | ||
962 | wasPruned = iTrue; | ||
963 | } | ||
964 | next_ObjectListIterator(&i); | ||
965 | if (!i.value) { | ||
966 | if (!wasPruned) break; | ||
967 | wasPruned = iFalse; | ||
968 | init_ObjectListIterator(&i, docs); | ||
969 | } | ||
970 | } | ||
971 | iRelease(docs); | ||
972 | } | ||
973 | |||
943 | static iBool handleIdentityCreationCommands_(iWidget *dlg, const char *cmd) { | 974 | static iBool handleIdentityCreationCommands_(iWidget *dlg, const char *cmd) { |
944 | iApp *d = &app_; | 975 | iApp *d = &app_; |
945 | if (equal_Command(cmd, "ident.temp.changed")) { | 976 | if (equal_Command(cmd, "ident.temp.changed")) { |
@@ -1154,6 +1185,13 @@ iBool handleCommand_App(const char *cmd) { | |||
1154 | postCommandf_App("theme.changed auto:1"); | 1185 | postCommandf_App("theme.changed auto:1"); |
1155 | return iTrue; | 1186 | return iTrue; |
1156 | } | 1187 | } |
1188 | else if (equal_Command(cmd, "cachesize.set")) { | ||
1189 | d->prefs.maxCacheSize = arg_Command(cmd); | ||
1190 | if (d->prefs.maxCacheSize <= 0) { | ||
1191 | d->prefs.maxCacheSize = 0; | ||
1192 | } | ||
1193 | return iTrue; | ||
1194 | } | ||
1157 | else if (equal_Command(cmd, "proxy.gemini")) { | 1195 | else if (equal_Command(cmd, "proxy.gemini")) { |
1158 | setCStr_String(&d->prefs.geminiProxy, suffixPtr_Command(cmd, "address")); | 1196 | setCStr_String(&d->prefs.geminiProxy, suffixPtr_Command(cmd, "address")); |
1159 | return iTrue; | 1197 | return iTrue; |
@@ -1331,6 +1369,8 @@ iBool handleCommand_App(const char *cmd) { | |||
1331 | dlg, format_CStr("prefs.saturation.%d", (int) (d->prefs.saturation * 3.99f))), | 1369 | dlg, format_CStr("prefs.saturation.%d", (int) (d->prefs.saturation * 3.99f))), |
1332 | selected_WidgetFlag, | 1370 | selected_WidgetFlag, |
1333 | iTrue); | 1371 | iTrue); |
1372 | setText_InputWidget(findChild_Widget(dlg, "prefs.cachesize"), | ||
1373 | collectNewFormat_String("%d", d->prefs.maxCacheSize)); | ||
1334 | setToggle_Widget(findChild_Widget(dlg, "prefs.decodeurls"), d->prefs.decodeUserVisibleURLs); | 1374 | setToggle_Widget(findChild_Widget(dlg, "prefs.decodeurls"), d->prefs.decodeUserVisibleURLs); |
1335 | setText_InputWidget(findChild_Widget(dlg, "prefs.proxy.gemini"), &d->prefs.geminiProxy); | 1375 | setText_InputWidget(findChild_Widget(dlg, "prefs.proxy.gemini"), &d->prefs.geminiProxy); |
1336 | setText_InputWidget(findChild_Widget(dlg, "prefs.proxy.gopher"), &d->prefs.gopherProxy); | 1376 | setText_InputWidget(findChild_Widget(dlg, "prefs.proxy.gopher"), &d->prefs.gopherProxy); |