Skip to content

Commit 62606a0

Browse files
committed
implemented builtin nginx variable $srcache_expire for automatic expiration time calculation; also added new directives srcache_max_expire and srcache_default_expire.
1 parent ec6cd95 commit 62606a0

File tree

5 files changed

+637
-7
lines changed

5 files changed

+637
-7
lines changed

src/ngx_http_srcache_filter_module.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,22 @@ static ngx_command_t ngx_http_srcache_commands[] = {
185185
offsetof(ngx_http_srcache_loc_conf_t, header_buf_size),
186186
NULL },
187187

188+
{ ngx_string("srcache_max_expire"),
189+
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF
190+
|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
191+
ngx_conf_set_sec_slot,
192+
NGX_HTTP_LOC_CONF_OFFSET,
193+
offsetof(ngx_http_srcache_loc_conf_t, max_expire),
194+
NULL },
195+
196+
{ ngx_string("srcache_default_expire"),
197+
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF
198+
|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
199+
ngx_conf_set_sec_slot,
200+
NGX_HTTP_LOC_CONF_OFFSET,
201+
offsetof(ngx_http_srcache_loc_conf_t, default_expire),
202+
NULL },
203+
188204
ngx_null_command
189205
};
190206

@@ -664,6 +680,9 @@ ngx_http_srcache_create_loc_conf(ngx_conf_t *cf)
664680
conf->store_no_store = NGX_CONF_UNSET;
665681
conf->store_no_cache = NGX_CONF_UNSET;
666682

683+
conf->max_expire = NGX_CONF_UNSET;
684+
conf->default_expire = NGX_CONF_UNSET;
685+
667686
conf->ignore_content_encoding = NGX_CONF_UNSET;
668687

669688
conf->hide_headers = NGX_CONF_UNSET_PTR;
@@ -712,6 +731,9 @@ ngx_http_srcache_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
712731
ngx_conf_merge_value(conf->store_no_store, prev->store_no_store, 0);
713732
ngx_conf_merge_value(conf->store_no_cache, prev->store_no_cache, 0);
714733

734+
ngx_conf_merge_value(conf->max_expire, prev->max_expire, 0);
735+
ngx_conf_merge_value(conf->default_expire, prev->default_expire, 60);
736+
715737
ngx_conf_merge_value(conf->ignore_content_encoding,
716738
prev->ignore_content_encoding, 0);
717739

src/ngx_http_srcache_filter_module.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ typedef struct {
5454
ngx_array_t *pass_headers;
5555

5656
unsigned hide_content_type;
57+
time_t max_expire;
58+
time_t default_expire;
5759

5860
unsigned postponed_to_access_phase_end;
5961
} ngx_http_srcache_loc_conf_t;

src/ngx_http_srcache_util.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,8 @@ ngx_http_srcache_store_response_header(ngx_http_request_t *r,
945945
dd("header key: %.*s", (int) header[i].key.len, header[i].key.data);
946946

947947
dd("header hash: %lu, hash lc: %lu", (unsigned long) header[i].hash,
948-
(unsigned long) ngx_hash_key_lc(header[i].key.data, header[i].key.len));
948+
(unsigned long) ngx_hash_key_lc(header[i].key.data,
949+
header[i].key.len));
949950

950951
if (ngx_hash_find(&conf->hide_headers_hash, header[i].hash,
951952
header[i].lowcase_key, header[i].key.len))

src/ngx_http_srcache_var.c

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ static ngx_http_variable_t ngx_http_srcache_variables[] = {
1515

1616
{ ngx_string("srcache_expire"), NULL,
1717
ngx_http_srcache_expire_variable, 0,
18-
0, 0 },
18+
NGX_HTTP_VAR_NOCACHEABLE, 0 },
1919

2020
{ ngx_null_string, NULL, NULL, 0, 0, 0 }
2121
};
@@ -25,27 +25,42 @@ static ngx_int_t
2525
ngx_http_srcache_expire_variable(ngx_http_request_t *r,
2626
ngx_http_variable_value_t *v, uintptr_t data)
2727
{
28-
ngx_http_srcache_ctx_t *ctx;
29-
u_char *p;
28+
ngx_http_srcache_ctx_t *ctx;
29+
u_char *p;
30+
time_t expire;
31+
ngx_http_srcache_loc_conf_t *conf;
32+
33+
conf = ngx_http_get_module_loc_conf(r, ngx_http_srcache_filter_module);
3034

3135
v->valid = 1;
32-
v->no_cacheable = 0;
36+
v->no_cacheable = 1;
3337
v->not_found = 0;
3438

3539
ctx = ngx_http_get_module_ctx(r, ngx_http_srcache_filter_module);
3640

37-
if (ctx->valid_sec == 0) {
41+
if (!ctx || !ctx->store_response) {
3842
v->not_found = 1;
3943
return NGX_OK;
4044
}
4145

46+
if (ctx->valid_sec == 0) {
47+
expire = conf->default_expire;
48+
49+
} else {
50+
expire = ctx->valid_sec - ngx_time();
51+
}
52+
53+
if (conf->max_expire > 0 && expire > conf->max_expire) {
54+
expire = conf->max_expire;
55+
}
56+
4257
p = ngx_palloc(r->pool, NGX_TIME_T_LEN);
4358
if (p == NULL) {
4459
return NGX_ERROR;
4560
}
4661

4762
v->data = p;
48-
p = ngx_sprintf(p, "%T", ctx->valid_sec - ngx_time());
63+
p = ngx_sprintf(p, "%T", expire);
4964
v->len = p - v->data;
5065

5166
return NGX_OK;

0 commit comments

Comments
 (0)