blob: fb446e471662e983e244294d8cb267fdfea0caf8 [file] [log] [blame]
Igor Sysoevd90282d2004-09-28 08:34:51 +00001
2/*
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#ifndef _NGX_HTTP_CACHE_H_INCLUDED_
8#define _NGX_HTTP_CACHE_H_INCLUDED_
9
10
11#include <ngx_config.h>
12#include <ngx_core.h>
Igor Sysoevf60b1a52003-11-04 17:09:19 +000013#include <ngx_http.h>
Igor Sysoev65977492003-11-02 22:56:18 +000014
15
Igor Sysoev865c1502003-11-30 20:03:18 +000016/*
Igor Sysoevf6906042004-11-25 16:17:31 +000017 * The 3 bits allows the 7 uses before the cache entry allocation.
Igor Sysoev865c1502003-11-30 20:03:18 +000018 * We can use maximum 7 bits, i.e up to the 127 uses.
19 */
20#define NGX_HTTP_CACHE_LAZY_ALLOCATION_BITS 3
21
Igor Sysoevf6906042004-11-25 16:17:31 +000022
Igor Sysoev65977492003-11-02 22:56:18 +000023typedef struct {
Igor Sysoev865c1502003-11-30 20:03:18 +000024 uint32_t crc;
25 ngx_str_t key;
26 time_t accessed;
Igor Sysoev0e499db2003-11-27 07:45:22 +000027
Igor Sysoev865c1502003-11-30 20:03:18 +000028 unsigned refs:20; /* 1048576 references */
Igor Sysoev0e499db2003-11-27 07:45:22 +000029
Igor Sysoev865c1502003-11-30 20:03:18 +000030 unsigned count:NGX_HTTP_CACHE_LAZY_ALLOCATION_BITS;
31
Igor Sysoev865c1502003-11-30 20:03:18 +000032 unsigned deleted:1;
Igor Sysoev4fc368f2003-12-01 16:28:14 +000033 unsigned expired:1;
Igor Sysoev865c1502003-11-30 20:03:18 +000034 unsigned memory:1;
35 unsigned mmap:1;
36 unsigned notify:1;
37
38 ngx_fd_t fd;
39#if (NGX_USE_HTTP_FILE_CACHE_UNIQ)
40 ngx_file_uniq_t uniq; /* no needed with kqueue */
41#endif
42 time_t last_modified;
43 time_t updated;
Igor Sysoev0e499db2003-11-27 07:45:22 +000044
45 union {
Igor Sysoev865c1502003-11-30 20:03:18 +000046 off_t size;
47 ngx_str_t value;
Igor Sysoev0e499db2003-11-27 07:45:22 +000048 } data;
Igor Sysoevf6906042004-11-25 16:17:31 +000049} ngx_http_cache_entry_t;
Igor Sysoev0e499db2003-11-27 07:45:22 +000050
51
52typedef struct {
Igor Sysoev65977492003-11-02 22:56:18 +000053 time_t expires;
54 time_t last_modified;
Igor Sysoevcf80a702003-11-03 22:20:44 +000055 time_t date;
Igor Sysoev65977492003-11-02 22:56:18 +000056 off_t length;
Igor Sysoev9cc1ace2003-11-04 22:12:39 +000057 size_t key_len;
Igor Sysoev74e95c22003-11-09 20:03:38 +000058 char key[1];
Igor Sysoev65977492003-11-02 22:56:18 +000059} ngx_http_cache_header_t;
60
61
Igor Sysoev0e499db2003-11-27 07:45:22 +000062#define NGX_HTTP_CACHE_HASH 7
Igor Sysoeva8fa0a62003-11-25 20:44:56 +000063#define NGX_HTTP_CACHE_NELTS 4
Igor Sysoev65977492003-11-02 22:56:18 +000064
65typedef struct {
Igor Sysoevf6906042004-11-25 16:17:31 +000066 ngx_http_cache_entry_t *elts;
Igor Sysoeva8fa0a62003-11-25 20:44:56 +000067 size_t hash;
68 size_t nelts;
Igor Sysoev877df632003-11-28 08:40:40 +000069 time_t life;
70 time_t update;
Igor Sysoevb54698b2004-02-23 20:57:12 +000071#if (NGX_THREADS)
72 ngx_mutex_t mutex;
73#endif
Igor Sysoeva8fa0a62003-11-25 20:44:56 +000074 ngx_pool_t *pool;
75} ngx_http_cache_hash_t;
76
77
78typedef struct {
79 ngx_http_cache_hash_t *hash;
Igor Sysoevf6906042004-11-25 16:17:31 +000080 ngx_http_cache_entry_t *cache;
Igor Sysoeva8fa0a62003-11-25 20:44:56 +000081 ngx_file_t file;
Igor Sysoevf6906042004-11-25 16:17:31 +000082 ngx_array_t key;
Igor Sysoeva8fa0a62003-11-25 20:44:56 +000083 uint32_t crc;
84 u_char md5[16];
85 ngx_path_t *path;
Igor Sysoev369145c2004-05-28 15:49:23 +000086 ngx_buf_t *buf;
Igor Sysoeva8fa0a62003-11-25 20:44:56 +000087 time_t expires;
88 time_t last_modified;
89 time_t date;
90 off_t length;
Igor Sysoevb1dfe472004-12-21 12:30:30 +000091 size_t key_len;
Igor Sysoeva8fa0a62003-11-25 20:44:56 +000092 size_t file_start;
Igor Sysoevb1dfe472004-12-21 12:30:30 +000093 ngx_file_uniq_t uniq;
Igor Sysoeva8fa0a62003-11-25 20:44:56 +000094 ngx_log_t *log;
Igor Sysoevf6906042004-11-25 16:17:31 +000095
96 /* STUB */
Igor Sysoevb1dfe472004-12-21 12:30:30 +000097 ssize_t header_size;
Igor Sysoevf6906042004-11-25 16:17:31 +000098 ngx_str_t key0;
99} ngx_http_cache_t;
Igor Sysoev65977492003-11-02 22:56:18 +0000100
101
Igor Sysoev42b12b32004-12-02 18:40:46 +0000102typedef struct {
Igor Sysoevb1dfe472004-12-21 12:30:30 +0000103 ngx_path_t *path;
Igor Sysoev42b12b32004-12-02 18:40:46 +0000104 ngx_str_t key;
Igor Sysoevb1dfe472004-12-21 12:30:30 +0000105 ngx_buf_t *buf;
106
107 unsigned file:1;
108 unsigned memory:1;
109 unsigned primary:1;
Igor Sysoev42b12b32004-12-02 18:40:46 +0000110} ngx_http_cache_ctx_t;
111
Igor Sysoev877df632003-11-28 08:40:40 +0000112
Igor Sysoev74e95c22003-11-09 20:03:38 +0000113#define NGX_HTTP_CACHE_STALE 1
114#define NGX_HTTP_CACHE_AGED 2
115#define NGX_HTTP_CACHE_THE_SAME 3
Igor Sysoeva1512b12003-11-03 17:33:31 +0000116
117
Igor Sysoevb1dfe472004-12-21 12:30:30 +0000118ngx_int_t ngx_http_cache_get(ngx_http_request_t *r, ngx_http_cache_ctx_t *ctx);
119
120ngx_int_t ngx_http_file_cache_get(ngx_http_request_t *r,
121 ngx_http_cache_ctx_t *ctx);
122
123ngx_int_t ngx_http_file_cache_open(ngx_http_cache_t *c);
124
125ngx_int_t ngx_http_cache_cleaner_handler(ngx_gc_t *gc, ngx_str_t *name,
126 ngx_dir_t *dir);
127
128
Igor Sysoevf6906042004-11-25 16:17:31 +0000129#if 0
130
Igor Sysoev865c1502003-11-30 20:03:18 +0000131ngx_http_cache_t *ngx_http_cache_get(ngx_http_cache_hash_t *cache,
132 ngx_http_cleanup_t *cleanup,
133 ngx_str_t *key, uint32_t *crc);
134
135ngx_http_cache_t *ngx_http_cache_alloc(ngx_http_cache_hash_t *hash,
136 ngx_http_cache_t *cache,
137 ngx_http_cleanup_t *cleanup,
138 ngx_str_t *key, uint32_t crc,
139 ngx_str_t *value, ngx_log_t *log);
140void ngx_http_cache_free(ngx_http_cache_t *cache,
141 ngx_str_t *key, ngx_str_t *value, ngx_log_t *log);
Igor Sysoeva7dcbaf2003-12-02 05:47:29 +0000142void ngx_http_cache_lock(ngx_http_cache_hash_t *hash, ngx_http_cache_t *cache);
Igor Sysoev865c1502003-11-30 20:03:18 +0000143void ngx_http_cache_unlock(ngx_http_cache_hash_t *hash,
144 ngx_http_cache_t *cache, ngx_log_t *log);
145
Igor Sysoev65977492003-11-02 22:56:18 +0000146int ngx_http_cache_update_file(ngx_http_request_t *r,ngx_http_cache_ctx_t *ctx,
147 ngx_str_t *temp_file);
148
Igor Sysoev865c1502003-11-30 20:03:18 +0000149int ngx_http_send_cached(ngx_http_request_t *r);
Igor Sysoevc2bba092003-11-28 17:41:47 +0000150
Igor Sysoev0e499db2003-11-27 07:45:22 +0000151
Igor Sysoev877df632003-11-28 08:40:40 +0000152char *ngx_http_set_cache_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
153
Igor Sysoevf6906042004-11-25 16:17:31 +0000154#endif
155
Igor Sysoev877df632003-11-28 08:40:40 +0000156
Igor Sysoev65977492003-11-02 22:56:18 +0000157#endif /* _NGX_HTTP_CACHE_H_INCLUDED_ */