comparison src/http/ngx_http_file_cache.c @ 5968:99639bfdfa2a

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.
author Roman Arutyunyan <arut@nginx.com>
date Mon, 02 Feb 2015 19:38:35 +0300
parents e9effef98874
children 3f568dd68af1
comparison
equal deleted inserted replaced
5967:863d9de1e62b 5968:99639bfdfa2a
47 static void ngx_http_file_cache_loader_sleep(ngx_http_file_cache_t *cache); 47 static void ngx_http_file_cache_loader_sleep(ngx_http_file_cache_t *cache);
48 static ngx_int_t ngx_http_file_cache_noop(ngx_tree_ctx_t *ctx, 48 static ngx_int_t ngx_http_file_cache_noop(ngx_tree_ctx_t *ctx,
49 ngx_str_t *path); 49 ngx_str_t *path);
50 static ngx_int_t ngx_http_file_cache_manage_file(ngx_tree_ctx_t *ctx, 50 static ngx_int_t ngx_http_file_cache_manage_file(ngx_tree_ctx_t *ctx,
51 ngx_str_t *path); 51 ngx_str_t *path);
52 static ngx_int_t ngx_http_file_cache_manage_directory(ngx_tree_ctx_t *ctx,
53 ngx_str_t *path);
52 static ngx_int_t ngx_http_file_cache_add_file(ngx_tree_ctx_t *ctx, 54 static ngx_int_t ngx_http_file_cache_add_file(ngx_tree_ctx_t *ctx,
53 ngx_str_t *path); 55 ngx_str_t *path);
54 static ngx_int_t ngx_http_file_cache_add(ngx_http_file_cache_t *cache, 56 static ngx_int_t ngx_http_file_cache_add(ngx_http_file_cache_t *cache,
55 ngx_http_cache_t *c); 57 ngx_http_cache_t *c);
56 static ngx_int_t ngx_http_file_cache_delete_file(ngx_tree_ctx_t *ctx, 58 static ngx_int_t ngx_http_file_cache_delete_file(ngx_tree_ctx_t *ctx,
1843 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0, 1845 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
1844 "http file cache loader"); 1846 "http file cache loader");
1845 1847
1846 tree.init_handler = NULL; 1848 tree.init_handler = NULL;
1847 tree.file_handler = ngx_http_file_cache_manage_file; 1849 tree.file_handler = ngx_http_file_cache_manage_file;
1848 tree.pre_tree_handler = ngx_http_file_cache_noop; 1850 tree.pre_tree_handler = ngx_http_file_cache_manage_directory;
1849 tree.post_tree_handler = ngx_http_file_cache_noop; 1851 tree.post_tree_handler = ngx_http_file_cache_noop;
1850 tree.spec_handler = ngx_http_file_cache_delete_file; 1852 tree.spec_handler = ngx_http_file_cache_delete_file;
1851 tree.data = cache; 1853 tree.data = cache;
1852 tree.alloc = 0; 1854 tree.alloc = 0;
1853 tree.log = ngx_cycle->log; 1855 tree.log = ngx_cycle->log;
1908 1910
1909 return (ngx_quit || ngx_terminate) ? NGX_ABORT : NGX_OK; 1911 return (ngx_quit || ngx_terminate) ? NGX_ABORT : NGX_OK;
1910 } 1912 }
1911 1913
1912 1914
1915 static ngx_int_t
1916 ngx_http_file_cache_manage_directory(ngx_tree_ctx_t *ctx, ngx_str_t *path)
1917 {
1918 if (path->len >= 5
1919 && ngx_strncmp(path->data + path->len - 5, "/temp", 5) == 0)
1920 {
1921 return NGX_DECLINED;
1922 }
1923
1924 return NGX_OK;
1925 }
1926
1927
1913 static void 1928 static void
1914 ngx_http_file_cache_loader_sleep(ngx_http_file_cache_t *cache) 1929 ngx_http_file_cache_loader_sleep(ngx_http_file_cache_t *cache)
1915 { 1930 {
1916 ngx_msleep(cache->loader_sleep); 1931 ngx_msleep(cache->loader_sleep);
1917 1932
1933 1948
1934 if (name->len < 2 * NGX_HTTP_CACHE_KEY_LEN) { 1949 if (name->len < 2 * NGX_HTTP_CACHE_KEY_LEN) {
1935 return NGX_ERROR; 1950 return NGX_ERROR;
1936 } 1951 }
1937 1952
1938 /*
1939 * Temporary files in cache have a suffix consisting of a dot
1940 * followed by 10 digits.
1941 */
1942
1943 if (name->len >= 2 * NGX_HTTP_CACHE_KEY_LEN + 1 + 10
1944 && name->data[name->len - 10 - 1] == '.')
1945 {
1946 return NGX_OK;
1947 }
1948
1949 if (ctx->size < (off_t) sizeof(ngx_http_file_cache_header_t)) { 1953 if (ctx->size < (off_t) sizeof(ngx_http_file_cache_header_t)) {
1950 ngx_log_error(NGX_LOG_CRIT, ctx->log, 0, 1954 ngx_log_error(NGX_LOG_CRIT, ctx->log, 0,
1951 "cache file \"%s\" is too small", name->data); 1955 "cache file \"%s\" is too small", name->data);
1952 return NGX_ERROR; 1956 return NGX_ERROR;
1953 } 1957 }
2068 char *confp = conf; 2072 char *confp = conf;
2069 2073
2070 off_t max_size; 2074 off_t max_size;
2071 u_char *last, *p; 2075 u_char *last, *p;
2072 time_t inactive; 2076 time_t inactive;
2077 size_t len;
2073 ssize_t size; 2078 ssize_t size;
2074 ngx_str_t s, name, *value; 2079 ngx_str_t s, name, *value;
2075 ngx_int_t loader_files; 2080 ngx_int_t loader_files;
2076 ngx_msec_t loader_sleep, loader_threshold; 2081 ngx_msec_t loader_sleep, loader_threshold;
2077 ngx_uint_t i, n, use_temp_path; 2082 ngx_uint_t i, n, use_temp_path;
2289 2294
2290 if (ngx_add_path(cf, &cache->path) != NGX_OK) { 2295 if (ngx_add_path(cf, &cache->path) != NGX_OK) {
2291 return NGX_CONF_ERROR; 2296 return NGX_CONF_ERROR;
2292 } 2297 }
2293 2298
2299 if (!use_temp_path) {
2300 cache->temp_path = ngx_pcalloc(cf->pool, sizeof(ngx_path_t));
2301 if (cache->temp_path == NULL) {
2302 return NGX_CONF_ERROR;
2303 }
2304
2305 len = cache->path->name.len + sizeof("/temp") - 1;
2306
2307 p = ngx_pnalloc(cf->pool, len + 1);
2308 if (p == NULL) {
2309 return NGX_CONF_ERROR;
2310 }
2311
2312 cache->temp_path->name.len = len;
2313 cache->temp_path->name.data = p;
2314
2315 p = ngx_cpymem(p, cache->path->name.data, cache->path->name.len);
2316 ngx_memcpy(p, "/temp", sizeof("/temp"));
2317
2318 ngx_memcpy(&cache->temp_path->level, &cache->path->level,
2319 3 * sizeof(size_t));
2320
2321 cache->temp_path->len = cache->path->len;
2322 cache->temp_path->conf_file = cf->conf_file->file.name.data;
2323 cache->temp_path->line = cf->conf_file->line;
2324
2325 if (ngx_add_path(cf, &cache->temp_path) != NGX_OK) {
2326 return NGX_CONF_ERROR;
2327 }
2328 }
2329
2294 cache->shm_zone = ngx_shared_memory_add(cf, &name, size, cmd->post); 2330 cache->shm_zone = ngx_shared_memory_add(cf, &name, size, cmd->post);
2295 if (cache->shm_zone == NULL) { 2331 if (cache->shm_zone == NULL) {
2296 return NGX_CONF_ERROR; 2332 return NGX_CONF_ERROR;
2297 } 2333 }
2298 2334
2303 } 2339 }
2304 2340
2305 2341
2306 cache->shm_zone->init = ngx_http_file_cache_init; 2342 cache->shm_zone->init = ngx_http_file_cache_init;
2307 cache->shm_zone->data = cache; 2343 cache->shm_zone->data = cache;
2308
2309 cache->use_temp_path = use_temp_path;
2310 2344
2311 cache->inactive = inactive; 2345 cache->inactive = inactive;
2312 cache->max_size = max_size; 2346 cache->max_size = max_size;
2313 2347
2314 caches = (ngx_array_t *) (confp + cmd->offset); 2348 caches = (ngx_array_t *) (confp + cmd->offset);