nginx 0.7.45
[nginx-catap.git] / src / http / ngx_http_file_cache.c
blobefe0da6b09cb102b047335c8c9ede4fa276b1b0a
2 /*
3 * Copyright (C) Igor Sysoev
4 */
7 #include <ngx_config.h>
8 #include <ngx_core.h>
9 #include <ngx_http.h>
10 #include <ngx_md5.h>
13 static ngx_int_t ngx_http_file_cache_exists(ngx_http_request_t *r,
14 ngx_http_file_cache_t *cache);
15 static ngx_http_file_cache_node_t *
16 ngx_http_file_cache_lookup(ngx_http_file_cache_t *cache, u_char *key);
17 static void ngx_http_file_cache_rbtree_insert_value(ngx_rbtree_node_t *temp,
18 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel);
19 static void ngx_http_file_cache_cleanup(void *data);
20 static time_t ngx_http_file_cache_expire(ngx_http_file_cache_t *cache,
21 ngx_uint_t force);
22 static ngx_int_t ngx_http_file_cache_clean_noop(ngx_tree_ctx_t *ctx,
23 ngx_str_t *path);
24 static ngx_int_t ngx_http_file_cache_clean_file(ngx_tree_ctx_t *ctx,
25 ngx_str_t *path);
28 static u_char ngx_http_file_cache_key[] = { LF, 'K', 'E', 'Y', ':', ' ' };
31 static ngx_int_t
32 ngx_http_file_cache_init(ngx_shm_zone_t *shm_zone, void *data)
34 ngx_http_file_cache_t *ocache = data;
36 ngx_rbtree_node_t *sentinel;
37 ngx_http_file_cache_t *cache;
39 cache = shm_zone->data;
41 if (ocache) {
42 if (ngx_strcmp(cache->path->name.data, ocache->path->name.data) != 0) {
43 ngx_log_error(NGX_LOG_EMERG, shm_zone->shm.log, 0,
44 "cache \"%V\" uses the \"%V\" cache path "
45 "while previously it used the \"%V\" cache path",
46 &shm_zone->name, &cache->path->name,
47 &ocache->path->name);
49 return NGX_ERROR;
52 cache->rbtree = ocache->rbtree;
53 cache->queue = ocache->queue;
54 cache->shpool = ocache->shpool;
55 cache->created = ocache->created;
57 return NGX_OK;
60 cache->shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
62 cache->rbtree = ngx_slab_alloc(cache->shpool, sizeof(ngx_rbtree_t));
63 if (cache->rbtree == NULL) {
64 return NGX_ERROR;
67 sentinel = ngx_slab_alloc(cache->shpool, sizeof(ngx_rbtree_node_t));
68 if (sentinel == NULL) {
69 return NGX_ERROR;
72 ngx_rbtree_init(cache->rbtree, sentinel,
73 ngx_http_file_cache_rbtree_insert_value);
75 cache->queue = ngx_slab_alloc(cache->shpool, sizeof(ngx_queue_t));
76 if (cache->queue == NULL) {
77 return NGX_ERROR;
80 ngx_queue_init(cache->queue);
82 cache->created = ngx_time();
84 return NGX_OK;
88 void
89 ngx_http_file_cache_create_key(ngx_http_request_t *r)
91 size_t len;
92 ngx_str_t *key;
93 ngx_uint_t i;
94 ngx_md5_t md5;
95 ngx_http_cache_t *c;
97 c = r->cache;
99 len = 0;
101 ngx_crc32_init(c->crc32);
102 ngx_md5_init(&md5);
104 key = c->keys.elts;
105 for (i = 0; i < c->keys.nelts; i++) {
106 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
107 "http cache key: \"%V\"", &key[i]);
109 len += key[i].len;
111 ngx_crc32_update(&c->crc32, key[i].data, key[i].len);
112 ngx_md5_update(&md5, key[i].data, key[i].len);
115 c->header_start = sizeof(ngx_http_file_cache_header_t)
116 + sizeof(ngx_http_file_cache_key) + len + 1;
118 ngx_crc32_final(c->crc32);
119 ngx_md5_final(c->key, &md5);
123 ngx_int_t
124 ngx_http_file_cache_open(ngx_http_request_t *r)
126 u_char *p;
127 time_t now;
128 ssize_t n;
129 ngx_int_t rc, rv;
130 ngx_uint_t cold, test;
131 ngx_path_t *path;
132 ngx_http_cache_t *c;
133 ngx_pool_cleanup_t *cln;
134 ngx_open_file_info_t of;
135 ngx_http_file_cache_t *cache;
136 ngx_http_core_loc_conf_t *clcf;
137 ngx_http_file_cache_header_t *h;
139 c = r->cache;
140 cache = c->file_cache;
142 cln = ngx_pool_cleanup_add(r->pool, 0);
143 if (cln == NULL) {
144 return NGX_ERROR;
147 rc = ngx_http_file_cache_exists(r, cache);
149 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
150 "http file cache exists: %i u:%ui e:%d",
151 rc, c->uses, c->exists);
153 if (rc == NGX_ERROR) {
154 return rc;
157 cln->handler = ngx_http_file_cache_cleanup;
158 cln->data = c;
160 if (rc == NGX_AGAIN) {
161 return rc;
164 now = ngx_time();
166 cold = (now - cache->created < cache->inactive) ? 1 : 0;
168 if (rc == NGX_OK) {
170 if (c->error) {
171 return c->error;
174 c->temp_file = 1;
175 test = c->exists ? 1 : 0;
176 rv = NGX_DECLINED;
178 } else { /* rc == NGX_DECLINED */
180 if (c->min_uses > 1) {
182 if (!cold) {
183 return NGX_AGAIN;
186 test = 1;
187 rv = NGX_AGAIN;
189 } else {
190 c->temp_file = 1;
191 test = cold ? 1 : 0;
192 rv = NGX_DECLINED;
196 path = cache->path;
198 c->file.name.len = path->name.len + 1 + path->len
199 + 2 * NGX_HTTP_CACHE_KEY_LEN;
201 c->file.name.data = ngx_pnalloc(r->pool, c->file.name.len + 1);
202 if (c->file.name.data == NULL) {
203 return NGX_ERROR;
206 ngx_memcpy(c->file.name.data, path->name.data, path->name.len);
208 p = c->file.name.data + path->name.len + 1 + path->len;
209 p = ngx_hex_dump(p, c->key, NGX_HTTP_CACHE_KEY_LEN);
210 *p = '\0';
212 ngx_create_hashed_filename(path, c->file.name.data, c->file.name.len);
214 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
215 "cache file: \"%s\"", c->file.name.data);
217 if (!test) {
218 return NGX_DECLINED;
221 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
223 ngx_memzero(&of, sizeof(ngx_open_file_info_t));
225 of.uniq = c->uniq;
226 of.valid = clcf->open_file_cache_valid;
227 of.min_uses = clcf->open_file_cache_min_uses;
228 of.events = clcf->open_file_cache_events;
229 of.directio = NGX_OPEN_FILE_DIRECTIO_OFF;
231 if (ngx_open_cached_file(clcf->open_file_cache, &c->file.name, &of, r->pool)
232 != NGX_OK)
234 switch (of.err) {
236 case 0:
237 return NGX_ERROR;
239 case NGX_ENOENT:
240 case NGX_ENOTDIR:
241 return rv;
243 default:
244 ngx_log_error(NGX_LOG_CRIT, r->connection->log, of.err,
245 ngx_open_file_n " \"%s\" failed", c->file.name.data);
246 return NGX_ERROR;
250 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
251 "http file cache fd: %d", of.fd);
253 c->file.fd = of.fd;
254 c->file.log = r->connection->log;
256 c->buf = ngx_create_temp_buf(r->pool, c->body_start);
257 if (c->buf == NULL) {
258 return NGX_ERROR;
261 n = ngx_read_file(&c->file, c->buf->pos, c->body_start, 0);
263 if (n == NGX_ERROR) {
264 return n;
267 if ((size_t) n <= c->header_start) {
268 ngx_log_error(NGX_LOG_CRIT, r->connection->log, 0,
269 "cache file \"%s\" is too small", c->file.name.data);
270 return NGX_ERROR;
273 h = (ngx_http_file_cache_header_t *) c->buf->pos;
275 if (h->crc32 != c->crc32 || (size_t) h->header_start != c->header_start) {
276 ngx_log_error(NGX_LOG_CRIT, r->connection->log, 0,
277 "cache file \"%s\" has md5 collision", c->file.name.data);
278 return NGX_DECLINED;
281 c->buf->last += n;
283 c->valid_sec = h->valid_sec;
284 c->last_modified = h->last_modified;
285 c->date = h->date;
286 c->valid_msec = h->valid_msec;
287 c->length = of.size;
288 c->body_start = h->body_start;
290 r->cached = 1;
292 if (cold) {
294 ngx_shmtx_lock(&cache->shpool->mutex);
296 c->node->uses = c->min_uses;
297 c->node->body_start = c->body_start;
298 c->node->exists = 1;
300 ngx_shmtx_unlock(&cache->shpool->mutex);
303 if (c->valid_sec < now) {
305 c->uses = c->min_uses;
307 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
308 "http file cache expired: %T %T", c->valid_sec, now);
310 return NGX_HTTP_CACHE_STALE;
313 /* TODO: NGX_HTTP_CACHE_AGED */
315 return NGX_OK;
319 static ngx_int_t
320 ngx_http_file_cache_exists(ngx_http_request_t *r, ngx_http_file_cache_t *cache)
322 ngx_int_t rc;
323 ngx_http_file_cache_node_t *fcn;
325 ngx_shmtx_lock(&cache->shpool->mutex);
327 fcn = ngx_http_file_cache_lookup(cache, r->cache->key);
329 if (fcn) {
330 ngx_queue_remove(&fcn->queue);
332 if (fcn->error) {
334 if (fcn->valid_sec < ngx_time()) {
335 goto renew;
338 rc = NGX_OK;
340 goto done;
343 fcn->uses++;
344 fcn->count++;
346 if (fcn->exists) {
348 r->cache->exists = fcn->exists;
349 r->cache->body_start = fcn->body_start;
351 rc = NGX_OK;
353 goto done;
356 if (fcn->uses >= r->cache->min_uses) {
358 r->cache->exists = fcn->exists;
359 r->cache->body_start = fcn->body_start;
361 rc = NGX_OK;
363 } else {
364 rc = NGX_AGAIN;
367 goto done;
370 fcn = ngx_slab_alloc_locked(cache->shpool,
371 sizeof(ngx_http_file_cache_node_t));
372 if (fcn == NULL) {
373 ngx_shmtx_unlock(&cache->shpool->mutex);
375 (void) ngx_http_file_cache_expire(cache, 1);
377 ngx_shmtx_lock(&cache->shpool->mutex);
379 fcn = ngx_slab_alloc_locked(cache->shpool,
380 sizeof(ngx_http_file_cache_node_t));
381 if (fcn == NULL) {
382 rc = NGX_ERROR;
383 goto failed;
387 ngx_memcpy((u_char *) &fcn->node.key, r->cache->key,
388 sizeof(ngx_rbtree_key_t));
390 ngx_memcpy(fcn->key, &r->cache->key[sizeof(ngx_rbtree_key_t)],
391 NGX_HTTP_CACHE_KEY_LEN - sizeof(ngx_rbtree_key_t));
393 ngx_rbtree_insert(cache->rbtree, &fcn->node);
395 renew:
397 rc = NGX_DECLINED;
399 fcn->uses = 1;
400 fcn->count = 1;
401 fcn->valid_msec = 0;
402 fcn->error = 0;
403 fcn->exists = 0;
404 fcn->valid_sec = 0;
405 fcn->uniq = 0;
406 fcn->body_start = 0;
408 done:
410 fcn->expire = ngx_time() + cache->inactive;
412 ngx_queue_insert_head(cache->queue, &fcn->queue);
414 r->cache->uniq = fcn->uniq;
415 r->cache->uses = fcn->uses;
416 r->cache->error = fcn->error;
417 r->cache->node = fcn;
419 failed:
421 ngx_shmtx_unlock(&cache->shpool->mutex);
423 return rc;
427 static ngx_http_file_cache_node_t *
428 ngx_http_file_cache_lookup(ngx_http_file_cache_t *cache, u_char *key)
430 ngx_int_t rc;
431 ngx_rbtree_key_t node_key;
432 ngx_rbtree_node_t *node, *sentinel;
433 ngx_http_file_cache_node_t *fcn;
435 ngx_memcpy((u_char *) &node_key, key, sizeof(ngx_rbtree_key_t));
437 node = cache->rbtree->root;
438 sentinel = cache->rbtree->sentinel;
440 while (node != sentinel) {
442 if (node_key < node->key) {
443 node = node->left;
444 continue;
447 if (node_key > node->key) {
448 node = node->right;
449 continue;
452 /* node_key == node->key */
454 do {
455 fcn = (ngx_http_file_cache_node_t *) node;
457 rc = ngx_memcmp(&key[sizeof(ngx_rbtree_key_t)], fcn->key,
458 NGX_HTTP_CACHE_KEY_LEN - sizeof(ngx_rbtree_key_t));
460 if (rc == 0) {
461 return fcn;
464 node = (rc < 0) ? node->left : node->right;
466 } while (node != sentinel && node_key == node->key);
468 break;
471 /* not found */
473 return NULL;
477 static void
478 ngx_http_file_cache_rbtree_insert_value(ngx_rbtree_node_t *temp,
479 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel)
481 ngx_rbtree_node_t **p;
482 ngx_http_file_cache_node_t *cn, *cnt;
484 for ( ;; ) {
486 if (node->key < temp->key) {
488 p = &temp->left;
490 } else if (node->key > temp->key) {
492 p = &temp->right;
494 } else { /* node->key == temp->key */
496 cn = (ngx_http_file_cache_node_t *) node;
497 cnt = (ngx_http_file_cache_node_t *) temp;
499 p = (ngx_memcmp(cn->key, cnt->key,
500 NGX_HTTP_CACHE_KEY_LEN - sizeof(ngx_rbtree_key_t))
501 < 0)
502 ? &temp->left : &temp->right;
505 if (*p == sentinel) {
506 break;
509 temp = *p;
512 *p = node;
513 node->parent = temp;
514 node->left = sentinel;
515 node->right = sentinel;
516 ngx_rbt_red(node);
520 void
521 ngx_http_file_cache_set_header(ngx_http_request_t *r, u_char *buf)
523 ngx_http_file_cache_header_t *h = (ngx_http_file_cache_header_t *) buf;
525 u_char *p;
526 ngx_str_t *key;
527 ngx_uint_t i;
528 ngx_http_cache_t *c;
530 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
531 "http file cache set header");
533 c = r->cache;
535 h->valid_sec = c->valid_sec;
536 h->last_modified = c->last_modified;
537 h->date = c->date;
538 h->crc32 = c->crc32;
539 h->valid_msec = (u_short) c->valid_msec;
540 h->header_start = (u_short) c->header_start;
541 h->body_start = (u_short) c->body_start;
543 p = buf + sizeof(ngx_http_file_cache_header_t);
545 p = ngx_cpymem(p, ngx_http_file_cache_key, sizeof(ngx_http_file_cache_key));
547 key = c->keys.elts;
548 for (i = 0; i < c->keys.nelts; i++) {
549 p = ngx_copy(p, key[i].data, key[i].len);
552 *p = LF;
556 void
557 ngx_http_file_cache_update(ngx_http_request_t *r, ngx_temp_file_t *tf)
559 ngx_int_t rc;
560 ngx_file_uniq_t uniq;
561 ngx_file_info_t fi;
562 ngx_http_cache_t *c;
563 ngx_ext_rename_file_t ext;
564 ngx_http_file_cache_t *cache;
566 c = r->cache;
568 if (c->updated) {
569 return;
572 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
573 "http file cache update");
575 c->updated = 1;
577 cache = c->file_cache;
579 uniq = 0;
581 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
582 "http file cache rename: \"%s\" to \"%s\"",
583 tf->file.name.data, c->file.name.data);
585 ext.access = NGX_FILE_OWNER_ACCESS;
586 ext.path_access = NGX_FILE_OWNER_ACCESS;
587 ext.time = -1;
588 ext.create_path = 1;
589 ext.delete_file = 1;
590 ext.log_rename_error = 1;
591 ext.log = r->connection->log;
593 rc = ngx_ext_rename_file(&tf->file.name, &c->file.name, &ext);
595 if (rc == NGX_OK) {
597 if (ngx_fd_info(tf->file.fd, &fi) == NGX_FILE_ERROR) {
598 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
599 ngx_fd_info_n " \"%s\" failed", tf->file.name.data);
601 rc = NGX_ERROR;
603 } else {
604 uniq = ngx_file_uniq(&fi);
608 ngx_shmtx_lock(&cache->shpool->mutex);
610 c->node->count--;
611 c->node->uniq = uniq;
612 c->node->body_start = c->body_start;
614 if (rc == NGX_OK) {
615 c->node->exists = 1;
618 ngx_shmtx_unlock(&cache->shpool->mutex);
622 ngx_int_t
623 ngx_http_cache_send(ngx_http_request_t *r)
625 ngx_int_t rc;
626 ngx_buf_t *b;
627 ngx_chain_t out;
628 ngx_http_cache_t *c;
630 c = r->cache;
632 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
633 "http file cache send: %s", c->file.name.data);
635 /* we need to allocate all before the header would be sent */
637 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
638 if (b == NULL) {
639 return NGX_HTTP_INTERNAL_SERVER_ERROR;
642 b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t));
643 if (b->file == NULL) {
644 return NGX_HTTP_INTERNAL_SERVER_ERROR;
647 rc = ngx_http_send_header(r);
649 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
650 return rc;
653 b->file_pos = c->body_start;
654 b->file_last = c->length;
656 b->in_file = (c->length - c->body_start) ? 1: 0;
657 b->last_buf = (r == r->main) ? 1: 0;
658 b->last_in_chain = 1;
660 b->file->fd = c->file.fd;
661 b->file->name = c->file.name;
662 b->file->log = r->connection->log;
664 out.buf = b;
665 out.next = NULL;
667 return ngx_http_output_filter(r, &out);
671 void
672 ngx_http_file_cache_free(ngx_http_request_t *r, ngx_temp_file_t *tf)
674 ngx_http_cache_t *c;
675 ngx_http_file_cache_t *cache;
677 c = r->cache;
679 if (c->updated) {
680 return;
683 c->updated = 1;
685 cache = c->file_cache;
687 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
688 "http file cache free");
690 ngx_shmtx_lock(&cache->shpool->mutex);
692 c->node->count--;
694 if (c->error) {
695 c->node->valid_sec = c->valid_sec;
696 c->node->valid_msec = c->valid_msec;
697 c->node->error = c->error;
700 ngx_shmtx_unlock(&cache->shpool->mutex);
702 if (c->temp_file) {
703 if (tf && tf->file.fd != NGX_INVALID_FILE) {
704 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
705 "http file cache incomplete: \"%s\"",
706 tf->file.name.data);
708 if (ngx_delete_file(tf->file.name.data) == NGX_FILE_ERROR) {
709 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
710 ngx_delete_file_n " \"%s\" failed",
711 tf->file.name.data);
718 static void
719 ngx_http_file_cache_cleanup(void *data)
721 ngx_http_cache_t *c = data;
723 ngx_http_file_cache_t *cache;
725 if (c->updated) {
726 return;
729 c->updated = 1;
731 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->file.log, 0,
732 "http file cache cleanup");
734 if (c->error) {
735 return;
738 cache = c->file_cache;
740 ngx_shmtx_lock(&cache->shpool->mutex);
742 c->node->count--;
744 ngx_shmtx_unlock(&cache->shpool->mutex);
748 static time_t
749 ngx_http_file_cache_expire(ngx_http_file_cache_t *cache, ngx_uint_t forced)
751 u_char *name, *p;
752 size_t len;
753 time_t now, wait;
754 ngx_uint_t tries;
755 ngx_path_t *path;
756 ngx_queue_t *q;
757 ngx_http_file_cache_node_t *fcn;
758 u_char key[2 * NGX_HTTP_CACHE_KEY_LEN];
760 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
761 "http file cache expire");
763 path = cache->path;
764 len = path->name.len + 1 + path->len + 2 * NGX_HTTP_CACHE_KEY_LEN;
766 now = ngx_time();
768 ngx_shmtx_lock(&cache->shpool->mutex);
770 tries = 0;
772 for ( ;; ) {
774 if (ngx_queue_empty(cache->queue)) {
775 wait = cache->inactive;
776 break;
779 q = ngx_queue_last(cache->queue);
781 fcn = ngx_queue_data(q, ngx_http_file_cache_node_t, queue);
783 if (!forced) {
784 wait = fcn->expire - now;
786 if (wait > 0) {
787 break;
791 ngx_log_debug6(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
792 "http file cache expire: #%d %d %02xd%02xd%02xd%02xd",
793 fcn->count, fcn->exists,
794 fcn->key[0], fcn->key[1], fcn->key[2], fcn->key[3]);
796 if (fcn->count) {
798 if (!forced) {
800 p = ngx_hex_dump(key, (u_char *) &fcn->node.key,
801 sizeof(ngx_rbtree_key_t));
802 (void) ngx_hex_dump(p, fcn->key,
803 NGX_HTTP_CACHE_KEY_LEN - sizeof(ngx_rbtree_key_t));
806 * abnormally exited workers may leave locked cache entries,
807 * and although it may be safe to remove them completely,
808 * we prefer to remove them from inactive queue and rbtree
809 * only, and to allow other leaks
812 ngx_queue_remove(q);
814 ngx_rbtree_delete(cache->rbtree, &fcn->node);
816 ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, 0,
817 "ignore long locked inactive cache entry %*s, count:%d",
818 2 * NGX_HTTP_CACHE_KEY_LEN, key, fcn->count);
821 if (tries++ < 10) {
822 continue;
825 wait = 1;
826 break;
829 forced = 0;
831 if (!fcn->exists) {
833 ngx_queue_remove(q);
835 ngx_rbtree_delete(cache->rbtree, &fcn->node);
837 ngx_slab_free_locked(cache->shpool, fcn);
839 continue;
842 name = ngx_alloc(len + 1, ngx_cycle->log);
843 if (name == NULL) {
844 wait = 60;
845 break;
848 ngx_memcpy(name, path->name.data, path->name.len);
850 p = name + path->name.len + 1 + path->len;
851 p = ngx_hex_dump(p, (u_char *) &fcn->node.key,
852 sizeof(ngx_rbtree_key_t));
853 p = ngx_hex_dump(p, fcn->key,
854 NGX_HTTP_CACHE_KEY_LEN - sizeof(ngx_rbtree_key_t));
855 *p = '\0';
857 ngx_queue_remove(q);
859 ngx_rbtree_delete(cache->rbtree, &fcn->node);
861 ngx_slab_free_locked(cache->shpool, fcn);
863 ngx_shmtx_unlock(&cache->shpool->mutex);
865 ngx_create_hashed_filename(path, name, len);
867 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
868 "http file cache expire: \"%s\"", name);
870 if (ngx_delete_file(name) == NGX_FILE_ERROR) {
871 ngx_log_error(NGX_LOG_CRIT, ngx_cycle->log, ngx_errno,
872 ngx_delete_file_n " \"%s\" failed", name);
875 ngx_free(name);
877 ngx_shmtx_lock(&cache->shpool->mutex);
880 ngx_shmtx_unlock(&cache->shpool->mutex);
882 return wait;
886 time_t
887 ngx_http_file_cache_cleaner(void *data)
889 ngx_http_file_cache_t *cache = data;
891 time_t now, next;
892 ngx_tree_ctx_t tree;
894 now = ngx_time();
896 if (now >= cache->next_clean_time
897 && now >= cache->created + cache->inactive)
899 ngx_log_error(NGX_LOG_NOTICE, ngx_cycle->log, 0,
900 "clean unused cache files");
902 tree.init_handler = NULL;
903 tree.file_handler = ngx_http_file_cache_clean_file;
904 tree.pre_tree_handler = ngx_http_file_cache_clean_noop;
905 tree.post_tree_handler = ngx_http_file_cache_clean_noop;
906 tree.spec_handler = ngx_http_file_cache_clean_file;
907 tree.data = cache;
908 tree.alloc = 0;
909 tree.log = ngx_cycle->log;
911 (void) ngx_walk_tree(&tree, &cache->path->name);
913 ngx_time_update(0, 0);
915 next = ngx_next_time(cache->clean_time);
917 if (next == -1) {
918 ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, ngx_errno,
919 ngx_next_time_n " failed");
920 return 60;
923 cache->next_clean_time = next;
926 next = ngx_http_file_cache_expire(cache, 0);
928 now = ngx_time();
930 return (now + next < cache->next_clean_time) ? next:
931 cache->next_clean_time - now;
935 static ngx_int_t
936 ngx_http_file_cache_clean_noop(ngx_tree_ctx_t *ctx, ngx_str_t *path)
938 return NGX_OK;
942 static ngx_int_t
943 ngx_http_file_cache_clean_file(ngx_tree_ctx_t *ctx, ngx_str_t *path)
945 u_char *p, key[2 * NGX_HTTP_CACHE_KEY_LEN];
946 ngx_int_t n;
947 ngx_uint_t i;
948 ngx_http_file_cache_t *cache;
949 ngx_http_file_cache_node_t *node;
951 if (path->len < 2 * NGX_HTTP_CACHE_KEY_LEN) {
952 goto clean;
955 p = &path->data[path->len - 2 * NGX_HTTP_CACHE_KEY_LEN];
957 for (i = 0; i < NGX_HTTP_CACHE_KEY_LEN; i++) {
958 n = ngx_hextoi(p, 2);
960 if (n == NGX_ERROR) {
961 goto clean;
964 p += 2;
966 key[i] = (u_char) n;
969 cache = ctx->data;
971 ngx_shmtx_lock(&cache->shpool->mutex);
973 node = ngx_http_file_cache_lookup(cache, key);
975 ngx_shmtx_unlock(&cache->shpool->mutex);
977 if (node) {
978 return NGX_OK;
981 clean:
983 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
984 "http file cache clean: \"%s\"", path->data);
986 if (ngx_delete_file(path->data) == NGX_FILE_ERROR) {
987 ngx_log_error(NGX_LOG_CRIT, ctx->log, ngx_errno,
988 ngx_delete_file_n " \"%s\" failed", path->data);
989 return NGX_ERROR;
992 return NGX_OK;
996 time_t
997 ngx_http_file_cache_valid(ngx_array_t *cache_valid, ngx_uint_t status)
999 ngx_uint_t i;
1000 ngx_http_cache_valid_t *valid;
1002 valid = cache_valid->elts;
1003 for (i = 0; i < cache_valid->nelts; i++) {
1005 if (valid[i].status == 0) {
1006 return valid[i].valid;
1009 if (valid[i].status == status) {
1010 return valid[i].valid;
1014 return 0;
1018 char *
1019 ngx_http_file_cache_set_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
1021 u_char *last, *p;
1022 time_t inactive, clean_time, next;
1023 ssize_t size;
1024 ngx_str_t s, name, *value;
1025 ngx_uint_t i, n;
1026 ngx_http_file_cache_t *cache;
1028 cache = ngx_pcalloc(cf->pool, sizeof(ngx_http_file_cache_t));
1029 if (cache == NULL) {
1030 return NGX_CONF_ERROR;
1033 cache->path = ngx_pcalloc(cf->pool, sizeof(ngx_path_t));
1034 if (cache->path == NULL) {
1035 return NGX_CONF_ERROR;
1038 inactive = 600;
1039 clean_time = 5 * 60 * 60;
1041 name.len = 0;
1042 size = 0;
1044 value = cf->args->elts;
1046 cache->path->name = value[1];
1048 if (cache->path->name.data[cache->path->name.len - 1] == '/') {
1049 cache->path->name.len--;
1052 if (ngx_conf_full_name(cf->cycle, &cache->path->name, 0) != NGX_OK) {
1053 return NGX_CONF_ERROR;
1056 for (i = 2; i < cf->args->nelts; i++) {
1058 if (ngx_strncmp(value[i].data, "levels=", 7) == 0) {
1060 n = 0;
1061 p = value[i].data + 7;
1062 last = value[i].data + value[i].len;
1064 while (p < last) {
1066 if (*p > '0' && *p < '6') {
1068 cache->path->level[n] = *p++ - '0';
1069 cache->path->len += cache->path->level[n] + 1;
1071 if (p == last) {
1072 break;
1075 if (*p++ == ':') {
1077 if (n > 2) {
1078 goto invalid_levels;
1081 if (cache->path->level[n] == 0) {
1082 goto invalid_levels;
1085 n++;
1087 continue;
1091 goto invalid_levels;
1094 if (cache->path->len < 10 + 3) {
1095 continue;
1098 invalid_levels:
1100 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1101 "invalid \"levels\" \"%V\"", &value[i]);
1102 return NGX_CONF_ERROR;
1105 if (ngx_strncmp(value[i].data, "keys_zone=", 10) == 0) {
1107 name.data = value[i].data + 10;
1109 p = (u_char *) ngx_strchr(name.data, ':');
1111 if (p) {
1112 name.len = p - name.data;
1114 p++;
1116 s.len = value[i].data + value[i].len - p;
1117 s.data = p;
1119 size = ngx_parse_size(&s);
1120 if (size > 8191) {
1121 continue;
1125 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1126 "invalid keys zone size \"%V\"", &value[i]);
1127 return NGX_CONF_ERROR;
1130 if (ngx_strncmp(value[i].data, "inactive=", 9) == 0) {
1132 s.len = value[i].len - 9;
1133 s.data = value[i].data + 9;
1135 inactive = ngx_parse_time(&s, 1);
1136 if (inactive < 0) {
1137 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1138 "invalid inactive value \"%V\"", &value[i]);
1139 return NGX_CONF_ERROR;
1142 continue;
1145 if (ngx_strncmp(value[i].data, "clean_time=", 11) == 0) {
1147 s.len = value[i].len - 11;
1148 s.data = value[i].data + 11;
1150 clean_time = ngx_parse_time(&s, 1);
1151 if (clean_time < 0 || clean_time > 24 * 60 * 60) {
1152 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1153 "invalid clean_time value \"%V\"", &value[i]);
1154 return NGX_CONF_ERROR;
1157 continue;
1160 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1161 "invalid parameter \"%V\"", &value[i]);
1162 return NGX_CONF_ERROR;
1165 if (name.len == 0 || size == 0) {
1166 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1167 "\"%V\" must have \"keys_zone\" parameter",
1168 &cmd->name);
1169 return NGX_CONF_ERROR;
1172 cache->path->cleaner = ngx_http_file_cache_cleaner;
1173 cache->path->data = cache;
1175 if (ngx_add_path(cf, &cache->path) != NGX_OK) {
1176 return NGX_CONF_ERROR;
1179 cache->shm_zone = ngx_shared_memory_add(cf, &name, size, cmd->post);
1180 if (cache->shm_zone == NULL) {
1181 return NGX_CONF_ERROR;
1184 if (cache->shm_zone->data) {
1185 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1186 "duplicate zone \"%V\"", &name);
1187 return NGX_CONF_ERROR;
1190 next = ngx_next_time(clean_time);
1192 if (next == -1) {
1193 ngx_conf_log_error(NGX_LOG_EMERG, cf, ngx_errno,
1194 ngx_next_time_n " failed");
1195 return NGX_CONF_ERROR;
1198 cache->shm_zone->init = ngx_http_file_cache_init;
1199 cache->shm_zone->data = cache;
1201 cache->inactive = inactive;
1202 cache->clean_time = clean_time;
1203 cache->next_clean_time = next;
1205 return NGX_CONF_OK;
1209 char *
1210 ngx_http_file_cache_valid_set_slot(ngx_conf_t *cf, ngx_command_t *cmd,
1211 void *conf)
1213 char *p = conf;
1215 time_t valid;
1216 ngx_str_t *value;
1217 ngx_uint_t i, n, status;
1218 ngx_array_t **a;
1219 ngx_http_cache_valid_t *v;
1220 static ngx_uint_t statuses[] = { 200, 301, 302 };
1222 a = (ngx_array_t **) (p + cmd->offset);
1224 if (*a == NGX_CONF_UNSET_PTR) {
1225 *a = ngx_array_create(cf->pool, 1, sizeof(ngx_http_cache_valid_t));
1226 if (*a == NULL) {
1227 return NGX_CONF_ERROR;
1231 value = cf->args->elts;
1232 n = cf->args->nelts - 1;
1234 valid = ngx_parse_time(&value[n], 1);
1235 if (valid < 0) {
1236 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1237 "invalid time value \"%V\"", &value[n]);
1238 return NGX_CONF_ERROR;
1241 if (n == 1) {
1243 for (i = 0; i < 3; i++) {
1244 v = ngx_array_push(*a);
1245 if (v == NULL) {
1246 return NGX_CONF_ERROR;
1249 v->status = statuses[i];
1250 v->valid = valid;
1253 return NGX_CONF_OK;
1256 for (i = 1; i < n; i++) {
1258 if (ngx_strcmp(value[i].data, "any") == 0) {
1260 status = 0;
1262 } else {
1264 status = ngx_atoi(value[i].data, value[i].len);
1265 if (status < 100) {
1266 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1267 "invalid status \"%V\"", &value[i]);
1268 return NGX_CONF_ERROR;
1272 v = ngx_array_push(*a);
1273 if (v == NULL) {
1274 return NGX_CONF_ERROR;
1277 v->status = status;
1278 v->valid = valid;
1281 return NGX_CONF_OK;