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 diff --git a/lapis/nginx/cache.moon b/lapis/nginx/cache.moon index 650fcb4a..00a2ed88 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 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 @@ -43,7 +45,10 @@ cached = (fn_or_tbl) -> if (@req.cmd_mth != "GET") or (cond and not cond @) return fn @ - key = _cache_key @req.parsed_url.path, @GET, @ + path = @req.parsed_url.path + path = @req.parsed_url.host .. @req.parsed_url.path if use_host + + key = _cache_key path, @GET, @ dict = get_dict dict_name, @ if cache_value = dict\get key @@ -64,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 = {} diff --git a/spec/cache_spec.moon b/spec/cache_spec.moon index 6ef28daa..3c71552b 100644 --- a/spec/cache_spec.moon +++ b/spec/cache_spec.moon @@ -70,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" @@ -92,7 +97,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"