blob: 8471459afd1f4a67caad3d01a78ff0cef8c64341 [file] [log] [blame]
Igor Sysoev13933252003-05-29 13:02:09 +00001
Igor Sysoevd90282d2004-09-28 08:34:51 +00002/*
Igor Sysoevff8da912004-09-29 16:00:49 +00003 * Copyright (C) Igor Sysoev
Igor Sysoevd90282d2004-09-28 08:34:51 +00004 */
5
6
Igor Sysoev65977492003-11-02 22:56:18 +00007#include <ngx_config.h>
8#include <ngx_core.h>
9#include <ngx_http.h>
10
Igor Sysoev1b138ed2003-11-18 21:34:08 +000011
Igor Sysoevb1dfe472004-12-21 12:30:30 +000012#if 0
Igor Sysoev65977492003-11-02 22:56:18 +000013
Igor Sysoeva8fa0a62003-11-25 20:44:56 +000014static ngx_http_module_t ngx_http_cache_module_ctx = {
15 NULL, /* pre conf */
16
17 NULL, /* create main configuration */
18 NULL, /* init main configuration */
19
20 NULL, /* create server configuration */
21 NULL, /* merge server configuration */
22
Igor Sysoev865c1502003-11-30 20:03:18 +000023 NULL, /* create location configuration */
24 NULL /* merge location configuration */
Igor Sysoeva8fa0a62003-11-25 20:44:56 +000025};
26
27
28ngx_module_t ngx_http_cache_module = {
29 NGX_MODULE,
30 &ngx_http_cache_module_ctx, /* module context */
31 NULL, /* module directives */
32 NGX_HTTP_MODULE, /* module type */
33 NULL, /* init module */
Igor Sysoevb1dfe472004-12-21 12:30:30 +000034 NULL /* init process */
Igor Sysoeva8fa0a62003-11-25 20:44:56 +000035};
36
Igor Sysoevb1dfe472004-12-21 12:30:30 +000037#endif
38
39
40static ngx_int_t ngx_http_cache_create(ngx_http_request_t *r)
41{
42 ngx_str_t *key;
43
44 if (!(r->cache = ngx_pcalloc(r->pool, sizeof(ngx_http_cache_t)))) {
45 return NGX_ERROR;
46 }
47
48 if (ngx_array_init(&r->cache->key, r->pool, 5, sizeof(ngx_str_t))
49 == NGX_ERROR)
50 {
51 return NGX_ERROR;
52 }
53
54 /* preallocate the primary key */
55
56 if (!(key = ngx_array_push(&r->cache->key))) {
57 return NGX_ERROR;
58 }
59
60 key->len = 0;
61 key->data = NULL;
62
63 /*
64 * we use offsetof() because sizeof() pads the struct size to the int size
65 */
66
67 r->cache->header_size = offsetof(ngx_http_cache_header_t, key);
68
69 r->cache->log = r->connection->log;
70 r->cache->file.log = r->connection->log;
71
72 return NGX_OK;
73}
74
75
76ngx_int_t ngx_http_cache_get(ngx_http_request_t *r, ngx_http_cache_ctx_t *ctx)
77{
78 ngx_str_t *key;
79 ngx_http_cache_t *c;
80
81 if (r->cache == NULL) {
82 if (ngx_http_cache_create(r) == NGX_ERROR) {
83 return NGX_ABORT;
84 }
85 }
86
87 c = r->cache;
88 key = c->key.elts;
89
90 if (ctx->primary) {
91 key[0] = ctx->key;
92 c->header_size += ctx->key.len;
93 c->key_len += ctx->key.len;
94 c->buf = ctx->buf;
95
96 } else {
97 if (key[0].len == 0) {
98 key[0] = r->uri;
99 c->header_size += r->uri.len;
100 c->key_len += ctx->key.len;
101 }
102
103 if (!(key = ngx_array_push(&r->cache->key))) {
104 return NGX_ABORT;
105 }
106
107 c->header_size += ctx->key.len;
108 c->key_len += ctx->key.len;
109 }
110
111#if 0
112
113 if (ctx->memory) {
114 ngx_http_memory_cache_get(r, ctx);
115 }
116
117#endif
118
119 if (ctx->file) {
120 return ngx_http_file_cache_get(r, ctx);
121 }
122
123 return NGX_DECLINED;
124}
125
126
127#if 0
128
Igor Sysoeva8fa0a62003-11-25 20:44:56 +0000129
Igor Sysoev865c1502003-11-30 20:03:18 +0000130ngx_http_cache_t *ngx_http_cache_get(ngx_http_cache_hash_t *hash,
131 ngx_http_cleanup_t *cleanup,
132 ngx_str_t *key, uint32_t *crc)
133{
134 ngx_uint_t i;
135 ngx_http_cache_t *c;
136
137 *crc = ngx_crc(key->data, key->len);
138
139 c = hash->elts + *crc % hash->hash * hash->nelts;
140
Igor Sysoev5edf3872004-03-03 16:14:15 +0000141 if (ngx_mutex_lock(&hash->mutex) == NGX_ERROR) {
142 return (void *) NGX_ERROR;
143 }
Igor Sysoev865c1502003-11-30 20:03:18 +0000144
145 for (i = 0; i < hash->nelts; i++) {
146 if (c[i].crc == *crc
147 && c[i].key.len == key->len
148 && ngx_rstrncmp(c[i].key.data, key->data, key->len) == 0)
149 {
Igor Sysoeva7dcbaf2003-12-02 05:47:29 +0000150#if 0
151 if (c[i].expired) {
152 ngx_mutex_unlock(&hash->mutex);
153 return (void *) NGX_AGAIN;
154 }
155#endif
156
Igor Sysoev865c1502003-11-30 20:03:18 +0000157 c[i].refs++;
Igor Sysoev865c1502003-11-30 20:03:18 +0000158
Igor Sysoevf6906042004-11-25 16:17:31 +0000159 if ((!(c[i].notify && (ngx_event_flags & NGX_USE_KQUEUE_EVENT)))
Igor Sysoev4fc368f2003-12-01 16:28:14 +0000160 && (ngx_cached_time - c[i].updated >= hash->update))
161 {
162 c[i].expired = 1;
Igor Sysoev865c1502003-11-30 20:03:18 +0000163 }
164
Igor Sysoeva7dcbaf2003-12-02 05:47:29 +0000165 ngx_mutex_unlock(&hash->mutex);
166
Igor Sysoev865c1502003-11-30 20:03:18 +0000167 if (cleanup) {
168 cleanup->data.cache.hash = hash;
169 cleanup->data.cache.cache = &c[i];
170 cleanup->valid = 1;
171 cleanup->cache = 1;
172 }
173
174 return &c[i];
175 }
176 }
177
178 ngx_mutex_unlock(&hash->mutex);
179
180 return NULL;
181}
182
183
184ngx_http_cache_t *ngx_http_cache_alloc(ngx_http_cache_hash_t *hash,
185 ngx_http_cache_t *cache,
186 ngx_http_cleanup_t *cleanup,
187 ngx_str_t *key, uint32_t crc,
188 ngx_str_t *value, ngx_log_t *log)
189{
190 time_t old;
191 ngx_uint_t i;
192 ngx_http_cache_t *c;
193
194 old = ngx_cached_time + 1;
195
196 c = hash->elts + crc % hash->hash * hash->nelts;
197
Igor Sysoev5edf3872004-03-03 16:14:15 +0000198 if (ngx_mutex_lock(&hash->mutex) == NGX_ERROR) {
199 return (void *) NGX_ERROR;
200 }
Igor Sysoev865c1502003-11-30 20:03:18 +0000201
202 if (cache == NULL) {
203
204 /* allocate a new entry */
205
206 for (i = 0; i < hash->nelts; i++) {
207 if (c[i].refs > 0) {
208 /* a busy entry */
209 continue;
210 }
211
212 if (c[i].key.len == 0) {
213 /* a free entry is found */
214 cache = &c[i];
215 break;
216 }
217
218 /* looking for the oldest cache entry */
219
220 if (old > c[i].accessed) {
221
222 old = c[i].accessed;
223 cache = &c[i];
224 }
225 }
226
227 if (cache == NULL) {
228 ngx_mutex_unlock(&hash->mutex);
229 return NULL;
230 }
231
232 ngx_http_cache_free(cache, key, value, log);
233
234 if (cache->key.data == NULL) {
235 cache->key.data = ngx_alloc(key->len, log);
236 if (cache->key.data == NULL) {
237 ngx_http_cache_free(cache, NULL, NULL, log);
238 ngx_mutex_unlock(&hash->mutex);
239 return NULL;
240 }
241 }
242
243 cache->key.len = key->len;
244 ngx_memcpy(cache->key.data, key->data, key->len);
245
246 } else if (value) {
247 ngx_http_cache_free(cache, key, value, log);
248 }
249
250 if (value) {
251 if (cache->data.value.data == NULL) {
252 cache->data.value.data = ngx_alloc(value->len, log);
253 if (cache->data.value.data == NULL) {
254 ngx_http_cache_free(cache, NULL, NULL, log);
255 ngx_mutex_unlock(&hash->mutex);
256 return NULL;
257 }
258 }
259
260 cache->data.value.len = value->len;
261 ngx_memcpy(cache->data.value.data, value->data, value->len);
262 }
263
264 cache->crc = crc;
265 cache->key.len = key->len;
266
267 cache->refs = 1;
268 cache->count = 0;
269
Igor Sysoev865c1502003-11-30 20:03:18 +0000270 cache->deleted = 0;
Igor Sysoev4fc368f2003-12-01 16:28:14 +0000271 cache->expired = 0;
Igor Sysoev865c1502003-11-30 20:03:18 +0000272 cache->memory = 0;
273 cache->mmap = 0;
274 cache->notify = 0;
275
276 if (cleanup) {
277 cleanup->data.cache.hash = hash;
278 cleanup->data.cache.cache = cache;
279 cleanup->valid = 1;
280 cleanup->cache = 1;
281 }
282
283 ngx_mutex_unlock(&hash->mutex);
284
285 return cache;
286}
287
288
289void ngx_http_cache_free(ngx_http_cache_t *cache,
290 ngx_str_t *key, ngx_str_t *value, ngx_log_t *log)
291{
292 if (cache->memory) {
293 if (cache->data.value.data
294 && (value == NULL || value->len > cache->data.value.len))
295 {
296 ngx_free(cache->data.value.data);
297 cache->data.value.data = NULL;
298 }
299 }
300
301 /* TODO: mmap */
302
303 cache->data.value.len = 0;
304
305 if (cache->fd != NGX_INVALID_FILE) {
306
307 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
308 "http cache close fd: %d", cache->fd);
309
310 if (ngx_close_file(cache->fd) == NGX_FILE_ERROR) {
311 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
312 ngx_close_file_n " \"%s\" failed",
313 cache->key.data);
314 }
315
316 cache->fd = NGX_INVALID_FILE;
317 }
318
319 if (cache->key.data && (key == NULL || key->len > cache->key.len)) {
320 ngx_free(cache->key.data);
321 cache->key.data = NULL;
322 }
323
324 cache->key.len = 0;
325
326 cache->refs = 0;
327}
328
329
Igor Sysoeva7dcbaf2003-12-02 05:47:29 +0000330void ngx_http_cache_lock(ngx_http_cache_hash_t *hash, ngx_http_cache_t *cache)
331{
Igor Sysoev5edf3872004-03-03 16:14:15 +0000332 if (ngx_mutex_lock(&hash->mutex) == NGX_ERROR) {
333 return;
334 }
Igor Sysoeva7dcbaf2003-12-02 05:47:29 +0000335}
336
337
Igor Sysoev865c1502003-11-30 20:03:18 +0000338void ngx_http_cache_unlock(ngx_http_cache_hash_t *hash,
339 ngx_http_cache_t *cache, ngx_log_t *log)
340{
Igor Sysoev5edf3872004-03-03 16:14:15 +0000341 if (ngx_mutex_lock(&hash->mutex) == NGX_ERROR) {
342 return;
343 }
Igor Sysoev865c1502003-11-30 20:03:18 +0000344
345 cache->refs--;
346
347 if (cache->refs == 0 && cache->deleted) {
348 ngx_http_cache_free(cache, NULL, NULL, log);
349 }
350
351 ngx_mutex_unlock(&hash->mutex);
352}
353
354
355#if 0
356
Igor Sysoeva7dcbaf2003-12-02 05:47:29 +0000357ngx_http_cache_add_file_event(ngx_http_cache_hash_t *hash,
358 ngx_http_cache_t *cache)
Igor Sysoev865c1502003-11-30 20:03:18 +0000359{
Igor Sysoeva7dcbaf2003-12-02 05:47:29 +0000360 ngx_event_t *ev;
361 ngx_http_cache_event_ctx_t *ctx;
Igor Sysoev865c1502003-11-30 20:03:18 +0000362
363 ev = &ngx_cycle->read_events[fd];
364 ngx_memzero(ev, sizeof(ngx_event_t);
365
366 ev->data = data;
Igor Sysoev4fc368f2003-12-01 16:28:14 +0000367 ev->event_handler = ngx_http_cache_invalidate;
Igor Sysoev865c1502003-11-30 20:03:18 +0000368
369 return ngx_add_event(ev, NGX_VNODE_EVENT, 0);
370}
371
372
373void ngx_http_cache_invalidate(ngx_event_t *ev)
374{
Igor Sysoeva7dcbaf2003-12-02 05:47:29 +0000375 ngx_http_cache_event_ctx_t *ctx;
Igor Sysoev865c1502003-11-30 20:03:18 +0000376
377 ctx = ev->data;
378
379 ngx_http_cache_lock(&ctx->hash->mutex);
380
381 if (ctx->cache->refs == 0)
382 ngx_http_cache_free(ctx->cache, NULL, NULL, ctx->log);
383
384 } else {
385 ctx->cache->deleted = 1;
386 }
387
388 ngx_http_cache_unlock(&ctx->hash->mutex);
389}
390
391#endif
392
Igor Sysoeva8fa0a62003-11-25 20:44:56 +0000393
Igor Sysoev865c1502003-11-30 20:03:18 +0000394/* TODO: currently fd only */
395
396ngx_int_t ngx_http_send_cached(ngx_http_request_t *r)
397{
398 ngx_int_t rc;
399 ngx_hunk_t *h;
400 ngx_chain_t out;
401 ngx_http_log_ctx_t *ctx;
402
403 ctx = r->connection->log->data;
404 ctx->action = "sending response to client";
405
406 r->headers_out.status = NGX_HTTP_OK;
407 r->headers_out.content_length_n = r->cache->data.size;
408 r->headers_out.last_modified_time = r->cache->last_modified;
409
410 if (ngx_http_set_content_type(r) != NGX_OK) {
411 return NGX_HTTP_INTERNAL_SERVER_ERROR;
412 }
413
414 /* we need to allocate all before the header would be sent */
415
416 if (!(h = ngx_pcalloc(r->pool, sizeof(ngx_hunk_t)))) {
417 return NGX_HTTP_INTERNAL_SERVER_ERROR;
418 }
419
420 if (!(h->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t)))) {
421 return NGX_HTTP_INTERNAL_SERVER_ERROR;
422 }
423
424 rc = ngx_http_send_header(r);
425
426 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
427 return rc;
428 }
429
430 h->type = r->main ? NGX_HUNK_FILE : NGX_HUNK_FILE|NGX_HUNK_LAST;
431
432 h->file_pos = 0;
Igor Sysoev4fc368f2003-12-01 16:28:14 +0000433 h->file_last = r->cache->data.size;
Igor Sysoev865c1502003-11-30 20:03:18 +0000434
435 h->file->fd = r->cache->fd;
436 h->file->log = r->connection->log;
437
438 out.hunk = h;
439 out.next = NULL;
440
441 return ngx_http_output_filter(r, &out);
442}
443
444
Igor Sysoev877df632003-11-28 08:40:40 +0000445char *ngx_http_set_cache_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
446{
447 char *p = conf;
448
Igor Sysoev865c1502003-11-30 20:03:18 +0000449 ngx_int_t i, j, dup, invalid;
Igor Sysoev877df632003-11-28 08:40:40 +0000450 ngx_str_t *value, line;
Igor Sysoev865c1502003-11-30 20:03:18 +0000451 ngx_http_cache_t *c;
Igor Sysoev877df632003-11-28 08:40:40 +0000452 ngx_http_cache_hash_t *ch, **chp;
453
454 chp = (ngx_http_cache_hash_t **) (p + cmd->offset);
455 if (*chp) {
456 return "is duplicate";
457 }
458
459 if (!(ch = ngx_pcalloc(cf->pool, sizeof(ngx_http_cache_hash_t)))) {
460 return NGX_CONF_ERROR;
461 }
462 *chp = ch;
463
464 dup = 0;
465 invalid = 0;
466
467 value = cf->args->elts;
468
469 for (i = 1; i < cf->args->nelts; i++) {
470
471 if (value[i].data[1] != '=') {
472 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
473 "invalid value \"%s\"", value[i].data);
474 return NGX_CONF_ERROR;
475 }
476
477 switch (value[i].data[0]) {
478
479 case 'h':
480 if (ch->hash) {
481 dup = 1;
482 break;
483 }
484
485 ch->hash = ngx_atoi(value[i].data + 2, value[i].len - 2);
486 if (ch->hash == (size_t) NGX_ERROR || ch->hash == 0) {
487 invalid = 1;
488 break;
489 }
490
491 continue;
492
493 case 'n':
494 if (ch->nelts) {
495 dup = 1;
496 break;
497 }
498
499 ch->nelts = ngx_atoi(value[i].data + 2, value[i].len - 2);
500 if (ch->nelts == (size_t) NGX_ERROR || ch->nelts == 0) {
501 invalid = 1;
502 break;
503 }
504
505 continue;
506
507 case 'l':
508 if (ch->life) {
509 dup = 1;
510 break;
511 }
512
513 line.len = value[i].len - 2;
514 line.data = value[i].data + 2;
515
516 ch->life = ngx_parse_time(&line, 1);
517 if (ch->life == NGX_ERROR || ch->life == 0) {
518 invalid = 1;
519 break;
520 }
521
522 continue;
523
524 case 'u':
525 if (ch->update) {
526 dup = 1;
527 break;
528 }
529
530 line.len = value[i].len - 2;
531 line.data = value[i].data + 2;
532
533 ch->update = ngx_parse_time(&line, 1);
534 if (ch->update == NGX_ERROR || ch->update == 0) {
535 invalid = 1;
536 break;
537 }
538
539 continue;
540
541 default:
542 invalid = 1;
543 }
544
545 if (dup) {
546 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
547 "duplicate value \"%s\"", value[i].data);
548 return NGX_CONF_ERROR;
549 }
550
551 if (invalid) {
552 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
553 "invalid value \"%s\"", value[i].data);
554 return NGX_CONF_ERROR;
555 }
556 }
557
558 ch->elts = ngx_pcalloc(cf->pool,
559 ch->hash * ch->nelts * sizeof(ngx_http_cache_t));
560 if (ch->elts == NULL) {
561 return NGX_CONF_ERROR;
562 }
Igor Sysoeva8fa0a62003-11-25 20:44:56 +0000563
Igor Sysoev865c1502003-11-30 20:03:18 +0000564 for (i = 0; i < (ngx_int_t) ch->hash; i++) {
565 c = ch->elts + i * ch->nelts;
566
567 for (j = 0; j < (ngx_int_t) ch->nelts; j++) {
568 c[j].fd = NGX_INVALID_FILE;
569 }
570 }
571
Igor Sysoeva8fa0a62003-11-25 20:44:56 +0000572 return NGX_CONF_OK;
Igor Sysoev65977492003-11-02 22:56:18 +0000573}
Igor Sysoevb1dfe472004-12-21 12:30:30 +0000574
575
576#endif