upstream 0.7.33
[nginx.git] / nginx / src / http / ngx_http_cache.c
1
2 /*
3  * Copyright (C) Igor Sysoev
4  */
5
6
7 #include <ngx_config.h>
8 #include <ngx_core.h>
9 #include <ngx_http.h>
10
11
12 #if 0
13
14 static 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
23     NULL,                                  /* create location configuration */
24     NULL                                   /* merge location configuration */
25 };
26
27
28 ngx_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 */
34     NULL                                   /* init process */
35 };
36
37 #endif
38
39
40 static 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
76 ngx_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
129
130 ngx_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
141     if (ngx_mutex_lock(&hash->mutex) == NGX_ERROR) {
142         return (void *) NGX_ERROR;
143     }
144
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         {
150 #if 0
151             if (c[i].expired) {
152                 ngx_mutex_unlock(&hash->mutex);
153                 return (void *) NGX_AGAIN;
154             }
155 #endif
156
157             c[i].refs++;
158
159             if ((!(c[i].notify && (ngx_event_flags & NGX_USE_KQUEUE_EVENT)))
160                 && (ngx_cached_time - c[i].updated >= hash->update))
161             {
162                 c[i].expired = 1;
163             }
164
165             ngx_mutex_unlock(&hash->mutex);
166
167             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
184 ngx_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
198     if (ngx_mutex_lock(&hash->mutex) == NGX_ERROR) {
199         return (void *) NGX_ERROR;
200     }
201
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
270     cache->deleted = 0;
271     cache->expired = 0;
272     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
289 void 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
330 void ngx_http_cache_lock(ngx_http_cache_hash_t *hash, ngx_http_cache_t *cache)
331 {
332     if (ngx_mutex_lock(&hash->mutex) == NGX_ERROR) {
333         return;
334     }
335 }
336
337
338 void ngx_http_cache_unlock(ngx_http_cache_hash_t *hash,
339                            ngx_http_cache_t *cache, ngx_log_t *log)
340 {
341     if (ngx_mutex_lock(&hash->mutex) == NGX_ERROR) {
342         return;
343     }
344
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
357 ngx_http_cache_add_file_event(ngx_http_cache_hash_t *hash,
358                               ngx_http_cache_t *cache)
359 {
360     ngx_event_t                 *ev;
361     ngx_http_cache_event_ctx_t  *ctx;
362
363     ev = &ngx_cycle->read_events[fd];
364     ngx_memzero(ev, sizeof(ngx_event_t);
365
366     ev->data = data;
367     ev->event_handler = ngx_http_cache_invalidate;
368
369     return ngx_add_event(ev, NGX_VNODE_EVENT, 0);
370 }
371
372
373 void ngx_http_cache_invalidate(ngx_event_t *ev)
374 {
375     ngx_http_cache_event_ctx_t  *ctx;
376
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
393
394 /* TODO: currently fd only */
395
396 ngx_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;
433     h->file_last = r->cache->data.size;
434
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
445 char *ngx_http_set_cache_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
446 {
447     char  *p = conf;
448
449     ngx_int_t              i, j, dup, invalid;
450     ngx_str_t              *value, line;
451     ngx_http_cache_t       *c;
452     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     }
563
564     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
572     return NGX_CONF_OK;
573 }
574
575
576 #endif