From 3d544233a63a2105d8d91e7fd4f62f7703fef6e0 Mon Sep 17 00:00:00 2001 From: default Date: Fri, 30 Sep 2022 09:56:29 +0200 Subject: [PATCH] Timeline is cached. --- data.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- html.c | 21 +++++++++++++++++---- snac.h | 5 +++++ 3 files changed, 77 insertions(+), 5 deletions(-) diff --git a/data.c b/data.c index ee21a98..5eb729c 100644 --- a/data.c +++ b/data.c @@ -178,7 +178,7 @@ float mtime(char *fn) struct stat st; float r = 0.0; - if (stat(fn, &st) != -1) + if (fn && stat(fn, &st) != -1) r = (float)st.st_mtim.tv_sec; return r; @@ -275,6 +275,13 @@ d_char *follower_list(snac *snac) } +float timeline_mtime(snac *snac) +{ + xs *fn = xs_fmt("%s/timeline", snac->basedir); + return mtime(fn); +} + + d_char *_timeline_find_fn(snac *snac, char *id) /* returns the file name of a timeline entry by its id */ { @@ -831,6 +838,53 @@ int static_get(snac *snac, char *id, d_char **data, int *size) } +d_char *_history_fn(snac *snac, char *id) +/* gets the filename for the history */ +{ + return xs_fmt("%s/history/%s", snac->basedir, id); +} + + +float history_mtime(snac *snac, char * id) +{ + float t = 0.0; + xs *fn = _history_fn(snac, id); + + if (fn != NULL) + t = mtime(fn); + + return t; +} + + +void history_add(snac *snac, char *id, char *content, int size) +/* adds something to the history */ +{ + xs *fn = _history_fn(snac, id); + FILE *f; + + if ((f = fopen(fn, "w")) != NULL) { + fwrite(content, size, 1, f); + fclose(f); + } +} + + +d_char *history_get(snac *snac, char *id) +{ + d_char *content = NULL; + xs *fn = _history_fn(snac, id); + FILE *f; + + if ((f = fopen(fn, "r")) != NULL) { + content = xs_readall(f); + fclose(f); + } + + return content; +} + + void enqueue_input(snac *snac, char *msg, char *req, int retries) /* enqueues an input message */ { diff --git a/html.c b/html.c index fc42fb3..fd6fbdf 100644 --- a/html.c +++ b/html.c @@ -713,11 +713,24 @@ int html_get_handler(d_char *req, char *q_path, char **body, int *b_size, char * if (!login(&snac, req)) status = 401; else { - xs *list = timeline_list(&snac, 0xfffffff); + if (history_mtime(&snac, "_timeline.html") > timeline_mtime(&snac)) { + snac_debug(&snac, 1, xs_fmt("serving cached timeline")); - *body = html_timeline(&snac, list, 0); - *b_size = strlen(*body); - status = 200; + *body = history_get(&snac, "_timeline.html"); + *b_size = strlen(*body); + status = 200; + } + else { + snac_debug(&snac, 1, xs_fmt("building timeline")); + + xs *list = timeline_list(&snac, 0xfffffff); + + *body = html_timeline(&snac, list, 0); + *b_size = strlen(*body); + status = 200; + + history_add(&snac, "_timeline.html", *body, *b_size); + } } } else diff --git a/snac.h b/snac.h index 392f57d..451fd78 100644 --- a/snac.h +++ b/snac.h @@ -60,6 +60,7 @@ int follower_del(snac *snac, char *actor); int follower_check(snac *snac, char *actor); d_char *follower_list(snac *snac); +float timeline_mtime(snac *snac); int timeline_here(snac *snac, char *id); d_char *_timeline_find_fn(snac *snac, char *id); d_char *timeline_find(snac *snac, char *id); @@ -84,6 +85,10 @@ int actor_get(snac *snac, char *actor, d_char **data); int static_get(snac *snac, char *id, d_char **data, int *size); +float history_mtime(snac *snac, char *id); +void history_add(snac *snac, char *id, char *content, int size); +d_char *history_get(snac *snac, char *id); + void enqueue_input(snac *snac, char *msg, char *req, int retries); void enqueue_output(snac *snac, char *msg, char *actor, int retries);