[nginx] Cache: ETag now saved into cache header.

Maxim Dounin mdounin at mdounin.ru
Wed Jun 25 22:40:31 UTC 2014


details:   http://hg.nginx.org/nginx/rev/44b9ab7752e3
branches:  
changeset: 5737:44b9ab7752e3
user:      Maxim Dounin <mdounin at mdounin.ru>
date:      Thu Jun 26 02:28:23 2014 +0400
description:
Cache: ETag now saved into cache header.

diffstat:

 src/http/ngx_http_cache.h      |   7 ++++++-
 src/http/ngx_http_file_cache.c |  12 ++++++++++++
 src/http/ngx_http_upstream.c   |   4 ++++
 3 files changed, 22 insertions(+), 1 deletions(-)

diffs (82 lines):

diff --git a/src/http/ngx_http_cache.h b/src/http/ngx_http_cache.h
--- a/src/http/ngx_http_cache.h
+++ b/src/http/ngx_http_cache.h
@@ -24,8 +24,9 @@
 #define NGX_HTTP_CACHE_SCARCE        8
 
 #define NGX_HTTP_CACHE_KEY_LEN       16
+#define NGX_HTTP_CACHE_ETAG_LEN      42
 
-#define NGX_HTTP_CACHE_VERSION       1
+#define NGX_HTTP_CACHE_VERSION       2
 
 
 typedef struct {
@@ -69,6 +70,8 @@ struct ngx_http_cache_s {
     time_t                           last_modified;
     time_t                           date;
 
+    ngx_str_t                        etag;
+
     size_t                           header_start;
     size_t                           body_start;
     off_t                            length;
@@ -107,6 +110,8 @@ typedef struct {
     u_short                          valid_msec;
     u_short                          header_start;
     u_short                          body_start;
+    u_char                           etag_len;
+    u_char                           etag[NGX_HTTP_CACHE_ETAG_LEN];
 } ngx_http_file_cache_header_t;
 
 
diff --git a/src/http/ngx_http_file_cache.c b/src/http/ngx_http_file_cache.c
--- a/src/http/ngx_http_file_cache.c
+++ b/src/http/ngx_http_file_cache.c
@@ -525,6 +525,8 @@ ngx_http_file_cache_read(ngx_http_reques
     c->valid_msec = h->valid_msec;
     c->header_start = h->header_start;
     c->body_start = h->body_start;
+    c->etag.len = h->etag_len;
+    c->etag.data = h->etag;
 
     r->cached = 1;
 
@@ -890,6 +892,11 @@ ngx_http_file_cache_set_header(ngx_http_
     h->header_start = (u_short) c->header_start;
     h->body_start = (u_short) c->body_start;
 
+    if (c->etag.len <= NGX_HTTP_CACHE_ETAG_LEN) {
+        h->etag_len = (u_char) c->etag.len;
+        ngx_memcpy(h->etag, c->etag.data, c->etag.len);
+    }
+
     p = buf + sizeof(ngx_http_file_cache_header_t);
 
     p = ngx_cpymem(p, ngx_http_file_cache_key, sizeof(ngx_http_file_cache_key));
@@ -1077,6 +1084,11 @@ ngx_http_file_cache_update_header(ngx_ht
     h.header_start = (u_short) c->header_start;
     h.body_start = (u_short) c->body_start;
 
+    if (c->etag.len <= NGX_HTTP_CACHE_ETAG_LEN) {
+        h.etag_len = (u_char) c->etag.len;
+        ngx_memcpy(h.etag, c->etag.data, c->etag.len);
+    }
+
     (void) ngx_write_file(&file, (u_char *) &h,
                           sizeof(ngx_http_file_cache_header_t), 0);
 
diff --git a/src/http/ngx_http_upstream.c b/src/http/ngx_http_upstream.c
--- a/src/http/ngx_http_upstream.c
+++ b/src/http/ngx_http_upstream.c
@@ -2498,6 +2498,10 @@ ngx_http_upstream_send_response(ngx_http
             r->cache->date = now;
             r->cache->body_start = (u_short) (u->buffer.pos - u->buffer.start);
 
+            if (u->headers_in.etag) {
+                r->cache->etag = u->headers_in.etag->value;
+            }
+
             ngx_http_file_cache_set_header(r, u->buffer.start);
 
         } else {



More information about the nginx-devel mailing list