Skip to content

Commit 5c6e67a

Browse files
ddragosdselfxp
authored andcommitted
request_caching only reads from memory
1 parent eb11825 commit 5c6e67a

File tree

6 files changed

+31
-22
lines changed

6 files changed

+31
-22
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Table of Contents
1515
Status
1616
======
1717

18-
This library is still under active development and is NOT YET production ready.
18+
This library is under development and is considered production ready.
1919

2020
Synopsis
2121
========

src/lua/api-gateway/cache/request/rcache.lua

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,12 @@ function _M:new(o)
2828
return o
2929
end
3030

31-
32-
--- This method returns the request body using Lua functions.
33-
-- There are 2 functions used:
34-
-- 1. ngx.req.get_body_data() - returning nil if
35-
-- 1. the request body has not been read,
36-
-- 2. the request body has been read into disk temporary files,
37-
-- 3. or the request body has zero size.
38-
-- 2. ngx.req.get_body_file()
39-
-- If the request body has been read into disk files, ngx.req.get_body_file() function may be used instead.
40-
--
31+
--- This method returns the request body.
32+
-- srcache module is loading the request body from memory.
33+
-- see: https://groups.google.com/forum/#!topic/openresty-en/Tc5ovYyNMGU
4134
local function readRequestBody()
4235
ngx.req.read_body()
43-
return ngx.req.get_body_data() or ngx.req.get_body_file()
36+
return ngx.req.get_body_data()
4437
end
4538

4639
--- Stores the given key into the given cache instance.
@@ -49,7 +42,11 @@ end
4942
-- @param key the key to save
5043
--
5144
local function put(cache, key)
52-
local value = readRequestBody()
45+
local pcall_ok, value = pcall(readRequestBody)
46+
if (not pcall_ok) then
47+
ngx.log(ngx.WARN, "Could not read request body. Check the logs for more info.")
48+
return
49+
end
5350
--ngx.log(ngx.DEBUG, "Storing value=", tostring(value), " into key=", tostring(key), " in cache")
5451
ngx.log(ngx.DEBUG, "Storing key=", tostring(key), " in cache.")
5552
if (value ~= nil) then

test/manual/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ Test manually caching with an external HTTP Client ( Browser, JMeter, etc )
1212
make test-docker-manual
1313
```
1414

15-
Then open the browser to `http://<docker_ip>/cache/favicon.ico` , `http://<docker_ip>/cache/index.html` and refresh the page to retrieve the content from cache.
15+
Then open the browser to `http://<docker_ip>/cache/favicon.ico` , `http://<docker_ip>/cache/index.html`
16+
or `http://<docker_ip>/cache/img_test.JPG` and refresh the page to retrieve the content from cache.

test/manual/api-gateway-config/conf.d/includes/basic_endpoints.conf

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ location = /request-caching {
2626
set $subrequest_method $echo_request_method;
2727
set_escape_uri $escaped_key $arg_key;
2828

29+
client_max_body_size 35m;
30+
# http://nginx.org/en/docs/http/ngx_http_core_module.html#client_body_in_file_only
31+
client_body_in_file_only on;
32+
2933
content_by_lua '
3034
local sr_method = ngx.var.subrequest_method
3135
local cache = ngx.apiGateway.request_cache
@@ -66,11 +70,17 @@ location = /html/index.html {
6670
root /etc/api-gateway/;
6771
}
6872

73+
location = /html/img_test.JPG {
74+
default_type image/jpeg;
75+
expires 25s;
76+
root /etc/api-gateway/;
77+
}
78+
6979
location ~ ^/cache/(?<subpath>.+) {
7080
sendfile off;
7181

7282
srcache_default_expire 1000s;
73-
srcache_request_cache_control on; # onor Cache-control: no-cache and Pragma:no-cache
83+
srcache_request_cache_control on; # honor Cache-control: no-cache and Pragma:no-cache
7484

7585
set $key $request_uri;
7686
set_escape_uri $escaped_key $key;
@@ -79,11 +89,12 @@ location ~ ^/cache/(?<subpath>.+) {
7989
# This directive controls what responses to store to the cache according to their status code.
8090
srcache_store_statuses 200 301 302;
8191

82-
# force small buffers for test
83-
proxy_buffer_size 1k;
84-
proxy_buffers 2 1k;
85-
proxy_buffering on;
86-
proxy_busy_buffers_size 1k;
92+
93+
# force small buffers for test
94+
# proxy_buffer_size 1k;
95+
# proxy_buffers 2 1k;
96+
# proxy_buffering on;
97+
# proxy_busy_buffers_size 1k;
8798

8899
# proxy_pass/fastcgi_pass/drizzle_pass/echo/etc...
89100
# or even static files on the disk
2.73 MB
Loading

test/manual/api-gateway-config/scripts/lua/api_gateway_init.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ local function initRequestCaching(parentObject)
4141
local cache_cls = require "api-gateway.cache.cache"
4242
parentObject.request_cache = cache_cls:new()
4343

44-
local local_cache_max_ttl = 1000
44+
local local_cache_max_ttl = 10
4545
local local_cache = require "api-gateway.cache.store.localCache":new({
4646
dict = "cachedkeys", -- defined in nginx conf as lua_shared_dict cachedkey 50m;
4747
ttl = function (value)
4848
return math.min(local_cache_max_ttl,(ngx.var.arg_exptime or local_cache_max_ttl))
4949
end
5050
})
51-
local redis_cache_max_ttl = 2000
51+
local redis_cache_max_ttl = 20
5252
local redis_cache = require "api-gateway.cache.store.redisSetCache":new({
5353
ttl = function(value)
5454
-- ngx.var.arg_exptime is automatically set when

0 commit comments

Comments
 (0)