From de0e7b17e1324c8f766ba751b2069579bb577940 Mon Sep 17 00:00:00 2001 From: Antoine Fontaine <antoine.fontaine@epfl.ch> Date: Thu, 6 Jun 2019 10:03:13 +0200 Subject: [PATCH] Added decent caching support; but index is still never cached --- config.h.in | 8 ++++++-- src/moodle-provider.c | 26 ++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/config.h.in b/config.h.in index ae6145b..b99ca13 100644 --- a/config.h.in +++ b/config.h.in @@ -3,10 +3,12 @@ // enables non-standard api connection (the only one supported at the moment) #define EPFL -#define USERNAME "afontain" - #mesondefine CACHE_ROOT +// how long we consider the cache «up to date»: for index files, +// the default is one hour; but for regular file, it is 10 years. +#define CACHE_TIME_HTML 3600 +#define CACHE_TIME_FILES (10*365*24*3600) // how verbose the http requests are: // 0 – none @@ -21,3 +23,5 @@ #include "epfl.h" #endif // EPFL + + diff --git a/src/moodle-provider.c b/src/moodle-provider.c index 67f53c5..5b41b70 100644 --- a/src/moodle-provider.c +++ b/src/moodle-provider.c @@ -118,6 +118,27 @@ get_index_filename (void) } +static gboolean +cache_is_usable_for (const gchar *filename) +{ + g_autoptr (GFile) file = g_file_new_for_uri (filename); + g_autoptr (GFileInfo) fileinfo = g_file_query_info (file, + G_FILE_ATTRIBUTE_TIME_MODIFIED, + G_FILE_QUERY_INFO_NONE, + NULL, + NULL); + if (fileinfo) { + guint64 mtime = g_file_info_get_attribute_uint64 (fileinfo, G_FILE_ATTRIBUTE_TIME_MODIFIED); + guint64 now = g_get_real_time () / 1000000; // that one is in µs + + if (g_str_has_suffix (filename, ".html")) { + return CACHE_TIME_HTML > (now - mtime); // if we updated recently enough, that's good + } + return CACHE_TIME_FILES > (now - mtime); // non-html files are less likely to be updated + } + return FALSE; // no file info: this file doesn't exists; we better not rely on cache +} + static void request_url_cb (GObject *unused, GAsyncResult *res, @@ -158,7 +179,8 @@ request_url (const gchar *url, provider_cb final_cb, gpointer user_data) { - if (self.online_mode == MOODLE_MODE_ONLINE) { + + if (!cache_is_usable_for (filename) && self.online_mode == MOODLE_MODE_ONLINE) { SoupMessage *msg; msg = soup_message_new ("GET", url); @@ -170,7 +192,7 @@ request_url (const gchar *url, soup_session_send_async (self.session, msg, NULL, request_url_cb, (gpointer)cb_info); } else { // offline_mode - +g_print ("using cache for %s\n", filename); // this is not quite async, but works «fast enough» g_autoptr (GFile) file = g_file_new_for_uri (filename); GInputStream *is; -- GitLab