3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
14 static ngx_http_module_t ngx_http_cache_module_ctx = {
17 NULL, /* create main configuration */
18 NULL, /* init main configuration */
20 NULL, /* create server configuration */
21 NULL, /* merge server configuration */
23 NULL, /* create location configuration */
24 NULL /* merge location configuration */
28 ngx_module_t ngx_http_cache_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 */
40 static ngx_int_t ngx_http_cache_create(ngx_http_request_t *r)
44 if (!(r->cache = ngx_pcalloc(r->pool, sizeof(ngx_http_cache_t)))) {
48 if (ngx_array_init(&r->cache->key, r->pool, 5, sizeof(ngx_str_t))
54 /* preallocate the primary key */
56 if (!(key = ngx_array_push(&r->cache->key))) {
64 * we use offsetof() because sizeof() pads the struct size to the int size
67 r->cache->header_size = offsetof(ngx_http_cache_header_t, key);
69 r->cache->log = r->connection->log;
70 r->cache->file.log = r->connection->log;
76 ngx_int_t ngx_http_cache_get(ngx_http_request_t *r, ngx_http_cache_ctx_t *ctx)
81 if (r->cache == NULL) {
82 if (ngx_http_cache_create(r) == NGX_ERROR) {
92 c->header_size += ctx->key.len;
93 c->key_len += ctx->key.len;
97 if (key[0].len == 0) {
99 c->header_size += r->uri.len;
100 c->key_len += ctx->key.len;
103 if (!(key = ngx_array_push(&r->cache->key))) {
107 c->header_size += ctx->key.len;
108 c->key_len += ctx->key.len;
114 ngx_http_memory_cache_get(r, ctx);
120 return ngx_http_file_cache_get(r, ctx);
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)
137 *crc = ngx_crc(key->data, key->len);
139 c = hash->elts + *crc % hash->hash * hash->nelts;
141 if (ngx_mutex_lock(&hash->mutex) == NGX_ERROR) {
142 return (void *) NGX_ERROR;
145 for (i = 0; i < hash->nelts; i++) {
147 && c[i].key.len == key->len
148 && ngx_rstrncmp(c[i].key.data, key->data, key->len) == 0)
152 ngx_mutex_unlock(&hash->mutex);
153 return (void *) NGX_AGAIN;
159 if ((!(c[i].notify && (ngx_event_flags & NGX_USE_KQUEUE_EVENT)))
160 && (ngx_cached_time - c[i].updated >= hash->update))
165 ngx_mutex_unlock(&hash->mutex);
168 cleanup->data.cache.hash = hash;
169 cleanup->data.cache.cache = &c[i];
178 ngx_mutex_unlock(&hash->mutex);
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)
194 old = ngx_cached_time + 1;
196 c = hash->elts + crc % hash->hash * hash->nelts;
198 if (ngx_mutex_lock(&hash->mutex) == NGX_ERROR) {
199 return (void *) NGX_ERROR;
204 /* allocate a new entry */
206 for (i = 0; i < hash->nelts; i++) {
212 if (c[i].key.len == 0) {
213 /* a free entry is found */
218 /* looking for the oldest cache entry */
220 if (old > c[i].accessed) {
228 ngx_mutex_unlock(&hash->mutex);
232 ngx_http_cache_free(cache, key, value, log);
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);
243 cache->key.len = key->len;
244 ngx_memcpy(cache->key.data, key->data, key->len);
247 ngx_http_cache_free(cache, key, value, log);
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);
260 cache->data.value.len = value->len;
261 ngx_memcpy(cache->data.value.data, value->data, value->len);
265 cache->key.len = key->len;
277 cleanup->data.cache.hash = hash;
278 cleanup->data.cache.cache = cache;
283 ngx_mutex_unlock(&hash->mutex);
289 void ngx_http_cache_free(ngx_http_cache_t *cache,
290 ngx_str_t *key, ngx_str_t *value, ngx_log_t *log)
293 if (cache->data.value.data
294 && (value == NULL || value->len > cache->data.value.len))
296 ngx_free(cache->data.value.data);
297 cache->data.value.data = NULL;
303 cache->data.value.len = 0;
305 if (cache->fd != NGX_INVALID_FILE) {
307 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
308 "http cache close fd: %d", cache->fd);
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",
316 cache->fd = NGX_INVALID_FILE;
319 if (cache->key.data && (key == NULL || key->len > cache->key.len)) {
320 ngx_free(cache->key.data);
321 cache->key.data = NULL;
330 void ngx_http_cache_lock(ngx_http_cache_hash_t *hash, ngx_http_cache_t *cache)
332 if (ngx_mutex_lock(&hash->mutex) == NGX_ERROR) {
338 void ngx_http_cache_unlock(ngx_http_cache_hash_t *hash,
339 ngx_http_cache_t *cache, ngx_log_t *log)
341 if (ngx_mutex_lock(&hash->mutex) == NGX_ERROR) {
347 if (cache->refs == 0 && cache->deleted) {
348 ngx_http_cache_free(cache, NULL, NULL, log);
351 ngx_mutex_unlock(&hash->mutex);
357 ngx_http_cache_add_file_event(ngx_http_cache_hash_t *hash,
358 ngx_http_cache_t *cache)
361 ngx_http_cache_event_ctx_t *ctx;
363 ev = &ngx_cycle->read_events[fd];
364 ngx_memzero(ev, sizeof(ngx_event_t);
367 ev->event_handler = ngx_http_cache_invalidate;
369 return ngx_add_event(ev, NGX_VNODE_EVENT, 0);
373 void ngx_http_cache_invalidate(ngx_event_t *ev)
375 ngx_http_cache_event_ctx_t *ctx;
379 ngx_http_cache_lock(&ctx->hash->mutex);
381 if (ctx->cache->refs == 0)
382 ngx_http_cache_free(ctx->cache, NULL, NULL, ctx->log);
385 ctx->cache->deleted = 1;
388 ngx_http_cache_unlock(&ctx->hash->mutex);
394 /* TODO: currently fd only */
396 ngx_int_t ngx_http_send_cached(ngx_http_request_t *r)
401 ngx_http_log_ctx_t *ctx;
403 ctx = r->connection->log->data;
404 ctx->action = "sending response to client";
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;
410 if (ngx_http_set_content_type(r) != NGX_OK) {
411 return NGX_HTTP_INTERNAL_SERVER_ERROR;
414 /* we need to allocate all before the header would be sent */
416 if (!(h = ngx_pcalloc(r->pool, sizeof(ngx_hunk_t)))) {
417 return NGX_HTTP_INTERNAL_SERVER_ERROR;
420 if (!(h->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t)))) {
421 return NGX_HTTP_INTERNAL_SERVER_ERROR;
424 rc = ngx_http_send_header(r);
426 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
430 h->type = r->main ? NGX_HUNK_FILE : NGX_HUNK_FILE|NGX_HUNK_LAST;
433 h->file_last = r->cache->data.size;
435 h->file->fd = r->cache->fd;
436 h->file->log = r->connection->log;
441 return ngx_http_output_filter(r, &out);
445 char *ngx_http_set_cache_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
449 ngx_int_t i, j, dup, invalid;
450 ngx_str_t *value, line;
452 ngx_http_cache_hash_t *ch, **chp;
454 chp = (ngx_http_cache_hash_t **) (p + cmd->offset);
456 return "is duplicate";
459 if (!(ch = ngx_pcalloc(cf->pool, sizeof(ngx_http_cache_hash_t)))) {
460 return NGX_CONF_ERROR;
467 value = cf->args->elts;
469 for (i = 1; i < cf->args->nelts; i++) {
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;
477 switch (value[i].data[0]) {
485 ch->hash = ngx_atoi(value[i].data + 2, value[i].len - 2);
486 if (ch->hash == (size_t) NGX_ERROR || ch->hash == 0) {
499 ch->nelts = ngx_atoi(value[i].data + 2, value[i].len - 2);
500 if (ch->nelts == (size_t) NGX_ERROR || ch->nelts == 0) {
513 line.len = value[i].len - 2;
514 line.data = value[i].data + 2;
516 ch->life = ngx_parse_time(&line, 1);
517 if (ch->life == NGX_ERROR || ch->life == 0) {
530 line.len = value[i].len - 2;
531 line.data = value[i].data + 2;
533 ch->update = ngx_parse_time(&line, 1);
534 if (ch->update == NGX_ERROR || ch->update == 0) {
546 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
547 "duplicate value \"%s\"", value[i].data);
548 return NGX_CONF_ERROR;
552 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
553 "invalid value \"%s\"", value[i].data);
554 return NGX_CONF_ERROR;
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;
564 for (i = 0; i < (ngx_int_t) ch->hash; i++) {
565 c = ch->elts + i * ch->nelts;
567 for (j = 0; j < (ngx_int_t) ch->nelts; j++) {
568 c[j].fd = NGX_INVALID_FILE;