From 09b2b146e1ec44b2a7d1f407589e207bf2b176c4 Mon Sep 17 00:00:00 2001 From: Glenn Strauss Date: Sat, 23 Sep 2017 12:05:13 -0400 Subject: [PATCH] [core] make strftime_cache_get() 16-element cache Prior code was effectively a 1-element cache after the initial fill of the array since only the first element was replaced after the initial fill. New code does round-robin replacement. (whether or not #define FILE_CACHE_MAX 16 is appropriately sized here is a question for another day) --- src/http-header-glue.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/http-header-glue.c b/src/http-header-glue.c index edbabe00..6ec1eb96 100644 --- a/src/http-header-glue.c +++ b/src/http-header-glue.c @@ -128,23 +128,19 @@ int http_response_redirect_to_directory(server *srv, connection *con) { } buffer * strftime_cache_get(server *srv, time_t last_mod) { + static int i; struct tm *tm; - size_t i; - for (i = 0; i < FILE_CACHE_MAX; i++) { - /* found cache-entry */ - if (srv->mtime_cache[i].mtime == last_mod) return srv->mtime_cache[i].str; - - /* found empty slot */ - if (srv->mtime_cache[i].mtime == 0) break; + for (int j = 0; j < FILE_CACHE_MAX; ++j) { + if (srv->mtime_cache[j].mtime == last_mod) + return srv->mtime_cache[j].str; /* found cache-entry */ } - if (i == FILE_CACHE_MAX) { + if (++i == FILE_CACHE_MAX) { i = 0; } srv->mtime_cache[i].mtime = last_mod; - buffer_string_prepare_copy(srv->mtime_cache[i].str, 1023); tm = gmtime(&(srv->mtime_cache[i].mtime)); buffer_append_strftime(srv->mtime_cache[i].str, "%a, %d %b %Y %H:%M:%S GMT", tm);