From 4f0d53d1586cf4cad1fc48e167dc5d10fa695db4 Mon Sep 17 00:00:00 2001 From: Olivier BONNAURE Date: Fri, 1 Mar 2019 15:05:31 +0100 Subject: [PATCH 1/7] Fix fn.dict which should be fn.dict_name Add host to cache key name for multitenants applications --- lapis/nginx/cache.lua | 4 ++-- lapis/nginx/cache.moon | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lapis/nginx/cache.lua b/lapis/nginx/cache.lua index d00f21de..88bd790d 100644 --- a/lapis/nginx/cache.lua +++ b/lapis/nginx/cache.lua @@ -40,7 +40,7 @@ cached = function(fn_or_tbl) local cond = nil if type(fn) == "table" then exptime = fn.exptime or exptime - dict_name = fn.dict or dict_name + dict_name = fn.dict_name or dict_name cond = fn.when _cache_key = fn.cache_key or _cache_key fn = fn[1] @@ -49,7 +49,7 @@ cached = function(fn_or_tbl) if (self.req.cmd_mth ~= "GET") or (cond and not cond(self)) then return fn(self) end - local key = _cache_key(self.req.parsed_url.path, self.GET, self) + local key = _cache_key(self.req.parsed_url.host .. self.req.parsed_url.path, self.GET, self) local dict = get_dict(dict_name, self) do local cache_value = dict:get(key) diff --git a/lapis/nginx/cache.moon b/lapis/nginx/cache.moon index 650fcb4a..bfdcc17f 100644 --- a/lapis/nginx/cache.moon +++ b/lapis/nginx/cache.moon @@ -33,7 +33,7 @@ cached = (fn_or_tbl) -> if type(fn) == "table" exptime = fn.exptime or exptime - dict_name = fn.dict or dict_name + dict_name = fn.dict_name or dict_name cond = fn.when _cache_key = fn.cache_key or _cache_key @@ -43,7 +43,7 @@ cached = (fn_or_tbl) -> if (@req.cmd_mth != "GET") or (cond and not cond @) return fn @ - key = _cache_key @req.parsed_url.path, @GET, @ + key = _cache_key @req.parsed_url.host .. @req.parsed_url.path, @GET, @ dict = get_dict dict_name, @ if cache_value = dict\get key From ff33aa83d1669c393ea2db5f45ef64bc28fa4768 Mon Sep 17 00:00:00 2001 From: Olivier BONNAURE Date: Fri, 1 Mar 2019 15:28:22 +0100 Subject: [PATCH 2/7] Fix tests --- lapis/nginx/cache.lua | 4 ++-- spec/cache_spec.moon | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lapis/nginx/cache.lua b/lapis/nginx/cache.lua index 88bd790d..d00f21de 100644 --- a/lapis/nginx/cache.lua +++ b/lapis/nginx/cache.lua @@ -40,7 +40,7 @@ cached = function(fn_or_tbl) local cond = nil if type(fn) == "table" then exptime = fn.exptime or exptime - dict_name = fn.dict_name or dict_name + dict_name = fn.dict or dict_name cond = fn.when _cache_key = fn.cache_key or _cache_key fn = fn[1] @@ -49,7 +49,7 @@ cached = function(fn_or_tbl) if (self.req.cmd_mth ~= "GET") or (cond and not cond(self)) then return fn(self) end - local key = _cache_key(self.req.parsed_url.host .. self.req.parsed_url.path, self.GET, self) + local key = _cache_key(self.req.parsed_url.path, self.GET, self) local dict = get_dict(dict_name, self) do local cache_value = dict:get(key) diff --git a/spec/cache_spec.moon b/spec/cache_spec.moon index 6ef28daa..e0ae612a 100644 --- a/spec/cache_spec.moon +++ b/spec/cache_spec.moon @@ -92,7 +92,7 @@ describe "lapis.cache", -> assert.same counters, { one: 1, two: 1, sure: 1} - cache.delete_path "/hello" + cache.delete_path "localhost/hello" assert_request App!, "/hello?counter_key=one&yes=dog" assert_request App!, "/hello?yes=dog&counter_key=two" From 73934fe58d1c8bc1f7c4c6b14468ae297e9f3659 Mon Sep 17 00:00:00 2001 From: Olivier BONNAURE Date: Fri, 1 Mar 2019 16:15:48 +0100 Subject: [PATCH 3/7] Make it optional --- lapis/nginx/cache.moon | 5 +++++ spec/cache_spec.moon | 1 + 2 files changed, 6 insertions(+) diff --git a/lapis/nginx/cache.moon b/lapis/nginx/cache.moon index bfdcc17f..71738263 100644 --- a/lapis/nginx/cache.moon +++ b/lapis/nginx/cache.moon @@ -28,12 +28,14 @@ cached = (fn_or_tbl) -> fn = fn_or_tbl exptime = 0 dict_name = default_dict_name + use_host = false _cache_key = cache_key cond = nil if type(fn) == "table" exptime = fn.exptime or exptime dict_name = fn.dict_name or dict_name + use_host = fn.use_host or use_host cond = fn.when _cache_key = fn.cache_key or _cache_key @@ -43,6 +45,9 @@ cached = (fn_or_tbl) -> if (@req.cmd_mth != "GET") or (cond and not cond @) return fn @ + path = @req.parsed_url.path + path = @req.parsed_url.host .. @req.parsed_url.path if use_host + key = _cache_key @req.parsed_url.host .. @req.parsed_url.path, @GET, @ dict = get_dict dict_name, @ diff --git a/spec/cache_spec.moon b/spec/cache_spec.moon index e0ae612a..659f0583 100644 --- a/spec/cache_spec.moon +++ b/spec/cache_spec.moon @@ -52,6 +52,7 @@ describe "lapis.cache", -> layout: false "/hoi": cached { + use_host: true when: => false => count += 1 From 95021887cdd63acc87019150a753db80483c9919 Mon Sep 17 00:00:00 2001 From: Olivier BONNAURE Date: Fri, 1 Mar 2019 16:18:14 +0100 Subject: [PATCH 4/7] Update cache tests --- spec/cache_spec.moon | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/spec/cache_spec.moon b/spec/cache_spec.moon index 659f0583..3c71552b 100644 --- a/spec/cache_spec.moon +++ b/spec/cache_spec.moon @@ -52,7 +52,6 @@ describe "lapis.cache", -> layout: false "/hoi": cached { - use_host: true when: => false => count += 1 @@ -71,14 +70,19 @@ describe "lapis.cache", -> class App extends lapis.Application layout: false - "/sure": cached => - counters.sure += 1 - "howdy doody" - - "/hello": cached => - counters[@params.counter_key] += 1 - "hello #{counters[@params.counter_key]}" + "/sure": cached { + use_host: true + => + counters.sure += 1 + "howdy doody" + } + "/hello": cached { + use_host: true + => + counters[@params.counter_key] += 1 + "hello #{counters[@params.counter_key]}" + } _, a_body = assert_request App!, "/hello?counter_key=one&yes=dog" _, b_body = assert_request App!, "/hello?yes=dog&counter_key=one" From 178c87236183d66202ec84f77a0618124a303619 Mon Sep 17 00:00:00 2001 From: Olivier BONNAURE Date: Fri, 8 Mar 2019 13:11:03 +0100 Subject: [PATCH 5/7] Use path --- lapis/nginx/cache.moon | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lapis/nginx/cache.moon b/lapis/nginx/cache.moon index 71738263..fb2a1e58 100644 --- a/lapis/nginx/cache.moon +++ b/lapis/nginx/cache.moon @@ -48,7 +48,7 @@ cached = (fn_or_tbl) -> path = @req.parsed_url.path path = @req.parsed_url.host .. @req.parsed_url.path if use_host - key = _cache_key @req.parsed_url.host .. @req.parsed_url.path, @GET, @ + key = _cache_key path, @GET, @ dict = get_dict dict_name, @ if cache_value = dict\get key From 4af8ae6c4ba5a0956fb318ac5f9cc1e725121326 Mon Sep 17 00:00:00 2001 From: Olivier BONNAURE Date: Tue, 19 Mar 2019 21:00:42 +0100 Subject: [PATCH 6/7] Don't cache 401 pages --- lapis/nginx/cache.moon | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lapis/nginx/cache.moon b/lapis/nginx/cache.moon index fb2a1e58..00a2ed88 100644 --- a/lapis/nginx/cache.moon +++ b/lapis/nginx/cache.moon @@ -69,8 +69,9 @@ cached = (fn_or_tbl) -> @res.content } - dict\set key, json.encode(cache_response), exptime - ngx.header["x-memory-cache-save"] = "1" + if @res.status ~= 401 + dict\set key, json.encode(cache_response), exptime + ngx.header["x-memory-cache-save"] = "1" -- reset the request @options = {} From af4420beee3e2c6708acc6ffa7f9bd99ce714a11 Mon Sep 17 00:00:00 2001 From: Olivier BONNAURE Date: Tue, 19 Mar 2019 21:00:47 +0100 Subject: [PATCH 7/7] Don't cache 401 pages --- lapis/nginx/cache.lua | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/lapis/nginx/cache.lua b/lapis/nginx/cache.lua index d00f21de..a87aac45 100644 --- a/lapis/nginx/cache.lua +++ b/lapis/nginx/cache.lua @@ -36,11 +36,13 @@ cached = function(fn_or_tbl) local fn = fn_or_tbl local exptime = 0 local dict_name = default_dict_name + local use_host = false local _cache_key = cache_key local cond = nil if type(fn) == "table" then exptime = fn.exptime or exptime - dict_name = fn.dict or dict_name + dict_name = fn.dict_name or dict_name + use_host = fn.use_host or use_host cond = fn.when _cache_key = fn.cache_key or _cache_key fn = fn[1] @@ -49,7 +51,11 @@ cached = function(fn_or_tbl) if (self.req.cmd_mth ~= "GET") or (cond and not cond(self)) then return fn(self) end - local key = _cache_key(self.req.parsed_url.path, self.GET, self) + local path = self.req.parsed_url.path + if use_host then + path = self.req.parsed_url.host .. self.req.parsed_url.path + end + local key = _cache_key(path, self.GET, self) local dict = get_dict(dict_name, self) do local cache_value = dict:get(key) @@ -69,8 +75,10 @@ cached = function(fn_or_tbl) }, self.res.content } - dict:set(key, json.encode(cache_response), exptime) - ngx.header["x-memory-cache-save"] = "1" + if self.res.status ~= 401 then + dict:set(key, json.encode(cache_response), exptime) + ngx.header["x-memory-cache-save"] = "1" + end self.options = { } self.buffer = { } self.res.content = nil