Skip to content

Commit 4156994

Browse files
committed
feature: added new nginx variable $srcache_store_status which takes the value "BYASS" or "STORE".
1 parent 5e5eb74 commit 4156994

File tree

5 files changed

+79
-3
lines changed

5 files changed

+79
-3
lines changed

src/ngx_http_srcache_filter_module.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ enum {
1414
};
1515

1616

17+
enum {
18+
NGX_HTTP_SRCACHE_STORE_BYPASS = 0,
19+
NGX_HTTP_SRCACHE_STORE_STORE = 1
20+
};
21+
22+
1723
extern ngx_module_t ngx_http_srcache_filter_module;
1824

1925

src/ngx_http_srcache_var.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ static ngx_int_t ngx_http_srcache_expire_variable(ngx_http_request_t *r,
1111
ngx_http_variable_value_t *v, uintptr_t data);
1212
static ngx_int_t ngx_http_srcache_fetch_status_variable(ngx_http_request_t *r,
1313
ngx_http_variable_value_t *v, uintptr_t data);
14+
static ngx_int_t ngx_http_srcache_store_status_variable(ngx_http_request_t *r,
15+
ngx_http_variable_value_t *v, uintptr_t data);
1416

1517

1618
static ngx_str_t ngx_http_srcache_fetch_status[] = {
@@ -20,6 +22,12 @@ static ngx_str_t ngx_http_srcache_fetch_status[] = {
2022
};
2123

2224

25+
static ngx_str_t ngx_http_srcache_store_status[] = {
26+
ngx_string("BYPASS"),
27+
ngx_string("STORE"),
28+
};
29+
30+
2331
static ngx_http_variable_t ngx_http_srcache_variables[] = {
2432

2533
{ ngx_string("srcache_expire"), NULL,
@@ -30,6 +38,10 @@ static ngx_http_variable_t ngx_http_srcache_variables[] = {
3038
ngx_http_srcache_fetch_status_variable, 0,
3139
NGX_HTTP_VAR_NOCACHEABLE, 0 },
3240

41+
{ ngx_string("srcache_store_status"), NULL,
42+
ngx_http_srcache_store_status_variable, 0,
43+
NGX_HTTP_VAR_NOCACHEABLE, 0 },
44+
3345
{ ngx_null_string, NULL, NULL, 0, 0, 0 }
3446
};
3547

@@ -113,6 +125,34 @@ ngx_http_srcache_fetch_status_variable(ngx_http_request_t *r,
113125
}
114126

115127

128+
static ngx_int_t
129+
ngx_http_srcache_store_status_variable(ngx_http_request_t *r,
130+
ngx_http_variable_value_t *v, uintptr_t data)
131+
{
132+
ngx_uint_t status;
133+
ngx_http_srcache_ctx_t *ctx;
134+
135+
ctx = ngx_http_get_module_ctx(r, ngx_http_srcache_filter_module);
136+
137+
if (ctx && ctx->store_response) {
138+
status = NGX_HTTP_SRCACHE_STORE_STORE;
139+
140+
} else {
141+
status = NGX_HTTP_SRCACHE_STORE_BYPASS;
142+
143+
}
144+
145+
v->valid = 1;
146+
v->no_cacheable = 1;
147+
v->not_found = 0;
148+
149+
v->len = ngx_http_srcache_store_status[status].len;
150+
v->data = ngx_http_srcache_store_status[status].data;
151+
152+
return NGX_OK;
153+
}
154+
155+
116156
ngx_int_t
117157
ngx_http_srcache_add_variables(ngx_conf_t *cf)
118158
{

t/main-req.t

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use Test::Nginx::Socket;
55

66
#repeat_each(2);
77

8-
plan tests => repeat_each() * (5 * blocks());
8+
plan tests => repeat_each() * (5 * blocks() + 3);
99

1010
$ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211;
1111

@@ -22,11 +22,13 @@ __DATA__
2222
set $memc_cmd 'flush_all';
2323
memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT;
2424
add_header X-Fetch-Status $srcache_fetch_status;
25+
add_header X-Store-Status $srcache_store_status;
2526
}
2627
--- response_headers
2728
Content-Type: text/plain
2829
Content-Length: 4
2930
X-Fetch-Status: BYPASS
31+
X-Store-Status: BYPASS
3032
--- request
3133
GET /flush
3234
--- response_body eval: "OK\r\n"
@@ -42,6 +44,7 @@ GET /flush
4244
4345
echo hello;
4446
add_header X-Fetch-Status $srcache_fetch_status;
47+
add_header X-Store-Status $srcache_store_status;
4548
}
4649
4750
location /memc {
@@ -57,6 +60,7 @@ GET /foo
5760
Content-Type: text/css
5861
Content-Length:
5962
X-Fetch-Status: MISS
63+
X-Store-Status: STORE
6064
--- response_body
6165
hello
6266
@@ -71,6 +75,7 @@ hello
7175
7276
echo world;
7377
add_header X-Fetch-Status $srcache_fetch_status;
78+
add_header X-Store-Status $srcache_store_status;
7479
}
7580
7681
location /memc {
@@ -86,6 +91,7 @@ GET /foo
8691
Content-Type: text/css
8792
Content-Length: 6
8893
X-Fetch-Status: HIT
94+
X-Store-Status: BYPASS
8995
--- response_body
9096
hello
9197

t/store-max-size.t

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use Test::Nginx::Socket;
55

66
#repeat_each(2);
77

8-
plan tests => repeat_each() * 2 * blocks();
8+
plan tests => repeat_each() * (2 * blocks() + 5);
99

1010
$ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211;
1111

@@ -35,6 +35,7 @@ GET /flush
3535
srcache_store_max_size 49;
3636
3737
echo hello;
38+
add_header X-Store-Status $srcache_store_status;
3839
}
3940
4041
location /memc {
@@ -49,6 +50,8 @@ GET /flush
4950
GET /foo
5051
--- response_body
5152
hello
53+
--- response_headers
54+
X-Store-Status: STORE
5255
5356
5457
@@ -90,6 +93,7 @@ GET /flush
9093
srcache_store_max_size 50;
9194
9295
echo hello;
96+
add_header X-Store-Status $srcache_store_status;
9397
}
9498
9599
location /memc {
@@ -104,6 +108,8 @@ GET /flush
104108
GET /foo
105109
--- response_body
106110
hello
111+
--- response_headers
112+
X-Store-Status: STORE
107113
108114
109115
@@ -145,6 +151,8 @@ GET /flush
145151
srcache_store_max_size 48;
146152
147153
echo hello;
154+
add_header X-Store-Status $srcache_store_status;
155+
log_by_lua 'ngx.log(ngx.WARN, "store status: ", ngx.var.srcache_store_status)';
148156
}
149157
150158
location /memc {
@@ -159,6 +167,10 @@ GET /flush
159167
GET /foo
160168
--- response_body
161169
hello
170+
--- response_headers
171+
X-Store-Status: STORE
172+
--- error_log
173+
store status: BYPASS
162174
163175
164176
@@ -199,6 +211,7 @@ GET /flush
199211
ngx.header.content_length = 40;
200212
ngx.say("hello")
201213
';
214+
add_header X-Store-Status $srcache_store_status;
202215
}
203216
204217
location /memc {
@@ -213,6 +226,8 @@ GET /flush
213226
GET /foo.txt
214227
--- response_body
215228
hello
229+
--- response_headers
230+
X-Store-Status: BYPASS
216231
217232
218233

t/store-skip.t

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use Test::Nginx::Socket;
55

66
#repeat_each(2);
77

8-
plan tests => repeat_each() * (2 * blocks() + 3);
8+
plan tests => repeat_each() * (2 * blocks() + 6);
99

1010
$ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211;
1111

@@ -36,6 +36,7 @@ GET /flush
3636
srcache_store_skip $skip;
3737
3838
echo hello;
39+
add_header X-Store-Status $srcache_store_status;
3940
}
4041
4142
location /memc {
@@ -50,6 +51,8 @@ GET /flush
5051
GET /foo
5152
--- response_body
5253
hello
54+
--- response_headers
55+
X-Store-Status: STORE
5356
5457
5558
@@ -89,6 +92,7 @@ GET /flush
8992
default_type text/css;
9093
srcache_store PUT /memc $uri;
9194
srcache_store_skip 0;
95+
add_header X-Store-Status $srcache_store_status;
9296
9397
echo hello;
9498
}
@@ -105,6 +109,8 @@ GET /flush
105109
GET /foo
106110
--- response_body
107111
hello
112+
--- response_headers
113+
X-Store-Status: STORE
108114
109115
110116
@@ -146,6 +152,7 @@ GET /flush
146152
srcache_store_skip 1;
147153
148154
echo hello;
155+
add_header X-Store-Status $srcache_store_status;
149156
}
150157
151158
location /memc {
@@ -160,6 +167,8 @@ GET /flush
160167
GET /foo
161168
--- response_body
162169
hello
170+
--- response_headers
171+
X-Store-Status: BYPASS
163172
164173
165174

0 commit comments

Comments
 (0)