[PATCH 1 of 2] Cache: don't update cache if revalidated response is not cacheable

Piotr Sikora piotr at cloudflare.com
Tue Nov 25 03:27:53 UTC 2014


# HG changeset patch
# User Piotr Sikora <piotr at cloudflare.com>
# Date 1416886025 28800
#      Mon Nov 24 19:27:05 2014 -0800
# Node ID 01f07fc7932b64f261c9e6cb778c87279fabcde2
# Parent  2c10db908b8c4a9c0532c58830275d5ad84ae686
Cache: don't update cache if revalidated response is not cacheable.

Signed-off-by: Piotr Sikora <piotr at cloudflare.com>

diff -r 2c10db908b8c -r 01f07fc7932b src/http/ngx_http_upstream.c
--- a/src/http/ngx_http_upstream.c	Fri Nov 21 22:51:49 2014 +0300
+++ b/src/http/ngx_http_upstream.c	Mon Nov 24 19:27:05 2014 -0800
@@ -2002,14 +2002,13 @@ ngx_http_upstream_test_next(ngx_http_req
         && u->cache_status == NGX_HTTP_CACHE_EXPIRED
         && u->conf->cache_revalidate)
     {
-        time_t     now, valid;
+        time_t     valid;
         ngx_int_t  rc;
 
         ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                        "http upstream not modified");
 
-        now = ngx_time();
-        valid = r->cache->valid_sec;
+        valid = u->cacheable ? r->cache->valid_sec : 0;
 
         rc = u->reinit_request(r);
 
@@ -2021,25 +2020,31 @@ ngx_http_upstream_test_next(ngx_http_req
         u->cache_status = NGX_HTTP_CACHE_REVALIDATED;
         rc = ngx_http_upstream_cache_send(r, u);
 
-        if (valid == 0) {
-            valid = r->cache->valid_sec;
-        }
-
-        if (valid == 0) {
-            valid = ngx_http_file_cache_valid(u->conf->cache_valid,
-                                              u->headers_in.status_n);
+        if (u->cacheable || valid) {
+            time_t  now;
+
+            now = ngx_time();
+
+            if (valid == 0) {
+                valid = r->cache->valid_sec;
+            }
+
+            if (valid == 0) {
+                valid = ngx_http_file_cache_valid(u->conf->cache_valid,
+                                                  u->headers_in.status_n);
+                if (valid) {
+                    valid = now + valid;
+                }
+            }
+
             if (valid) {
-                valid = now + valid;
+                r->cache->valid_sec = valid;
+                r->cache->date = now;
+
+                ngx_http_file_cache_update_header(r);
             }
         }
 
-        if (valid) {
-            r->cache->valid_sec = valid;
-            r->cache->date = now;
-
-            ngx_http_file_cache_update_header(r);
-        }
-
         ngx_http_upstream_finalize_request(r, u, rc);
         return NGX_OK;
     }



More information about the nginx-devel mailing list