Skip to content
Snippets Groups Projects
Commit de0e7b17 authored by Antoine Fontaine's avatar Antoine Fontaine :8ball:
Browse files

Added decent caching support; but index is still never cached

parent e8146523
Branches
No related merge requests found
......@@ -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
......@@ -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;
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment