Skip to content

Commit ccf190d

Browse files
committed
feature: added new nginx variable $srcache_fetch_status which takes one of three values, "BYASS", "MISS", and "HIT". thanks Feibo Li for the patch.
1 parent 96ed47d commit ccf190d

8 files changed

+154
-7
lines changed

README.markdown

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
<!---
2+
Don't edit this file manually! Instead you should generate it by using:
3+
wiki2markdown.pl doc/HttpSRCacheModule.wiki
4+
-->
5+
16
Name
27
====
38

src/ngx_http_srcache_fetch.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ ngx_http_srcache_fetch_subrequest(ngx_http_request_t *r,
374374
ngx_http_set_ctx(sr, sr_ctx, ngx_http_srcache_filter_module);
375375

376376
ctx->fetch_sr = sr;
377+
ctx->issued_fetch_subrequest = 1;
377378

378379
return NGX_OK;
379380
}

src/ngx_http_srcache_filter_module.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -517,11 +517,12 @@ ngx_http_srcache_post_config(ngx_conf_t *cf)
517517
ngx_http_handler_pt *h;
518518
ngx_http_core_main_conf_t *cmcf;
519519

520+
rc = ngx_http_srcache_add_variables(cf);
521+
if (rc != NGX_OK) {
522+
return rc;
523+
}
524+
520525
if (ngx_http_srcache_used) {
521-
rc = ngx_http_srcache_add_variables(cf);
522-
if (rc != NGX_OK) {
523-
return rc;
524-
}
525526

526527
dd("using ngx-srcache");
527528

src/ngx_http_srcache_filter_module.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77
#include <nginx.h>
88

99

10+
enum {
11+
NGX_HTTP_SRCACHE_FETCH_BYPASS = 0,
12+
NGX_HTTP_SRCACHE_FETCH_MISS = 1,
13+
NGX_HTTP_SRCACHE_FETCH_HIT = 2
14+
};
15+
16+
1017
extern ngx_module_t ngx_http_srcache_filter_module;
1118

1219

@@ -107,6 +114,7 @@ struct ngx_http_srcache_ctx_s {
107114
unsigned parsing_cached_headers:1;
108115
unsigned store_response:1;
109116
unsigned store_skip:1;
117+
unsigned issued_fetch_subrequest:1;
110118
};
111119

112120

src/ngx_http_srcache_var.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@
99

1010
static ngx_int_t ngx_http_srcache_expire_variable(ngx_http_request_t *r,
1111
ngx_http_variable_value_t *v, uintptr_t data);
12+
static ngx_int_t ngx_http_srcache_fetch_status_variable(ngx_http_request_t *r,
13+
ngx_http_variable_value_t *v, uintptr_t data);
14+
15+
16+
static ngx_str_t ngx_http_srcache_fetch_status[] = {
17+
ngx_string("BYPASS"),
18+
ngx_string("MISS"),
19+
ngx_string("HIT")
20+
};
1221

1322

1423
static ngx_http_variable_t ngx_http_srcache_variables[] = {
@@ -17,6 +26,10 @@ static ngx_http_variable_t ngx_http_srcache_variables[] = {
1726
ngx_http_srcache_expire_variable, 0,
1827
NGX_HTTP_VAR_NOCACHEABLE, 0 },
1928

29+
{ ngx_string("srcache_fetch_status"), NULL,
30+
ngx_http_srcache_fetch_status_variable, 0,
31+
NGX_HTTP_VAR_NOCACHEABLE, 0 },
32+
2033
{ ngx_null_string, NULL, NULL, 0, 0, 0 }
2134
};
2235

@@ -67,6 +80,39 @@ ngx_http_srcache_expire_variable(ngx_http_request_t *r,
6780
}
6881

6982

83+
static ngx_int_t
84+
ngx_http_srcache_fetch_status_variable(ngx_http_request_t *r,
85+
ngx_http_variable_value_t *v, uintptr_t data)
86+
{
87+
ngx_uint_t status;
88+
ngx_http_srcache_ctx_t *ctx;
89+
90+
ctx = ngx_http_get_module_ctx(r, ngx_http_srcache_filter_module);
91+
92+
if (ctx == NULL) {
93+
status = NGX_HTTP_SRCACHE_FETCH_BYPASS;
94+
95+
} else if (ctx->from_cache) {
96+
status = NGX_HTTP_SRCACHE_FETCH_HIT;
97+
98+
} else if (ctx->issued_fetch_subrequest) {
99+
status = NGX_HTTP_SRCACHE_FETCH_MISS;
100+
101+
} else {
102+
status = NGX_HTTP_SRCACHE_FETCH_BYPASS;
103+
}
104+
105+
v->valid = 1;
106+
v->no_cacheable = 1;
107+
v->not_found = 0;
108+
109+
v->len = ngx_http_srcache_fetch_status[status].len;
110+
v->data = ngx_http_srcache_fetch_status[status].data;
111+
112+
return NGX_OK;
113+
}
114+
115+
70116
ngx_int_t
71117
ngx_http_srcache_add_variables(ngx_conf_t *cf)
72118
{

t/fetch-header.t

Lines changed: 15 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() * (4 * blocks() + 1);
8+
plan tests => repeat_each() * (5 * blocks() + 1);
99

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

@@ -21,10 +21,12 @@ __DATA__
2121
location /flush {
2222
set $memc_cmd 'flush_all';
2323
memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT;
24+
add_header X-Fetch-Status $srcache_fetch_status;
2425
}
2526
--- response_headers
2627
Content-Type: text/plain
2728
Content-Length: 4
29+
X-Fetch-Status: BYPASS
2830
--- request
2931
GET /flush
3032
--- response_body eval: "OK\r\n"
@@ -40,6 +42,7 @@ GET /flush
4042
--- response_headers
4143
Content-Type: text/plain
4244
Content-Length: 8
45+
!X-Fetch-Status
4346
--- request eval
4447
"PUT /memc
4548
HTTP/1.1 200 OK\r
@@ -61,6 +64,7 @@ hello
6164
srcache_store PUT /memc $uri;
6265
6366
echo world;
67+
add_header X-Fetch-Status $srcache_fetch_status;
6468
}
6569
6670
location /memc {
@@ -76,6 +80,7 @@ GET /foo
7680
Content-Type: foo/bar
7781
Content-Length: 5
7882
Foo: Bar
83+
X-Fetch-Status: HIT
7984
--- response_body chop
8085
hello
8186
@@ -90,6 +95,7 @@ hello
9095
--- response_headers
9196
Content-Type: text/plain
9297
Content-Length: 8
98+
!X-Fetch-Status
9399
--- request eval
94100
"PUT /memc
95101
HTTP 200 OK\r
@@ -111,6 +117,7 @@ hello
111117
srcache_store PUT /memc $uri;
112118
113119
echo world;
120+
add_header X-Fetch-Status $srcache_fetch_status;
114121
}
115122
116123
location /memc {
@@ -125,6 +132,7 @@ GET /foo
125132
--- response_headers
126133
Content-Type: text/css
127134
Content-Length:
135+
X-Fetch-Status: MISS
128136
--- response_body
129137
world
130138
@@ -139,6 +147,7 @@ world
139147
--- response_headers
140148
Content-Type: text/plain
141149
Content-Length: 8
150+
!X-Fetch-Status
142151
--- request eval
143152
"PUT /memc
144153
HTTP/1.1 200"
@@ -155,6 +164,7 @@ HTTP/1.1 200"
155164
srcache_store PUT /memc $uri;
156165
157166
echo world;
167+
add_header X-Fetch-Status $srcache_fetch_status;
158168
}
159169
160170
location /memc {
@@ -169,6 +179,7 @@ GET /foo
169179
--- response_headers
170180
Content-Type: text/css
171181
Content-Length:
182+
X-Fetch-Status: MISS
172183
--- response_body
173184
world
174185
@@ -183,6 +194,7 @@ world
183194
--- response_headers
184195
Content-Type: text/plain
185196
Content-Length: 8
197+
!X-Fetch-Status
186198
--- request eval
187199
"PUT /memc
188200
HTTP/1.1 200 OK\r
@@ -198,6 +210,7 @@ Content-Ty"
198210
default_type text/css;
199211
srcache_fetch GET /memc $uri;
200212
srcache_store PUT /memc $uri;
213+
add_header X-Fetch-Status $srcache_fetch_status;
201214
202215
echo world;
203216
}
@@ -214,6 +227,7 @@ GET /foo
214227
--- response_headers
215228
Content-Type: text/css
216229
Content-Length:
230+
X-Fetch-Status: MISS
217231
--- response_body
218232
world
219233

0 commit comments

Comments
 (0)