Added soup caching support.
authorDaniel Carl <danielcarl@gmx.de>
Mon, 4 Aug 2014 22:19:00 +0000 (00:19 +0200)
committerDaniel Carl <danielcarl@gmx.de>
Mon, 4 Aug 2014 22:19:00 +0000 (00:19 +0200)
Added new settings maximum-cache-size to set the size all the cache may use.
Default is 2000kB.

doc/vimb.1
src/config.def.h
src/main.c
src/main.h
src/setting.c
src/util.c

index 7944334..7a1d53a 100644 (file)
@@ -1047,6 +1047,10 @@ link. Default
 you have to escape the '|' as '\\|' else the '|' will terminate the :set
 command and start a new command.
 .TP
+.B maximum-cache-size (int)
+Size in kB used to cache various page data. This caching is independent from
+`pagecache' or `offlinecache'. To disable caching, the size could be set to '0'.
+.TP
 .B previouspattern (list)
 Patterns to use when guessing the previous page in a document. Each pattern is
 successively tested against each link in the page beginning from the last
index af20027..dd3c2b1 100644 (file)
@@ -46,7 +46,8 @@
 #endif
 /* enable HTTP Strict-Transport-Security*/
 #define FEATURE_HSTS
-
+/* enable soup caching - size can be configure by maximum-cache-size setting */
+#define FEATURE_SOUP_CACHE
 
 /* time in seconds after that message will be removed from inputbox if the
  * message where only temporary */
index b8f10d9..10199f6 100644 (file)
@@ -1004,6 +1004,14 @@ static void session_init(void)
      * when the setting hsts=on */
     vb.config.hsts_provider = hsts_provider_new();
 #endif
+#ifdef FEATURE_SOUP_CACHE
+    /* setup the soup cache but without setting the cache size - this is done in setting.c */
+    char *cache_dir      = util_get_cache_dir();
+    vb.config.soup_cache = soup_cache_new(cache_dir, SOUP_CACHE_SINGLE_USER);
+    soup_session_add_feature(vb.session, SOUP_SESSION_FEATURE(vb.config.soup_cache));
+    soup_cache_load(vb.config.soup_cache);
+    g_free(cache_dir);
+#endif
 }
 
 static void session_cleanup(void)
@@ -1014,6 +1022,12 @@ static void session_cleanup(void)
     g_object_unref(vb.config.hsts_provider);
     soup_session_remove_feature_by_type(vb.session, HSTS_TYPE_PROVIDER);
 #endif
+#ifdef FEATURE_SOUP_CACHE
+    /* commit all cache writes */
+    soup_cache_flush(vb.config.soup_cache);
+    /* make sure that the cache will be kept on next browser start */
+    soup_cache_dump(vb.config.soup_cache);
+#endif
 }
 
 static void register_init(void)
index cc5bc83..dc513e3 100644 (file)
@@ -323,6 +323,9 @@ typedef struct {
     gboolean     kioskmode;
 #ifdef FEATURE_HSTS
     HSTSProvider *hsts_provider;  /* the hsts session feature that is added to soup session */
+#endif
+#ifdef FEATURE_SOUP_CACHE
+    SoupCache    *soup_cache;     /* soup caching feature model */
 #endif
     GHashTable   *settings;
 } Config;
index 41f1c0e..d803b8b 100644 (file)
@@ -80,6 +80,9 @@ static int fullscreen(const char *name, int type, void *value, void *data);
 #ifdef FEATURE_HSTS
 static int hsts(const char *name, int type, void *value, void *data);
 #endif
+#ifdef FEATURE_SOUP_CACHE
+static int soup_cache(const char *name, int type, void *value, void *data);
+#endif
 static gboolean validate_js_regexp_list(const char *pattern);
 
 void setting_init()
@@ -200,6 +203,10 @@ void setting_init()
     setting_add("download-use-external", TYPE_BOOLEAN, &off, NULL, 0, NULL);
 #ifdef FEATURE_HSTS
     setting_add("hsts", TYPE_BOOLEAN, &on, hsts, 0, NULL);
+#endif
+#ifdef FEATURE_SOUP_CACHE
+    i = 2000;
+    setting_add("maximum-cache-size", TYPE_INTEGER, &i, soup_cache, 0, NULL);
 #endif
     setting_add("x-hint-command", TYPE_CHAR, &":o <C-R>;", NULL, 0, NULL);
 
@@ -806,6 +813,22 @@ static int hsts(const char *name, int type, void *value, void *data)
 }
 #endif
 
+#ifdef FEATURE_SOUP_CACHE
+static int soup_cache(const char *name, int type, void *value, void *data)
+{
+    int kilobytes = *(int*)value;
+
+    soup_cache_set_max_size(vb.config.soup_cache, kilobytes * 1000);
+
+    /* clear the cache if maximum-cache-size is set to zero - note that this
+     * will also effect other vimb instances */
+    if (!kilobytes) {
+        soup_cache_clear(vb.config.soup_cache);
+    }
+    return SETTING_OK;
+}
+#endif
+
 /**
  * Validated syntax given list of JavaScript RegExp patterns.
  * If validation fails, the error is shown to the user.
index b75d443..d07909b 100644 (file)
@@ -36,6 +36,10 @@ char *util_get_config_dir(void)
     return path;
 }
 
+/**
+ * Retrieves the path to the cach dir.
+ * Returned string must be freed.
+ */
 char *util_get_cache_dir(void)
 {
     char *path = g_build_filename(g_get_user_cache_dir(), PROJECT, NULL);