Hi all,

It turns out that enabling libsoup's disk cache is fairly straightforward.

I left it enabled by default in the patch, but I am not proposing that in
any way. I am pretty sure this will apply fairly cleanly with other
patches, except maybe the change of the size of the togglestat struct.

There is probably a concurrency issue with this, where multiple surf
instances open the same cache. Note that I load on startup, and flush after
each page load. At least I haven't run into any corruption issues with it,
so maybe the worst that comes of that issue is fewer cache hits. Compared
to no cache at all, it is an improvement.

I used the command line options "d" and "D".

Beware that a disk cache may hold cache-based trackers for a longer
duration than the memory-only cache that is the default. I recommend
disabling the cache in any private sessions.

Thanks,

Ben
From 99b2620525722c687e7bb45b679053908419a46e Mon Sep 17 00:00:00 2001
From: Ben Woolley <tauto...@gmail.com>
Date: Wed, 7 Jan 2015 14:37:59 -0800
Subject: [PATCH] support for disk cache

---
 config.def.h |  3 +++
 surf.c       | 30 +++++++++++++++++++++++++++++-
 2 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/config.def.h b/config.def.h
index 80a0feb..4771931 100644
--- a/config.def.h
+++ b/config.def.h
@@ -4,6 +4,7 @@ static char *useragent      = "Mozilla/5.0 (X11; U; Unix; en-US) "
 	"Safari/537.15 Surf/"VERSION;
 static char *stylefile      = "~/.surf/style.css";
 static char *scriptfile     = "~/.surf/script.js";
+static char *cachefolder    = "~/.surf/cache/";
 
 static Bool kioskmode       = FALSE; /* Ignore shortcuts */
 static Bool showindicators  = TRUE;  /* Show indicators in window title */
@@ -24,6 +25,8 @@ static time_t sessiontime   = 3600;
 /* Webkit default features */
 static Bool enablescrollbars = TRUE;
 static Bool enablespatialbrowsing = TRUE;
+static Bool enablediskcache = TRUE;
+static int diskcachebytes = 5 * 1024 * 1024;
 static Bool enableplugins = TRUE;
 static Bool enablescripts = TRUE;
 static Bool enableinspector = TRUE;
diff --git a/surf.c b/surf.c
index 6beda59..fc4d019 100644
--- a/surf.c
+++ b/surf.c
@@ -78,10 +78,11 @@ static GdkNativeWindow embed = 0;
 static gboolean showxid = FALSE;
 static char winid[64];
 static gboolean usingproxy = 0;
-static char togglestat[8];
+static char togglestat[9];
 static char pagestat[3];
 static GTlsDatabase *tlsdb;
 static int policysel = 0;
+static SoupCache *diskcache = NULL;
 
 static void addaccelgroup(Client *c);
 static void beforerequest(WebKitWebView *w, WebKitWebFrame *f,
@@ -260,6 +261,10 @@ buttonrelease(WebKitWebView *web, GdkEventButton *e, GList *gl) {
 
 static void
 cleanup(void) {
+	if (diskcache) {
+		soup_cache_flush(diskcache);
+		soup_cache_dump(diskcache);
+	}
 	while(clients)
 		destroyclient(clients);
 	g_free(cookiefile);
@@ -648,6 +653,10 @@ loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
 	case WEBKIT_LOAD_FINISHED:
 		c->progress = 100;
 		updatetitle(c);
+		if (diskcache) {
+			soup_cache_flush(diskcache);
+			soup_cache_dump(diskcache);
+		}
 		break;
 	default:
 		break;
@@ -938,6 +947,8 @@ newwindow(Client *c, const Arg *arg, gboolean noembed) {
 		cmd[i++] = "-s";
 	if(showxid)
 		cmd[i++] = "-x";
+	if(enablediskcache)
+		cmd[i++] = "-D";
 	cmd[i++] = "-c";
 	cmd[i++] = cookiefile;
 	cmd[i++] = "--";
@@ -1115,6 +1126,7 @@ setup(void) {
 	cookiefile = buildpath(cookiefile);
 	scriptfile = buildpath(scriptfile);
 	stylefile = buildpath(stylefile);
+	cachefolder = buildpath(cachefolder);
 
 	/* request handler */
 	s = webkit_get_default_session();
@@ -1124,6 +1136,14 @@ setup(void) {
 			SOUP_SESSION_FEATURE(cookiejar_new(cookiefile, FALSE,
 					cookiepolicy_get())));
 
+	/* disk cache */
+	if (enablediskcache) {
+		diskcache = soup_cache_new(cachefolder, SOUP_CACHE_SINGLE_USER);
+		soup_cache_set_max_size(diskcache, diskcachebytes);
+		soup_cache_load(diskcache);
+		soup_session_add_feature(s, SOUP_SESSION_FEATURE(diskcache));
+	}
+
 	/* ssl */
 	tlsdb = g_tls_file_database_new(cafile, &error);
 
@@ -1308,6 +1328,8 @@ gettogglestat(Client *c){
 
 	togglestat[p++] = allowgeolocation? 'G': 'g';
 
+	togglestat[p++] = enablediskcache? 'D': 'd';
+
 	g_object_get(G_OBJECT(settings), "auto-load-images", &value, NULL);
 	togglestat[p++] = value? 'I': 'i';
 
@@ -1424,6 +1446,12 @@ main(int argc, char *argv[]) {
 	case 'c':
 		cookiefile = EARGF(usage());
 		break;
+	case 'd':
+		enablediskcache = 0;
+		break;
+	case 'D':
+		enablediskcache = 1;
+		break;
 	case 'e':
 		embed = strtol(EARGF(usage()), NULL, 0);
 		break;
-- 
2.1.2

Reply via email to