1
2/*
3 * Copyright (C) Igor Sysoev
4 * Copyright (C) Nginx, Inc.
5 */
6
7
8#ifndef _NGX_HTTP_CACHE_H_INCLUDED_
9#define _NGX_HTTP_CACHE_H_INCLUDED_
10
11
12#include <ngx_config.h>
13#include <ngx_core.h>
14#include <ngx_http.h>
15
16
17#define NGX_HTTP_CACHE_MISS          1
18#define NGX_HTTP_CACHE_BYPASS        2
19#define NGX_HTTP_CACHE_EXPIRED       3
20#define NGX_HTTP_CACHE_STALE         4
21#define NGX_HTTP_CACHE_UPDATING      5
22#define NGX_HTTP_CACHE_REVALIDATED   6
23#define NGX_HTTP_CACHE_HIT           7
24#define NGX_HTTP_CACHE_SCARCE        8
25
26#define NGX_HTTP_CACHE_KEY_LEN       16
27#define NGX_HTTP_CACHE_ETAG_LEN      128
28#define NGX_HTTP_CACHE_VARY_LEN      128
29
30#define NGX_HTTP_CACHE_VERSION       5
31
32
33typedef struct {
34    ngx_uint_t                       status;
35    time_t                           valid;
36} ngx_http_cache_valid_t;
37
38
39typedef struct {
40    ngx_rbtree_node_t                node;
41    ngx_queue_t                      queue;
42
43    u_char                           key[NGX_HTTP_CACHE_KEY_LEN
44                                         - sizeof(ngx_rbtree_key_t)];
45
46    unsigned                         count:20;
47    unsigned                         uses:10;
48    unsigned                         valid_msec:10;
49    unsigned                         error:10;
50    unsigned                         exists:1;
51    unsigned                         updating:1;
52    unsigned                         deleting:1;
53    unsigned                         purged:1;
54                                     /* 10 unused bits */
55
56    ngx_file_uniq_t                  uniq;
57    time_t                           expire;
58    time_t                           valid_sec;
59    size_t                           body_start;
60    off_t                            fs_size;
61    ngx_msec_t                       lock_time;
62} ngx_http_file_cache_node_t;
63
64
65struct ngx_http_cache_s {
66    ngx_file_t                       file;
67    ngx_array_t                      keys;
68    uint32_t                         crc32;
69    u_char                           key[NGX_HTTP_CACHE_KEY_LEN];
70    u_char                           main[NGX_HTTP_CACHE_KEY_LEN];
71
72    ngx_file_uniq_t                  uniq;
73    time_t                           valid_sec;
74    time_t                           updating_sec;
75    time_t                           error_sec;
76    time_t                           last_modified;
77    time_t                           date;
78
79    ngx_str_t                        etag;
80    ngx_str_t                        vary;
81    u_char                           variant[NGX_HTTP_CACHE_KEY_LEN];
82
83    size_t                           header_start;
84    size_t                           body_start;
85    off_t                            length;
86    off_t                            fs_size;
87
88    ngx_uint_t                       min_uses;
89    ngx_uint_t                       error;
90    ngx_uint_t                       valid_msec;
91    ngx_uint_t                       vary_tag;
92
93    ngx_buf_t                       *buf;
94
95    ngx_http_file_cache_t           *file_cache;
96    ngx_http_file_cache_node_t      *node;
97
98#if (NGX_THREADS || NGX_COMPAT)
99    ngx_thread_task_t               *thread_task;
100#endif
101
102    ngx_msec_t                       lock_timeout;
103    ngx_msec_t                       lock_age;
104    ngx_msec_t                       lock_time;
105    ngx_msec_t                       wait_time;
106
107    ngx_event_t                      wait_event;
108
109    unsigned                         lock:1;
110    unsigned                         waiting:1;
111
112    unsigned                         updated:1;
113    unsigned                         updating:1;
114    unsigned                         exists:1;
115    unsigned                         temp_file:1;
116    unsigned                         purged:1;
117    unsigned                         reading:1;
118    unsigned                         secondary:1;
119    unsigned                         background:1;
120
121    unsigned                         stale_updating:1;
122    unsigned                         stale_error:1;
123};
124
125
126typedef struct {
127    ngx_uint_t                       version;
128    time_t                           valid_sec;
129    time_t                           updating_sec;
130    time_t                           error_sec;
131    time_t                           last_modified;
132    time_t                           date;
133    uint32_t                         crc32;
134    u_short                          valid_msec;
135    u_short                          header_start;
136    u_short                          body_start;
137    u_char                           etag_len;
138    u_char                           etag[NGX_HTTP_CACHE_ETAG_LEN];
139    u_char                           vary_len;
140    u_char                           vary[NGX_HTTP_CACHE_VARY_LEN];
141    u_char                           variant[NGX_HTTP_CACHE_KEY_LEN];
142} ngx_http_file_cache_header_t;
143
144
145typedef struct {
146    ngx_rbtree_t                     rbtree;
147    ngx_rbtree_node_t                sentinel;
148    ngx_queue_t                      queue;
149    ngx_atomic_t                     cold;
150    ngx_atomic_t                     loading;
151    off_t                            size;
152    ngx_uint_t                       count;
153    ngx_uint_t                       watermark;
154} ngx_http_file_cache_sh_t;
155
156
157struct ngx_http_file_cache_s {
158    ngx_http_file_cache_sh_t        *sh;
159    ngx_slab_pool_t                 *shpool;
160
161    ngx_path_t                      *path;
162
163    off_t                            max_size;
164    size_t                           bsize;
165
166    time_t                           inactive;
167
168    time_t                           fail_time;
169
170    ngx_uint_t                       files;
171    ngx_uint_t                       loader_files;
172    ngx_msec_t                       last;
173    ngx_msec_t                       loader_sleep;
174    ngx_msec_t                       loader_threshold;
175
176    ngx_uint_t                       manager_files;
177    ngx_msec_t                       manager_sleep;
178    ngx_msec_t                       manager_threshold;
179
180    ngx_shm_zone_t                  *shm_zone;
181
182    ngx_uint_t                       use_temp_path;
183                                     /* unsigned use_temp_path:1 */
184};
185
186
187ngx_int_t ngx_http_file_cache_new(ngx_http_request_t *r);
188ngx_int_t ngx_http_file_cache_create(ngx_http_request_t *r);
189void ngx_http_file_cache_create_key(ngx_http_request_t *r);
190ngx_int_t ngx_http_file_cache_open(ngx_http_request_t *r);
191ngx_int_t ngx_http_file_cache_set_header(ngx_http_request_t *r, u_char *buf);
192void ngx_http_file_cache_update(ngx_http_request_t *r, ngx_temp_file_t *tf);
193void ngx_http_file_cache_update_header(ngx_http_request_t *r);
194ngx_int_t ngx_http_cache_send(ngx_http_request_t *);
195void ngx_http_file_cache_free(ngx_http_cache_t *c, ngx_temp_file_t *tf);
196time_t ngx_http_file_cache_valid(ngx_array_t *cache_valid, ngx_uint_t status);
197
198char *ngx_http_file_cache_set_slot(ngx_conf_t *cf, ngx_command_t *cmd,
199    void *conf);
200char *ngx_http_file_cache_valid_set_slot(ngx_conf_t *cf, ngx_command_t *cmd,
201    void *conf);
202
203
204extern ngx_str_t  ngx_http_cache_status[];
205
206
207#endif /* _NGX_HTTP_CACHE_H_INCLUDED_ */
208