Mercurial > hg > nginx
comparison src/http/ngx_http_cache.c @ 199:a65b630b3a66
nginx-0.0.1-2003-11-28-11:40:40 import
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Fri, 28 Nov 2003 08:40:40 +0000 |
parents | 0b81c7a0b133 |
children | abeaebe0a33c |
comparison
equal
deleted
inserted
replaced
198:34995c5ec6c4 | 199:a65b630b3a66 |
---|---|
86 ngx_uint_t i; | 86 ngx_uint_t i; |
87 ngx_http_cache_t *c; | 87 ngx_http_cache_t *c; |
88 | 88 |
89 *crc = ngx_crc(key->data, key->len); | 89 *crc = ngx_crc(key->data, key->len); |
90 | 90 |
91 c = cache->elts | 91 c = cache->elts + *crc % cache->hash * cache->nelts; |
92 + *crc % cache->hash * cache->nelts * sizeof(ngx_http_cache_t); | 92 |
93 ngx_mutex_lock(&cache->mutex); | |
93 | 94 |
94 for (i = 0; i < cache->nelts; i++) { | 95 for (i = 0; i < cache->nelts; i++) { |
95 if (c[i].crc == *crc | 96 if (c[i].crc == *crc |
96 && c[i].key.len == key->len | 97 && c[i].key.len == key->len |
97 && ngx_rstrncmp(c[i].key.data, key->data, key->len) == 0) | 98 && ngx_rstrncmp(c[i].key.data, key->data, key->len) == 0) |
98 { | 99 { |
99 c[i].refs++; | 100 c[i].refs++; |
101 ngx_mutex_unlock(&cache->mutex); | |
100 return &c[i]; | 102 return &c[i]; |
101 } | 103 } |
102 } | 104 } |
105 | |
106 ngx_mutex_unlock(&cache->mutex); | |
103 | 107 |
104 return NULL; | 108 return NULL; |
105 } | 109 } |
106 | 110 |
107 | 111 |
111 { | 115 { |
112 time_t old; | 116 time_t old; |
113 ngx_uint_t i; | 117 ngx_uint_t i; |
114 ngx_http_cache_t *c, *found; | 118 ngx_http_cache_t *c, *found; |
115 | 119 |
116 old = ngx_time() + 1; | 120 old = ngx_cached_time + 1; |
117 found = NULL; | 121 found = NULL; |
118 | 122 |
119 c = cache->elts | 123 c = cache->elts + crc % cache->hash * cache->nelts; |
120 + crc % cache->hash * cache->nelts * sizeof(ngx_http_cache_t); | 124 |
125 ngx_mutex_lock(&cache->mutex); | |
121 | 126 |
122 for (i = 0; i < cache->nelts; i++) { | 127 for (i = 0; i < cache->nelts; i++) { |
128 if (c[i].refs > 0) { | |
129 /* a busy entry */ | |
130 continue; | |
131 } | |
132 | |
123 if (c[i].key.data == NULL) { | 133 if (c[i].key.data == NULL) { |
124 | |
125 /* a free entry is found */ | 134 /* a free entry is found */ |
126 | |
127 found = &c[i]; | 135 found = &c[i]; |
128 break; | 136 break; |
129 } | 137 } |
130 | 138 |
131 /* looking for the oldest cache entry that is not been using */ | 139 /* looking for the oldest cache entry */ |
132 | 140 |
133 if (c[i].refs == 0 && old > c[i].accessed) { | 141 if (old > c[i].accessed) { |
134 | 142 |
135 old = c[i].accessed; | 143 old = c[i].accessed; |
136 found = &c[i]; | 144 found = &c[i]; |
137 } | 145 } |
138 } | 146 } |
146 } | 154 } |
147 | 155 |
148 if (found->key.data == NULL) { | 156 if (found->key.data == NULL) { |
149 found->key.data = ngx_alloc(key->len, log); | 157 found->key.data = ngx_alloc(key->len, log); |
150 if (found->key.data == NULL) { | 158 if (found->key.data == NULL) { |
159 ngx_mutex_unlock(&cache->mutex); | |
151 return NULL; | 160 return NULL; |
152 } | 161 } |
153 } | 162 } |
154 | 163 |
155 ngx_memcpy(found->key.data, key->data, key->len); | 164 ngx_memcpy(found->key.data, key->data, key->len); |
156 | 165 |
157 found->crc = crc; | 166 found->crc = crc; |
158 found->key.len = key->len; | 167 found->key.len = key->len; |
159 found->refs = 1; | 168 found->refs = 1; |
169 found->count = 0; | |
160 found->deleted = 0; | 170 found->deleted = 0; |
161 } | 171 } |
172 | |
173 ngx_mutex_unlock(&cache->mutex); | |
162 | 174 |
163 return found; | 175 return found; |
164 } | 176 } |
165 | 177 |
166 | 178 |
378 { | 390 { |
379 ngx_http_cache_conf_t *prev = parent; | 391 ngx_http_cache_conf_t *prev = parent; |
380 ngx_http_cache_conf_t *conf = child; | 392 ngx_http_cache_conf_t *conf = child; |
381 | 393 |
382 if (conf->open_files == NULL) { | 394 if (conf->open_files == NULL) { |
395 conf->open_files = prev->open_files; | |
396 } | |
397 | |
398 #if 0 | |
399 if (conf->open_files == NULL) { | |
383 if (prev->open_files) { | 400 if (prev->open_files) { |
384 conf->open_files = prev->open_files; | 401 conf->open_files = prev->open_files; |
385 | 402 |
386 } else { | 403 } else { |
387 conf->open_files = ngx_pcalloc(cf->pool, | 404 conf->open_files = ngx_pcalloc(cf->pool, |
400 if (conf->open_files->elts == NULL) { | 417 if (conf->open_files->elts == NULL) { |
401 return NGX_CONF_ERROR; | 418 return NGX_CONF_ERROR; |
402 } | 419 } |
403 } | 420 } |
404 } | 421 } |
422 #endif | |
405 | 423 |
406 return NGX_CONF_OK; | 424 return NGX_CONF_OK; |
407 } | 425 } |
426 | |
427 | |
428 char *ngx_http_set_cache_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
429 { | |
430 char *p = conf; | |
431 | |
432 ngx_int_t i, dup, invalid; | |
433 ngx_str_t *value, line; | |
434 ngx_http_cache_hash_t *ch, **chp; | |
435 | |
436 chp = (ngx_http_cache_hash_t **) (p + cmd->offset); | |
437 if (*chp) { | |
438 return "is duplicate"; | |
439 } | |
440 | |
441 if (!(ch = ngx_pcalloc(cf->pool, sizeof(ngx_http_cache_hash_t)))) { | |
442 return NGX_CONF_ERROR; | |
443 } | |
444 *chp = ch; | |
445 | |
446 dup = 0; | |
447 invalid = 0; | |
448 | |
449 value = cf->args->elts; | |
450 | |
451 for (i = 1; i < cf->args->nelts; i++) { | |
452 | |
453 if (value[i].data[1] != '=') { | |
454 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
455 "invalid value \"%s\"", value[i].data); | |
456 return NGX_CONF_ERROR; | |
457 } | |
458 | |
459 switch (value[i].data[0]) { | |
460 | |
461 case 'h': | |
462 if (ch->hash) { | |
463 dup = 1; | |
464 break; | |
465 } | |
466 | |
467 ch->hash = ngx_atoi(value[i].data + 2, value[i].len - 2); | |
468 if (ch->hash == (size_t) NGX_ERROR || ch->hash == 0) { | |
469 invalid = 1; | |
470 break; | |
471 } | |
472 | |
473 continue; | |
474 | |
475 case 'n': | |
476 if (ch->nelts) { | |
477 dup = 1; | |
478 break; | |
479 } | |
480 | |
481 ch->nelts = ngx_atoi(value[i].data + 2, value[i].len - 2); | |
482 if (ch->nelts == (size_t) NGX_ERROR || ch->nelts == 0) { | |
483 invalid = 1; | |
484 break; | |
485 } | |
486 | |
487 continue; | |
488 | |
489 case 'l': | |
490 if (ch->life) { | |
491 dup = 1; | |
492 break; | |
493 } | |
494 | |
495 line.len = value[i].len - 2; | |
496 line.data = value[i].data + 2; | |
497 | |
498 ch->life = ngx_parse_time(&line, 1); | |
499 if (ch->life == NGX_ERROR || ch->life == 0) { | |
500 invalid = 1; | |
501 break; | |
502 } | |
503 | |
504 continue; | |
505 | |
506 case 'u': | |
507 if (ch->update) { | |
508 dup = 1; | |
509 break; | |
510 } | |
511 | |
512 line.len = value[i].len - 2; | |
513 line.data = value[i].data + 2; | |
514 | |
515 ch->update = ngx_parse_time(&line, 1); | |
516 if (ch->update == NGX_ERROR || ch->update == 0) { | |
517 invalid = 1; | |
518 break; | |
519 } | |
520 | |
521 continue; | |
522 | |
523 default: | |
524 invalid = 1; | |
525 } | |
526 | |
527 if (dup) { | |
528 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
529 "duplicate value \"%s\"", value[i].data); | |
530 return NGX_CONF_ERROR; | |
531 } | |
532 | |
533 if (invalid) { | |
534 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
535 "invalid value \"%s\"", value[i].data); | |
536 return NGX_CONF_ERROR; | |
537 } | |
538 } | |
539 | |
540 ch->elts = ngx_pcalloc(cf->pool, | |
541 ch->hash * ch->nelts * sizeof(ngx_http_cache_t)); | |
542 if (ch->elts == NULL) { | |
543 return NGX_CONF_ERROR; | |
544 } | |
545 | |
546 return NGX_CONF_OK; | |
547 } |