comparison src/http/ngx_http_cache.c @ 201:267ea1d98683

nginx-0.0.1-2003-11-30-23:03:18 import
author Igor Sysoev <igor@sysoev.ru>
date Sun, 30 Nov 2003 20:03:18 +0000
parents abeaebe0a33c
children 74994aeef848
comparison
equal deleted inserted replaced
200:abeaebe0a33c 201:267ea1d98683
11 #define MD5Update MD5_Update 11 #define MD5Update MD5_Update
12 #define MD5Final MD5_Final 12 #define MD5Final MD5_Final
13 #endif 13 #endif
14 14
15 15
16 static int ngx_crc(char *data, size_t len);
17
18 static void *ngx_http_cache_create_conf(ngx_conf_t *cf);
19 static char *ngx_http_core_merge_loc_conf(ngx_conf_t *cf,
20 void *parent, void *child);
21
22
23 static ngx_http_module_t ngx_http_cache_module_ctx = { 16 static ngx_http_module_t ngx_http_cache_module_ctx = {
24 NULL, /* pre conf */ 17 NULL, /* pre conf */
25 18
26 NULL, /* create main configuration */ 19 NULL, /* create main configuration */
27 NULL, /* init main configuration */ 20 NULL, /* init main configuration */
28 21
29 NULL, /* create server configuration */ 22 NULL, /* create server configuration */
30 NULL, /* merge server configuration */ 23 NULL, /* merge server configuration */
31 24
32 ngx_http_cache_create_conf, /* create location configuration */ 25 NULL, /* create location configuration */
33 ngx_http_core_merge_loc_conf /* merge location configuration */ 26 NULL /* merge location configuration */
34 }; 27 };
35 28
36 29
37 ngx_module_t ngx_http_cache_module = { 30 ngx_module_t ngx_http_cache_module = {
38 NGX_MODULE, 31 NGX_MODULE,
42 NULL, /* init module */ 35 NULL, /* init module */
43 NULL /* init child */ 36 NULL /* init child */
44 }; 37 };
45 38
46 39
47 40 ngx_http_cache_t *ngx_http_cache_get(ngx_http_cache_hash_t *hash,
48 int ngx_http_cache_get_file(ngx_http_request_t *r, ngx_http_cache_ctx_t *ctx) 41 ngx_http_cleanup_t *cleanup,
49 {
50 MD5_CTX md5;
51
52 /* we use offsetof() because sizeof() pads struct size to int size */
53 ctx->header_size = offsetof(ngx_http_cache_header_t, key)
54 + ctx->key.len + 1;
55
56 ctx->file.name.len = ctx->path->name.len + 1 + ctx->path->len + 32;
57 if (!(ctx->file.name.data = ngx_palloc(r->pool, ctx->file.name.len + 1))) {
58 return NGX_ERROR;
59 }
60
61 ngx_memcpy(ctx->file.name.data, ctx->path->name.data, ctx->path->name.len);
62
63 MD5Init(&md5);
64 MD5Update(&md5, (u_char *) ctx->key.data, ctx->key.len);
65 MD5Final(ctx->md5, &md5);
66
67 ngx_md5_text(ctx->file.name.data + ctx->path->name.len + 1 + ctx->path->len,
68 ctx->md5);
69
70 ngx_log_debug(r->connection->log, "URL: %s, md5: %s" _ ctx->key.data _
71 ctx->file.name.data + ctx->path->name.len + 1 + ctx->path->len);
72
73 ngx_create_hashed_filename(&ctx->file, ctx->path);
74
75 ngx_log_debug(r->connection->log, "FILE: %s" _ ctx->file.name.data);
76
77 /* TODO: look open files cache */
78
79 return ngx_http_cache_open_file(ctx, 0);
80 }
81
82
83 ngx_http_cache_t *ngx_http_cache_get(ngx_http_cache_hash_t *cache,
84 ngx_str_t *key, uint32_t *crc) 42 ngx_str_t *key, uint32_t *crc)
85 { 43 {
86 ngx_uint_t i; 44 ngx_uint_t i;
87 ngx_http_cache_t *c; 45 ngx_http_cache_t *c;
88 46
89 *crc = ngx_crc(key->data, key->len); 47 *crc = ngx_crc(key->data, key->len);
90 48
91 c = cache->elts + *crc % cache->hash * cache->nelts; 49 c = hash->elts + *crc % hash->hash * hash->nelts;
92 50
93 ngx_mutex_lock(&cache->mutex); 51 ngx_mutex_lock(&hash->mutex);
94 52
95 for (i = 0; i < cache->nelts; i++) { 53 for (i = 0; i < hash->nelts; i++) {
96 if (c[i].crc == *crc 54 if (c[i].crc == *crc
97 && c[i].key.len == key->len 55 && c[i].key.len == key->len
98 && ngx_rstrncmp(c[i].key.data, key->data, key->len) == 0) 56 && ngx_rstrncmp(c[i].key.data, key->data, key->len) == 0)
99 { 57 {
100 c[i].refs++; 58 c[i].refs++;
101 ngx_mutex_unlock(&cache->mutex); 59 ngx_mutex_unlock(&hash->mutex);
60
61 if (c[i].notify) {
62 if (!(ngx_event_flags & NGX_HAVE_KQUEUE_EVENT)) {
63 c[i].valid = 0;
64 }
65
66 } else if (ngx_cached_time - c[i].updated >= hash->update) {
67 c[i].valid = 0;
68 }
69
70 if (cleanup) {
71 cleanup->data.cache.hash = hash;
72 cleanup->data.cache.cache = &c[i];
73 cleanup->valid = 1;
74 cleanup->cache = 1;
75 }
76
102 return &c[i]; 77 return &c[i];
103 } 78 }
104 } 79 }
105 80
106 ngx_mutex_unlock(&cache->mutex); 81 ngx_mutex_unlock(&hash->mutex);
107 82
108 return NULL; 83 return NULL;
109 } 84 }
110 85
111 86
112 ngx_http_cache_t *ngx_http_cache_alloc(ngx_http_cache_hash_t *cache, 87 ngx_http_cache_t *ngx_http_cache_alloc(ngx_http_cache_hash_t *hash,
88 ngx_http_cache_t *cache,
89 ngx_http_cleanup_t *cleanup,
113 ngx_str_t *key, uint32_t crc, 90 ngx_str_t *key, uint32_t crc,
114 ngx_log_t *log) 91 ngx_str_t *value, ngx_log_t *log)
115 { 92 {
116 time_t old; 93 time_t old;
117 ngx_uint_t i; 94 ngx_uint_t i;
118 ngx_http_cache_t *c, *found; 95 ngx_http_cache_t *c;
119 96
120 old = ngx_cached_time + 1; 97 old = ngx_cached_time + 1;
121 found = NULL; 98
122 99 c = hash->elts + crc % hash->hash * hash->nelts;
123 c = cache->elts + crc % cache->hash * cache->nelts; 100
124 101 ngx_mutex_lock(&hash->mutex);
125 ngx_mutex_lock(&cache->mutex); 102
126 103 if (cache == NULL) {
127 for (i = 0; i < cache->nelts; i++) { 104
128 if (c[i].refs > 0) { 105 /* allocate a new entry */
129 /* a busy entry */ 106
130 continue; 107 for (i = 0; i < hash->nelts; i++) {
131 } 108 if (c[i].refs > 0) {
132 109 /* a busy entry */
133 if (c[i].key.data == NULL) { 110 continue;
134 /* a free entry is found */ 111 }
135 found = &c[i]; 112
136 break; 113 if (c[i].key.len == 0) {
137 } 114 /* a free entry is found */
138 115 cache = &c[i];
139 /* looking for the oldest cache entry */ 116 break;
140 117 }
141 if (old > c[i].accessed) { 118
142 119 /* looking for the oldest cache entry */
143 old = c[i].accessed; 120
144 found = &c[i]; 121 if (old > c[i].accessed) {
145 } 122
146 } 123 old = c[i].accessed;
147 124 cache = &c[i];
148 if (found) { 125 }
149 if (found->key.data) { 126 }
150 if (key->len > found->key.len) { 127
151 ngx_free(found->key.data); 128 if (cache == NULL) {
152 found->key.data = NULL; 129 ngx_mutex_unlock(&hash->mutex);
153 } 130 return NULL;
154 } 131 }
155 132
156 if (found->key.data == NULL) { 133 ngx_http_cache_free(cache, key, value, log);
157 found->key.data = ngx_alloc(key->len, log); 134
158 if (found->key.data == NULL) { 135 if (cache->key.data == NULL) {
159 ngx_mutex_unlock(&cache->mutex); 136 cache->key.data = ngx_alloc(key->len, log);
137 if (cache->key.data == NULL) {
138 ngx_http_cache_free(cache, NULL, NULL, log);
139 ngx_mutex_unlock(&hash->mutex);
160 return NULL; 140 return NULL;
161 } 141 }
162 } 142 }
163 143
164 ngx_memcpy(found->key.data, key->data, key->len); 144 cache->key.len = key->len;
165 145 ngx_memcpy(cache->key.data, key->data, key->len);
166 found->crc = crc; 146
167 found->key.len = key->len; 147 } else if (value) {
168 found->refs = 1; 148 ngx_http_cache_free(cache, key, value, log);
169 found->count = 0; 149 }
170 found->deleted = 0; 150
171 } 151 if (value) {
172 152 if (cache->data.value.data == NULL) {
173 ngx_mutex_unlock(&cache->mutex); 153 cache->data.value.data = ngx_alloc(value->len, log);
174 154 if (cache->data.value.data == NULL) {
175 return found; 155 ngx_http_cache_free(cache, NULL, NULL, log);
156 ngx_mutex_unlock(&hash->mutex);
157 return NULL;
158 }
159 }
160
161 cache->data.value.len = value->len;
162 ngx_memcpy(cache->data.value.data, value->data, value->len);
163 }
164
165 cache->crc = crc;
166 cache->key.len = key->len;
167
168 cache->refs = 1;
169 cache->count = 0;
170
171 cache->valid = 1;
172 cache->deleted = 0;
173 cache->memory = 0;
174 cache->mmap = 0;
175 cache->notify = 0;
176
177 if (cleanup) {
178 cleanup->data.cache.hash = hash;
179 cleanup->data.cache.cache = cache;
180 cleanup->valid = 1;
181 cleanup->cache = 1;
182 }
183
184 ngx_mutex_unlock(&hash->mutex);
185
186 return cache;
187 }
188
189
190 void ngx_http_cache_free(ngx_http_cache_t *cache,
191 ngx_str_t *key, ngx_str_t *value, ngx_log_t *log)
192 {
193 if (cache->memory) {
194 if (cache->data.value.data
195 && (value == NULL || value->len > cache->data.value.len))
196 {
197 ngx_free(cache->data.value.data);
198 cache->data.value.data = NULL;
199 }
200 }
201
202 /* TODO: mmap */
203
204 cache->data.value.len = 0;
205
206 if (cache->fd != NGX_INVALID_FILE) {
207
208 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
209 "http cache close fd: %d", cache->fd);
210
211 if (ngx_close_file(cache->fd) == NGX_FILE_ERROR) {
212 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
213 ngx_close_file_n " \"%s\" failed",
214 cache->key.data);
215 }
216
217 cache->fd = NGX_INVALID_FILE;
218 }
219
220 if (cache->key.data && (key == NULL || key->len > cache->key.len)) {
221 ngx_free(cache->key.data);
222 cache->key.data = NULL;
223 }
224
225 cache->key.len = 0;
226
227 cache->refs = 0;
176 } 228 }
177 229
178 230
179 void ngx_http_cache_unlock(ngx_http_cache_hash_t *hash, 231 void ngx_http_cache_unlock(ngx_http_cache_hash_t *hash,
180 ngx_http_cache_t *cache, ngx_log_t *log) 232 ngx_http_cache_t *cache, ngx_log_t *log)
182 ngx_mutex_lock(&hash->mutex); 234 ngx_mutex_lock(&hash->mutex);
183 235
184 cache->refs--; 236 cache->refs--;
185 237
186 if (cache->refs == 0 && cache->deleted) { 238 if (cache->refs == 0 && cache->deleted) {
187 ngx_log_debug(log, "CLOSE FILE: %d" _ cache->fd); 239 ngx_http_cache_free(cache, NULL, NULL, log);
188 if (cache->fd != NGX_INVALID_FILE) {
189 if (ngx_close_file(cache->fd) == NGX_FILE_ERROR) {
190 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
191 ngx_close_file_n " \"%s\" failed",
192 cache->key.data);
193 }
194 }
195 cache->key.data = NULL;
196 } 240 }
197 241
198 ngx_mutex_unlock(&hash->mutex); 242 ngx_mutex_unlock(&hash->mutex);
243 }
244
245
246 #if 0
247
248 ngx_add_file_event(ngx_fd_t, ngx_event_handler_pt *handler, void *data)
249 {
250 ngx_event_t *ev;
251
252 ev = &ngx_cycle->read_events[fd];
253 ngx_memzero(ev, sizeof(ngx_event_t);
254
255 ev->data = data;
256 ev->event_handler = handler;
257
258 return ngx_add_event(ev, NGX_VNODE_EVENT, 0);
259 }
260
261
262 void ngx_http_cache_invalidate(ngx_event_t *ev)
263 {
264 ngx_http_cache_ctx_t *ctx;
265
266 ctx = ev->data;
267
268 ngx_http_cache_lock(&ctx->hash->mutex);
269
270 if (ctx->cache->refs == 0)
271 ngx_http_cache_free(ctx->cache, NULL, NULL, ctx->log);
272
273 } else {
274 ctx->cache->deleted = 1;
275 }
276
277 ngx_http_cache_unlock(&ctx->hash->mutex);
278 }
279
280 #endif
281
282
283 int ngx_http_cache_get_file(ngx_http_request_t *r, ngx_http_cache_ctx_t *ctx)
284 {
285 MD5_CTX md5;
286
287 /* we use offsetof() because sizeof() pads struct size to int size */
288 ctx->header_size = offsetof(ngx_http_cache_header_t, key)
289 + ctx->key.len + 1;
290
291 ctx->file.name.len = ctx->path->name.len + 1 + ctx->path->len + 32;
292 if (!(ctx->file.name.data = ngx_palloc(r->pool, ctx->file.name.len + 1))) {
293 return NGX_ERROR;
294 }
295
296 ngx_memcpy(ctx->file.name.data, ctx->path->name.data, ctx->path->name.len);
297
298 MD5Init(&md5);
299 MD5Update(&md5, (u_char *) ctx->key.data, ctx->key.len);
300 MD5Final(ctx->md5, &md5);
301
302 ngx_md5_text(ctx->file.name.data + ctx->path->name.len + 1 + ctx->path->len,
303 ctx->md5);
304
305 ngx_log_debug(r->connection->log, "URL: %s, md5: %s" _ ctx->key.data _
306 ctx->file.name.data + ctx->path->name.len + 1 + ctx->path->len);
307
308 ngx_create_hashed_filename(&ctx->file, ctx->path);
309
310 ngx_log_debug(r->connection->log, "FILE: %s" _ ctx->file.name.data);
311
312 /* TODO: look open files cache */
313
314 return ngx_http_cache_open_file(ctx, 0);
199 } 315 }
200 316
201 317
202 int ngx_http_cache_open_file(ngx_http_cache_ctx_t *ctx, ngx_file_uniq_t uniq) 318 int ngx_http_cache_open_file(ngx_http_cache_ctx_t *ctx, ngx_file_uniq_t uniq)
203 { 319 {
329 retry = 1; 445 retry = 1;
330 } 446 }
331 } 447 }
332 448
333 449
450 /* TODO: currently fd only */
451
452 ngx_int_t ngx_http_send_cached(ngx_http_request_t *r)
453 {
454 ngx_int_t rc;
455 ngx_hunk_t *h;
456 ngx_chain_t out;
457 ngx_http_log_ctx_t *ctx;
458
459 ctx = r->connection->log->data;
460 ctx->action = "sending response to client";
461
462 r->headers_out.status = NGX_HTTP_OK;
463 r->headers_out.content_length_n = r->cache->data.size;
464 r->headers_out.last_modified_time = r->cache->last_modified;
465
466 if (ngx_http_set_content_type(r) != NGX_OK) {
467 return NGX_HTTP_INTERNAL_SERVER_ERROR;
468 }
469
470 /* we need to allocate all before the header would be sent */
471
472 if (!(h = ngx_pcalloc(r->pool, sizeof(ngx_hunk_t)))) {
473 return NGX_HTTP_INTERNAL_SERVER_ERROR;
474 }
475
476 if (!(h->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t)))) {
477 return NGX_HTTP_INTERNAL_SERVER_ERROR;
478 }
479
480 rc = ngx_http_send_header(r);
481
482 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
483 return rc;
484 }
485
486 h->type = r->main ? NGX_HUNK_FILE : NGX_HUNK_FILE|NGX_HUNK_LAST;
487
488 h->file_pos = 0;
489 h->file_last = ngx_file_size(&r->file.info);
490
491 h->file->fd = r->cache->fd;
492 h->file->log = r->connection->log;
493
494 out.hunk = h;
495 out.next = NULL;
496
497 return ngx_http_output_filter(r, &out);
498 }
499
500
334 int ngx_garbage_collector_http_cache_handler(ngx_gc_t *gc, ngx_str_t *name, 501 int ngx_garbage_collector_http_cache_handler(ngx_gc_t *gc, ngx_str_t *name,
335 ngx_dir_t *dir) 502 ngx_dir_t *dir)
336 { 503 {
337 int rc; 504 int rc;
338 char data[sizeof(ngx_http_cache_header_t)]; 505 char data[sizeof(ngx_http_cache_header_t)];
373 540
374 return NGX_OK; 541 return NGX_OK;
375 } 542 }
376 543
377 544
378 /* 32-bit crc16 */
379
380 static int ngx_crc(char *data, size_t len)
381 {
382 uint32_t sum;
383
384 for (sum = 0; len; len--) {
385 /*
386 * gcc 2.95.2 x86 and icc 7.1.006 compile that operator
387 * into the single rol opcode.
388 * msvc 6.0sp2 compiles it into four opcodes.
389 */
390 sum = sum >> 1 | sum << 31;
391
392 sum += *data++;
393 }
394
395 return sum;
396 }
397
398
399 static void *ngx_http_cache_create_conf(ngx_conf_t *cf)
400 {
401 ngx_http_cache_conf_t *conf;
402
403 if (!(conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_cache_conf_t)))) {
404 return NGX_CONF_ERROR;
405 }
406
407 return conf;
408 }
409
410
411 static char *ngx_http_core_merge_loc_conf(ngx_conf_t *cf,
412 void *parent, void *child)
413 {
414 ngx_http_cache_conf_t *prev = parent;
415 ngx_http_cache_conf_t *conf = child;
416
417 if (conf->open_files == NULL) {
418 conf->open_files = prev->open_files;
419 }
420
421 #if 0
422 if (conf->open_files == NULL) {
423 if (prev->open_files) {
424 conf->open_files = prev->open_files;
425
426 } else {
427 conf->open_files = ngx_pcalloc(cf->pool,
428 sizeof(ngx_http_cache_hash_t));
429 if (conf->open_files == NULL) {
430 return NGX_CONF_ERROR;
431 }
432
433 conf->open_files->hash = NGX_HTTP_CACHE_HASH;
434 conf->open_files->nelts = NGX_HTTP_CACHE_NELTS;
435
436 conf->open_files->elts = ngx_pcalloc(cf->pool,
437 NGX_HTTP_CACHE_HASH
438 * NGX_HTTP_CACHE_NELTS
439 * sizeof(ngx_http_cache_t));
440 if (conf->open_files->elts == NULL) {
441 return NGX_CONF_ERROR;
442 }
443 }
444 }
445 #endif
446
447 return NGX_CONF_OK;
448 }
449
450
451 char *ngx_http_set_cache_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) 545 char *ngx_http_set_cache_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
452 { 546 {
453 char *p = conf; 547 char *p = conf;
454 548
455 ngx_int_t i, dup, invalid; 549 ngx_int_t i, j, dup, invalid;
456 ngx_str_t *value, line; 550 ngx_str_t *value, line;
551 ngx_http_cache_t *c;
457 ngx_http_cache_hash_t *ch, **chp; 552 ngx_http_cache_hash_t *ch, **chp;
458 553
459 chp = (ngx_http_cache_hash_t **) (p + cmd->offset); 554 chp = (ngx_http_cache_hash_t **) (p + cmd->offset);
460 if (*chp) { 555 if (*chp) {
461 return "is duplicate"; 556 return "is duplicate";
564 ch->hash * ch->nelts * sizeof(ngx_http_cache_t)); 659 ch->hash * ch->nelts * sizeof(ngx_http_cache_t));
565 if (ch->elts == NULL) { 660 if (ch->elts == NULL) {
566 return NGX_CONF_ERROR; 661 return NGX_CONF_ERROR;
567 } 662 }
568 663
664 for (i = 0; i < (ngx_int_t) ch->hash; i++) {
665 c = ch->elts + i * ch->nelts;
666
667 for (j = 0; j < (ngx_int_t) ch->nelts; j++) {
668 c[j].fd = NGX_INVALID_FILE;
669 }
670 }
671
569 return NGX_CONF_OK; 672 return NGX_CONF_OK;
570 } 673 }