Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add host to cache key name for multi-tenants applications #664

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions lapis/nginx/cache.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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)
Expand All @@ -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
Expand Down
14 changes: 10 additions & 4 deletions lapis/nginx/cache.moon
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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 = {}
Expand Down
21 changes: 13 additions & 8 deletions spec/cache_spec.moon
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down