[PATCH 1/2] ttl: Consolidate TTL calculation

Tim Nordell tim.nordell at logicpd.com
Fri Feb 26 21:57:08 CET 2016


Currently part of the TTL calculation is done outside of the
calc_ttl() function.  Ideally a single variable in main()
would have the total seconds until the cache expires, and
ctx.page.expires would be incremented by this, as well
as the parameter to cache_process getting this value in
seconds.

Signed-off-by: Tim Nordell <tim.nordell at logicpd.com>

diff --git a/cache.c b/cache.c
index 6736a01..417f1a2 100644
--- a/cache.c
+++ b/cache.c
@@ -126,7 +126,7 @@ static int is_expired(struct cache_slot *slot)
 	if (slot->ttl < 0)
 		return 0;
 	else
-		return slot->cache_st.st_mtime + slot->ttl * 60 < time(NULL);
+		return slot->cache_st.st_mtime + slot->ttl < time(NULL);
 }
 
 /* Check if the slot has been modified since we opened it.
diff --git a/cgit.c b/cgit.c
index fc482be..963aee8 100644
--- a/cgit.c
+++ b/cgit.c
@@ -1005,25 +1005,28 @@ static void cgit_parse_args(int argc, const char **argv)
 
 static int calc_ttl(void)
 {
-	if (!ctx.repo)
-		return ctx.cfg.cache_root_ttl;
-
-	if (!ctx.qry.page)
-		return ctx.cfg.cache_repo_ttl;
-
-	if (!strcmp(ctx.qry.page, "about"))
-		return ctx.cfg.cache_about_ttl;
-
-	if (!strcmp(ctx.qry.page, "snapshot"))
-		return ctx.cfg.cache_snapshot_ttl;
+	const int ttl_conversion_multiplier = 60;
+	int ttl;
 
-	if (ctx.qry.has_sha1)
-		return ctx.cfg.cache_static_ttl;
+	if (!ctx.repo)
+		ttl = ctx.cfg.cache_root_ttl;
+	else if (!ctx.qry.page)
+		ttl = ctx.cfg.cache_repo_ttl;
+	else if (!strcmp(ctx.qry.page, "about"))
+		ttl = ctx.cfg.cache_about_ttl;
+	else if (!strcmp(ctx.qry.page, "snapshot"))
+		ttl = ctx.cfg.cache_snapshot_ttl;
+	else if (ctx.qry.has_sha1)
+		ttl = ctx.cfg.cache_static_ttl;
+	else if (ctx.qry.has_symref)
+		ttl = ctx.cfg.cache_dynamic_ttl;
+	else
+		ttl = ctx.cfg.cache_repo_ttl;
 
-	if (ctx.qry.has_symref)
-		return ctx.cfg.cache_dynamic_ttl;
+	if (ttl >= 0)
+		return ttl * ttl_conversion_multiplier;
 
-	return ctx.cfg.cache_repo_ttl;
+	return 10 * 365 * 24 * 60 * 60; /* 10 years */
 }
 
 int main(int argc, const char **argv)
@@ -1076,10 +1079,7 @@ int main(int argc, const char **argv)
 	authenticate_cookie();
 
 	ttl = calc_ttl();
-	if (ttl < 0)
-		ctx.page.expires += 10 * 365 * 24 * 60 * 60; /* 10 years */
-	else
-		ctx.page.expires += ttl * 60;
+	ctx.page.expires += ttl;
 	if (!ctx.env.authenticated || (ctx.env.request_method && !strcmp(ctx.env.request_method, "HEAD")))
 		ctx.cfg.nocache = 1;
 	if (ctx.cfg.nocache)
-- 
2.4.9



More information about the CGit mailing list