[nginx] Cache: added temp_path to file cache.

Roman Arutyunyan arut at nginx.com
Mon Feb 2 16:40:40 UTC 2015


details:   http://hg.nginx.org/nginx/rev/99639bfdfa2a
branches:  
changeset: 5968:99639bfdfa2a
user:      Roman Arutyunyan <arut at nginx.com>
date:      Mon Feb 02 19:38:35 2015 +0300
description:
Cache: added temp_path to file cache.

If use_temp_path is set to off, a subdirectory "temp" is created in the cache
directory.  It's used instead of proxy_temp_path and friends for caching
upstream response.

diffstat:

 src/http/ngx_http_cache.h      |   4 +--
 src/http/ngx_http_file_cache.c |  62 ++++++++++++++++++++++++++++++++---------
 src/http/ngx_http_upstream.c   |   6 +--
 3 files changed, 51 insertions(+), 21 deletions(-)

diffs (151 lines):

diff -r 863d9de1e62b -r 99639bfdfa2a src/http/ngx_http_cache.h
--- a/src/http/ngx_http_cache.h	Mon Feb 02 19:38:32 2015 +0300
+++ b/src/http/ngx_http_cache.h	Mon Feb 02 19:38:35 2015 +0300
@@ -142,6 +142,7 @@ struct ngx_http_file_cache_s {
     ngx_slab_pool_t                 *shpool;
 
     ngx_path_t                      *path;
+    ngx_path_t                      *temp_path;
 
     off_t                            max_size;
     size_t                           bsize;
@@ -155,9 +156,6 @@ struct ngx_http_file_cache_s {
     ngx_msec_t                       loader_threshold;
 
     ngx_shm_zone_t                  *shm_zone;
-
-    ngx_uint_t                       use_temp_path;
-                                     /* unsigned use_temp_path:1 */
 };
 
 
diff -r 863d9de1e62b -r 99639bfdfa2a src/http/ngx_http_file_cache.c
--- a/src/http/ngx_http_file_cache.c	Mon Feb 02 19:38:32 2015 +0300
+++ b/src/http/ngx_http_file_cache.c	Mon Feb 02 19:38:35 2015 +0300
@@ -49,6 +49,8 @@ static ngx_int_t ngx_http_file_cache_noo
     ngx_str_t *path);
 static ngx_int_t ngx_http_file_cache_manage_file(ngx_tree_ctx_t *ctx,
     ngx_str_t *path);
+static ngx_int_t ngx_http_file_cache_manage_directory(ngx_tree_ctx_t *ctx,
+    ngx_str_t *path);
 static ngx_int_t ngx_http_file_cache_add_file(ngx_tree_ctx_t *ctx,
     ngx_str_t *path);
 static ngx_int_t ngx_http_file_cache_add(ngx_http_file_cache_t *cache,
@@ -1845,7 +1847,7 @@ ngx_http_file_cache_loader(void *data)
 
     tree.init_handler = NULL;
     tree.file_handler = ngx_http_file_cache_manage_file;
-    tree.pre_tree_handler = ngx_http_file_cache_noop;
+    tree.pre_tree_handler = ngx_http_file_cache_manage_directory;
     tree.post_tree_handler = ngx_http_file_cache_noop;
     tree.spec_handler = ngx_http_file_cache_delete_file;
     tree.data = cache;
@@ -1910,6 +1912,19 @@ ngx_http_file_cache_manage_file(ngx_tree
 }
 
 
+static ngx_int_t
+ngx_http_file_cache_manage_directory(ngx_tree_ctx_t *ctx, ngx_str_t *path)
+{
+    if (path->len >= 5
+        && ngx_strncmp(path->data + path->len - 5, "/temp", 5) == 0)
+    {
+        return NGX_DECLINED;
+    }
+
+    return NGX_OK;
+}
+
+
 static void
 ngx_http_file_cache_loader_sleep(ngx_http_file_cache_t *cache)
 {
@@ -1935,17 +1950,6 @@ ngx_http_file_cache_add_file(ngx_tree_ct
         return NGX_ERROR;
     }
 
-    /*
-     * Temporary files in cache have a suffix consisting of a dot
-     * followed by 10 digits.
-     */
-
-    if (name->len >= 2 * NGX_HTTP_CACHE_KEY_LEN + 1 + 10
-        && name->data[name->len - 10 - 1] == '.')
-    {
-        return NGX_OK;
-    }
-
     if (ctx->size < (off_t) sizeof(ngx_http_file_cache_header_t)) {
         ngx_log_error(NGX_LOG_CRIT, ctx->log, 0,
                       "cache file \"%s\" is too small", name->data);
@@ -2070,6 +2074,7 @@ ngx_http_file_cache_set_slot(ngx_conf_t 
     off_t                   max_size;
     u_char                 *last, *p;
     time_t                  inactive;
+    size_t                  len;
     ssize_t                 size;
     ngx_str_t               s, name, *value;
     ngx_int_t               loader_files;
@@ -2291,6 +2296,37 @@ ngx_http_file_cache_set_slot(ngx_conf_t 
         return NGX_CONF_ERROR;
     }
 
+    if (!use_temp_path) {
+        cache->temp_path = ngx_pcalloc(cf->pool, sizeof(ngx_path_t));
+        if (cache->temp_path == NULL) {
+            return NGX_CONF_ERROR;
+        }
+
+        len = cache->path->name.len + sizeof("/temp") - 1;
+
+        p = ngx_pnalloc(cf->pool, len + 1);
+        if (p == NULL) {
+            return NGX_CONF_ERROR;
+        }
+
+        cache->temp_path->name.len = len;
+        cache->temp_path->name.data = p;
+
+        p = ngx_cpymem(p, cache->path->name.data, cache->path->name.len);
+        ngx_memcpy(p, "/temp", sizeof("/temp"));
+
+        ngx_memcpy(&cache->temp_path->level, &cache->path->level,
+                   3 * sizeof(size_t));
+
+        cache->temp_path->len = cache->path->len;
+        cache->temp_path->conf_file = cf->conf_file->file.name.data;
+        cache->temp_path->line = cf->conf_file->line;
+
+        if (ngx_add_path(cf, &cache->temp_path) != NGX_OK) {
+            return NGX_CONF_ERROR;
+        }
+    }
+
     cache->shm_zone = ngx_shared_memory_add(cf, &name, size, cmd->post);
     if (cache->shm_zone == NULL) {
         return NGX_CONF_ERROR;
@@ -2306,8 +2342,6 @@ ngx_http_file_cache_set_slot(ngx_conf_t 
     cache->shm_zone->init = ngx_http_file_cache_init;
     cache->shm_zone->data = cache;
 
-    cache->use_temp_path = use_temp_path;
-
     cache->inactive = inactive;
     cache->max_size = max_size;
 
diff -r 863d9de1e62b -r 99639bfdfa2a src/http/ngx_http_upstream.c
--- a/src/http/ngx_http_upstream.c	Mon Feb 02 19:38:32 2015 +0300
+++ b/src/http/ngx_http_upstream.c	Mon Feb 02 19:38:35 2015 +0300
@@ -2688,10 +2688,8 @@ ngx_http_upstream_send_response(ngx_http
         p->temp_file->persistent = 1;
 
 #if (NGX_HTTP_CACHE)
-        if (r->cache && !r->cache->file_cache->use_temp_path) {
-            p->temp_file->file.name = r->cache->file.name;
-            p->temp_file->path = r->cache->file_cache->path;
-            p->temp_file->prefix = 1;
+        if (r->cache && r->cache->file_cache->temp_path) {
+            p->temp_file->path = r->cache->file_cache->temp_path;
         }
 #endif
 



More information about the nginx-devel mailing list