[PATCH 1/1] grl-net: Use libsoup cache



If libsoup version is newer enough, use the cache feature it provides.

Thus, it will cache the content to speed up network access.

Signed-off-by: Juan A. Suarez Romero <jasuarez igalia com>
---
 configure.ac          |    4 ++++
 libs/net/Makefile.am  |    6 ++++++
 libs/net/grl-net-wc.c |   22 ++++++++++++++++++++++
 3 files changed, 32 insertions(+), 0 deletions(-)

diff --git a/configure.ac b/configure.ac
index c307620..8a91bd2 100644
--- a/configure.ac
+++ b/configure.ac
@@ -117,6 +117,9 @@ PKG_CHECK_MODULES(NET, libsoup-2.4,
 			HAVE_LIBSOUP=yes,
 			HAVE_LIBSOUP=no)
 
+PKG_CHECK_MODULES(NETCACHE, libsoup-2.4 >= 2.33.4,
+                            HAVE_CACHED_LIBSOUP=yes,
+                            HAVE_CACHED_LIBSOUP=no)
 AC_ARG_ENABLE([grl_net],
         AS_HELP_STRING([--enable-grl-net],
                 [Enable Grilo Net library (default: auto)]),
@@ -134,6 +137,7 @@ AC_ARG_ENABLE([grl_net],
         ])
 
 AM_CONDITIONAL(BUILD_GRILO_NET, test "x$HAVE_LIBSOUP" = "xyes")
+AM_CONDITIONAL(BUILD_GRILO_NET_WITH_CACHE, test "x$HAVE_CACHED_LIBSOUP" = "xyes")
 
 # ----------------------------------------------------------
 # DEBUG SUPPORT
diff --git a/libs/net/Makefile.am b/libs/net/Makefile.am
index d7542fa..b43cc9a 100644
--- a/libs/net/Makefile.am
+++ b/libs/net/Makefile.am
@@ -5,6 +5,7 @@
 #
 # Copyright (C) 2010 Igalia S.L. All rights reserved.
 
+
 lib_LTLIBRARIES = libgrlnet-@GRL_MAJORMINOR@.la
 noinst_PROGRAMS = wc-test
 
@@ -20,6 +21,11 @@ libgrlnet_@GRL_MAJORMINOR@_la_CFLAGS =	\
 	$(DEPS_CFLAGS)			\
 	$(NET_CFLAGS)
 
+if BUILD_GRILO_NET_WITH_CACHE
+libgrlnet_@GRL_MAJORMINOR@_la_CFLAGS += \
+	-DLIBSOUP_WITH_CACHE
+endif
+
 libgrlnet_@GRL_MAJORMINOR@_la_LIBADD =	\
 	$(top_builddir)/src/lib@GRL_NAME@.la	\
 	$(DEPS_LIBS)				\
diff --git a/libs/net/grl-net-wc.c b/libs/net/grl-net-wc.c
index 0b6253f..d9fd6bb 100644
--- a/libs/net/grl-net-wc.c
+++ b/libs/net/grl-net-wc.c
@@ -35,8 +35,14 @@
 #include "config.h"
 #endif
 
+#define LIBSOUP_USE_UNSTABLE_REQUEST_API
+
 #include <libsoup/soup.h>
 
+#ifdef LIBSOUP_WITH_CACHE
+#include <libsoup/soup-cache.h>
+#endif
+
 #include <grilo.h>
 #include "grl-net-wc.h"
 
@@ -59,6 +65,9 @@ typedef struct _RequestClosure RequestClosure;
 struct _GrlNetWcPrivate {
   SoupSession *session;
   SoupLoggerLogLevel log_level;
+#ifdef LIBSOUP_WITH_CACHE
+  SoupCache *cache;
+#endif
   guint throttling;
   GTimeVal last_request;
   GQueue *pending; /* closure queue for delayed requests */
@@ -141,6 +150,15 @@ grl_net_wc_init (GrlNetWc *wc)
 
   wc->priv->session = soup_session_async_new ();
   wc->priv->pending = g_queue_new ();
+#ifdef LIBSOUP_WITH_CACHE
+  gchar *cache_dir = g_build_filename (g_get_user_cache_dir (),
+                                       g_get_prgname (),
+                                       NULL);
+  wc->priv->cache = soup_cache_new (cache_dir, SOUP_CACHE_SINGLE_USER);
+  g_free (cache_dir);
+  soup_session_add_feature (wc->priv->session,
+                            SOUP_SESSION_FEATURE (wc->priv->cache));
+#endif
 }
 
 static void
@@ -150,6 +168,10 @@ grl_net_wc_finalize (GObject *object)
 
   wc = GRL_NET_WC (object);
   grl_net_wc_flush_delayed_requests (wc);
+#ifdef LIBSOUP_WITH_CACHE
+  soup_cache_clear (wc->priv->cache);
+  g_object_unref (wc->priv->cache);
+#endif
   g_queue_free (wc->priv->pending);
   g_object_unref (wc->priv->session);
 
-- 
1.7.1



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]