From 4bf3e2693b841858f1d58b2df3192916f8f659a3 Mon Sep 17 00:00:00 2001 From: Witold Filipczyk Date: Thu, 26 Oct 2006 11:53:30 +0200 Subject: [PATCH] "If-Modified-Since" second approach. Added document.cache.interval option. When time elapsed since previous access to the document is less than interval then the document is taken from the cache. Otherwise the request with filled "If-Modified-Since" and/or "If-None-Match" header field is sent. By default interval is set to 10 minutes. This requires the correct time to be set on your machine. --- src/cache/cache.c | 16 +++++++++------- src/cache/cache.h | 2 ++ src/config/options.inc | 5 +++++ src/protocol/http/http.c | 19 +++++++++++++------ 4 files changed, 29 insertions(+), 13 deletions(-) diff --git a/src/cache/cache.c b/src/cache/cache.c index de0e576b..7dc67db0 100644 --- a/src/cache/cache.c +++ b/src/cache/cache.c @@ -21,6 +21,7 @@ #include "util/error.h" #include "util/memory.h" #include "util/string.h" +#include "util/time.h" /* The list of cache entries */ static INIT_LIST_HEAD(cache_entries); @@ -181,13 +182,6 @@ get_validated_cache_entry(struct uri *uri, enum cache_mode cache_mode) if (!cached || cached->incomplete) return NULL; -#if 0 - if (uri->protocol == PROTOCOL_HTTP || uri->protocol == PROTOCOL_HTTPS - || uri->protocol == PROTOCOL_FILE /* CGI */) { - if (cached->last_modified && cache_mode == CACHE_MODE_NORMAL) - return NULL; - } -#endif /* A bit of a gray zone. Delete the entry if the it has the stricktest * cache mode and we don't want the most aggressive mode or we have to @@ -200,6 +194,13 @@ get_validated_cache_entry(struct uri *uri, enum cache_mode cache_mode) return NULL; } + if (cached->cache_mode <= CACHE_MODE_CHECK_IF_MODIFIED + && cache_mode <= CACHE_MODE_CHECK_IF_MODIFIED + && (cached->last_modified || cached->etag)) { + if (cached->seconds + get_opt_int("document.cache.interval") < time(NULL)) + return NULL; + } + return cached; } @@ -685,6 +686,7 @@ normalize_cache_entry(struct cache_entry *cached, off_t truncate_length) truncate_entry(cached, truncate_length, 1); cached->incomplete = 0; cached->preformatted = 0; + cached->seconds = time(NULL); } diff --git a/src/cache/cache.h b/src/cache/cache.h index 584e07f0..78180bce 100644 --- a/src/cache/cache.h +++ b/src/cache/cache.h @@ -44,6 +44,8 @@ struct cache_entry { unsigned int id; /* Change each time entry is modified. */ + time_t seconds; /* Access time. Used by 'If-Modified-Since' */ + off_t length; /* The expected and complete size */ off_t data_size; /* The actual size of all fragments */ diff --git a/src/config/options.inc b/src/config/options.inc index b3b95474..fc5d5838 100644 --- a/src/config/options.inc +++ b/src/config/options.inc @@ -502,6 +502,11 @@ static struct option_info config_options_info[] = { "is always cached, even if it is over the memory cache size\n" "threshold. (Then of course no other documents can be cached.)")), +/* FIXME: Write more. */ + INIT_OPT_INT("document.cache", N_("Interval"), + "interval", 0, 0, 86400, 600, + N_("Interval between requests for the same document in seconds.")), + INIT_OPT_TREE("document.cache", N_("Memory cache"), "memory", 0, N_("Memory cache options.")), diff --git a/src/protocol/http/http.c b/src/protocol/http/http.c index 1502a209..da94c966 100644 --- a/src/protocol/http/http.c +++ b/src/protocol/http/http.c @@ -570,9 +570,9 @@ http_send_header(struct socket *socket) http_end_request(conn, S_OUT_OF_MEM, 0); return; } -#if 0 + if (!conn->cached) conn->cached = find_in_cache(uri); -#endif + talking_to_proxy = IS_PROXY_URI(conn->uri) && !conn->socket->ssl; use_connect = connection_is_https_proxy(conn) && !conn->socket->ssl; @@ -786,11 +786,18 @@ http_send_header(struct socket *socket) } if (conn->cached) { - if (!conn->cached->incomplete && conn->cached->head && conn->cached->last_modified + if (!conn->cached->incomplete && conn->cached->head && conn->cache_mode <= CACHE_MODE_CHECK_IF_MODIFIED) { - add_to_string(&header, "If-Modified-Since: "); - add_to_string(&header, conn->cached->last_modified); - add_crlf_to_string(&header); + if (conn->cached->last_modified) { + add_to_string(&header, "If-Modified-Since: "); + add_to_string(&header, conn->cached->last_modified); + add_crlf_to_string(&header); + } + if (conn->cached->etag) { + add_to_string(&header, "If-None-Match: "); + add_to_string(&header, conn->cached->etag); + add_crlf_to_string(&header); + } } }