Timeline is cached.

This commit is contained in:
default 2022-09-30 09:56:29 +02:00
parent 4176799296
commit 3d544233a6
3 changed files with 77 additions and 5 deletions

56
data.c
View file

@ -178,7 +178,7 @@ float mtime(char *fn)
struct stat st; struct stat st;
float r = 0.0; float r = 0.0;
if (stat(fn, &st) != -1) if (fn && stat(fn, &st) != -1)
r = (float)st.st_mtim.tv_sec; r = (float)st.st_mtim.tv_sec;
return r; 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) d_char *_timeline_find_fn(snac *snac, char *id)
/* returns the file name of a timeline entry by its 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) void enqueue_input(snac *snac, char *msg, char *req, int retries)
/* enqueues an input message */ /* enqueues an input message */
{ {

21
html.c
View file

@ -713,11 +713,24 @@ int html_get_handler(d_char *req, char *q_path, char **body, int *b_size, char *
if (!login(&snac, req)) if (!login(&snac, req))
status = 401; status = 401;
else { 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); *body = history_get(&snac, "_timeline.html");
*b_size = strlen(*body); *b_size = strlen(*body);
status = 200; 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 else

5
snac.h
View file

@ -60,6 +60,7 @@ int follower_del(snac *snac, char *actor);
int follower_check(snac *snac, char *actor); int follower_check(snac *snac, char *actor);
d_char *follower_list(snac *snac); d_char *follower_list(snac *snac);
float timeline_mtime(snac *snac);
int timeline_here(snac *snac, char *id); int timeline_here(snac *snac, char *id);
d_char *_timeline_find_fn(snac *snac, char *id); d_char *_timeline_find_fn(snac *snac, char *id);
d_char *timeline_find(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); 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_input(snac *snac, char *msg, char *req, int retries);
void enqueue_output(snac *snac, char *msg, char *actor, int retries); void enqueue_output(snac *snac, char *msg, char *actor, int retries);