[nginx] Cache: update variant while setting header.

Valentin Bartenev vbart at nginx.com
Fri Dec 26 13:23:59 UTC 2014


details:   http://hg.nginx.org/nginx/rev/f7584d7c0ccb
branches:  
changeset: 5959:f7584d7c0ccb
user:      Valentin Bartenev <vbart at nginx.com>
date:      Fri Dec 26 16:22:56 2014 +0300
description:
Cache: update variant while setting header.

Some parts of code related to handling variants of a resource moved into
a separate function that is called earlier.  This allows to use cache file
name as a prefix for temporary file in the following patch.

diffstat:

 src/http/ngx_http_cache.h      |   2 +-
 src/http/ngx_http_file_cache.c |  94 +++++++++++++++++++++++++----------------
 src/http/ngx_http_upstream.c   |   5 +-
 3 files changed, 63 insertions(+), 38 deletions(-)

diffs (159 lines):

diff -r a9138c35120d -r f7584d7c0ccb src/http/ngx_http_cache.h
--- a/src/http/ngx_http_cache.h	Fri Dec 26 16:22:54 2014 +0300
+++ b/src/http/ngx_http_cache.h	Fri Dec 26 16:22:56 2014 +0300
@@ -162,7 +162,7 @@ ngx_int_t ngx_http_file_cache_new(ngx_ht
 ngx_int_t ngx_http_file_cache_create(ngx_http_request_t *r);
 void ngx_http_file_cache_create_key(ngx_http_request_t *r);
 ngx_int_t ngx_http_file_cache_open(ngx_http_request_t *r);
-void ngx_http_file_cache_set_header(ngx_http_request_t *r, u_char *buf);
+ngx_int_t ngx_http_file_cache_set_header(ngx_http_request_t *r, u_char *buf);
 void ngx_http_file_cache_update(ngx_http_request_t *r, ngx_temp_file_t *tf);
 void ngx_http_file_cache_update_header(ngx_http_request_t *r);
 ngx_int_t ngx_http_cache_send(ngx_http_request_t *);
diff -r a9138c35120d -r f7584d7c0ccb src/http/ngx_http_file_cache.c
--- a/src/http/ngx_http_file_cache.c	Fri Dec 26 16:22:54 2014 +0300
+++ b/src/http/ngx_http_file_cache.c	Fri Dec 26 16:22:56 2014 +0300
@@ -37,6 +37,8 @@ static void ngx_http_file_cache_vary_hea
     ngx_md5_t *md5, ngx_str_t *name);
 static ngx_int_t ngx_http_file_cache_reopen(ngx_http_request_t *r,
     ngx_http_cache_t *c);
+static ngx_int_t ngx_http_file_cache_update_variant(ngx_http_request_t *r,
+    ngx_http_cache_t *c);
 static void ngx_http_file_cache_cleanup(void *data);
 static time_t ngx_http_file_cache_forced_expire(ngx_http_file_cache_t *cache);
 static time_t ngx_http_file_cache_expire(ngx_http_file_cache_t *cache);
@@ -1122,7 +1124,7 @@ ngx_http_file_cache_reopen(ngx_http_requ
 }
 
 
-void
+ngx_int_t
 ngx_http_file_cache_set_header(ngx_http_request_t *r, u_char *buf)
 {
     ngx_http_file_cache_header_t  *h = (ngx_http_file_cache_header_t *) buf;
@@ -1164,9 +1166,10 @@ ngx_http_file_cache_set_header(ngx_http_
 
         ngx_http_file_cache_vary(r, c->vary.data, c->vary.len, c->variant);
         ngx_memcpy(h->variant, c->variant, NGX_HTTP_CACHE_KEY_LEN);
-
-    } else {
-        ngx_memzero(c->variant, NGX_HTTP_CACHE_KEY_LEN);
+    }
+
+    if (ngx_http_file_cache_update_variant(r, c) != NGX_OK) {
+        return NGX_ERROR;
     }
 
     p = buf + sizeof(ngx_http_file_cache_header_t);
@@ -1179,6 +1182,57 @@ ngx_http_file_cache_set_header(ngx_http_
     }
 
     *p = LF;
+
+    return NGX_OK;
+}
+
+
+static ngx_int_t
+ngx_http_file_cache_update_variant(ngx_http_request_t *r, ngx_http_cache_t *c)
+{
+    ngx_http_file_cache_t  *cache;
+
+    if (!c->secondary) {
+        return NGX_OK;
+    }
+
+    if (c->vary.len
+        && ngx_memcmp(c->variant, c->key, NGX_HTTP_CACHE_KEY_LEN) == 0)
+    {
+        return NGX_OK;
+    }
+
+    /*
+     * if the variant hash doesn't match one we used as a secondary
+     * cache key, switch back to the original key
+     */
+
+    cache = c->file_cache;
+
+    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                   "http file cache main key");
+
+    ngx_shmtx_lock(&cache->shpool->mutex);
+
+    c->node->count--;
+    c->node->updating = 0;
+    c->node = NULL;
+
+    ngx_shmtx_unlock(&cache->shpool->mutex);
+
+    c->file.name.len = 0;
+
+    ngx_memcpy(c->key, c->main, NGX_HTTP_CACHE_KEY_LEN);
+
+    if (ngx_http_file_cache_exists(cache, c) == NGX_ERROR) {
+        return NGX_ERROR;
+    }
+
+    if (ngx_http_file_cache_name(r, cache->path) != NGX_OK) {
+        return NGX_ERROR;
+    }
+
+    return NGX_OK;
 }
 
 
@@ -1204,38 +1258,6 @@ ngx_http_file_cache_update(ngx_http_requ
 
     cache = c->file_cache;
 
-    if (c->secondary
-        && ngx_memcmp(c->variant, c->key, NGX_HTTP_CACHE_KEY_LEN) != 0)
-    {
-        /*
-         * if the variant hash doesn't match one we used as a secondary
-         * cache key, switch back to the original key
-         */
-
-        ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
-                       "http file cache main key");
-
-        ngx_shmtx_lock(&cache->shpool->mutex);
-
-        c->node->count--;
-        c->node->updating = 0;
-        c->node = NULL;
-
-        ngx_shmtx_unlock(&cache->shpool->mutex);
-
-        c->file.name.len = 0;
-
-        ngx_memcpy(c->key, c->main, NGX_HTTP_CACHE_KEY_LEN);
-
-        if (ngx_http_file_cache_exists(cache, c) == NGX_ERROR) {
-            return;
-        }
-
-        if (ngx_http_file_cache_name(r, cache->path) != NGX_OK) {
-            return;
-        }
-    }
-
     c->updated = 1;
     c->updating = 0;
 
diff -r a9138c35120d -r f7584d7c0ccb src/http/ngx_http_upstream.c
--- a/src/http/ngx_http_upstream.c	Fri Dec 26 16:22:54 2014 +0300
+++ b/src/http/ngx_http_upstream.c	Fri Dec 26 16:22:56 2014 +0300
@@ -2628,7 +2628,10 @@ ngx_http_upstream_send_response(ngx_http
                 }
             }
 
-            ngx_http_file_cache_set_header(r, u->buffer.start);
+            if (ngx_http_file_cache_set_header(r, u->buffer.start) != NGX_OK) {
+                ngx_http_upstream_finalize_request(r, u, NGX_ERROR);
+                return;
+            }
 
         } else {
             u->cacheable = 0;



More information about the nginx-devel mailing list