Skip to content

Commit

Permalink
fix(mlcache) use ttl given by callback call
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaultcha committed Apr 28, 2017
1 parent 2aea030 commit 92c42e1
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 17 deletions.
29 changes: 16 additions & 13 deletions lib/resty/mlcache.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ ffi.cdef [[
unsigned char *type_data;
unsigned char *data;
double at;
double ttl;
};
]]

Expand All @@ -51,7 +52,8 @@ do


marshallers = {
shm_value = function(str_value, value_type, at)
shm_value = function(str_value, value_type, at, ttl)
shm_value_cdata.ttl = ttl
shm_value_cdata.at = at
shm_value_cdata.len = #str_value
shm_value_cdata.type_len = #value_type
Expand All @@ -61,8 +63,9 @@ do
return ffi_str(shm_value_cdata, shm_value_size)
end,

shm_nil = function(at)
shm_value_nil_cdata.at = at
shm_nil = function(at, ttl)
shm_value_nil_cdata.at = at
shm_value_nil_cdata.ttl = ttl

return ffi_str(shm_value_nil_cdata, shm_value_size)
end,
Expand Down Expand Up @@ -97,7 +100,7 @@ do
local value = ffi_str(shm_value.data, shm_value.len)
local value_type = ffi_str(shm_value.type_data, shm_value.type_len)

return value, value_type, shm_value.at
return value, value_type, shm_value.at, shm_value.ttl
end,

number = function(str)
Expand Down Expand Up @@ -197,17 +200,18 @@ local function set_lru(self, key, value, ttl)
end


local function shmlru_get(self, key, ttl, neg_ttl)
local function shmlru_get(self, key)
local v, err = self.dict:get(key)
if err then
return nil, "could not read from lua_shared_dict: " .. err
end

if v ~= nil then
local str_serialized, value_type, at = unmarshallers.shm_value(v)
local str_serialized, value_type, at, ttl = unmarshallers.shm_value(v)

local remaining_ttl = ttl - (now() - at)

if value_type == "nil" then
local remaining_ttl = neg_ttl - (now() - at)
return set_lru(self, key, CACHE_MISS_SENTINEL_LRU, remaining_ttl)
end

Expand All @@ -217,8 +221,6 @@ local function shmlru_get(self, key, ttl, neg_ttl)
"retrieval: " .. err
end

local remaining_ttl = ttl - (now() - at)

return set_lru(self, key, value, remaining_ttl)
end
end
Expand All @@ -228,7 +230,7 @@ local function shmlru_set(self, key, value, ttl, neg_ttl)
local at = now()

if value == nil then
local shm_nil = marshallers.shm_nil(at)
local shm_nil = marshallers.shm_nil(at, neg_ttl)

-- we need to cache that this was a miss, and ensure cache hit for a
-- nil value
Expand Down Expand Up @@ -258,7 +260,8 @@ local function shmlru_set(self, key, value, ttl, neg_ttl)
.. err
end

local shm_marshalled = marshallers.shm_value(str_marshalled, value_type, at)
local shm_marshalled = marshallers.shm_value(str_marshalled, value_type,
at, ttl)

-- cache value in shm for currently-locked workers

Expand Down Expand Up @@ -336,7 +339,7 @@ function _M:get(key, opts, cb, ...)
-- not in worker's LRU cache, need shm lookup

local err
data, err = shmlru_get(self, key, ttl, neg_ttl)
data, err = shmlru_get(self, key)
if err then
return nil, err
end
Expand Down Expand Up @@ -364,7 +367,7 @@ function _M:get(key, opts, cb, ...)

-- check for another worker's success at running the callback

data, err = shmlru_get(self, key, ttl, neg_ttl)
data, err = shmlru_get(self, key)
if err then
return unlock_and_ret(lock, nil, err)
end
Expand Down
142 changes: 138 additions & 4 deletions t/02-get.t
Original file line number Diff line number Diff line change
Expand Up @@ -701,9 +701,7 @@ in callback
content_by_lua_block {
local mlcache = require "resty.mlcache"
local cache, err = mlcache.new("cache", {
ttl = 2
})
local cache, err = mlcache.new("cache")
if not cache then
ngx.log(ngx.ERR, err)
return
Expand Down Expand Up @@ -753,7 +751,74 @@ in callback



=== TEST 16: get() caches for 'opts.neg_ttl'
=== TEST 16: get() shm -> LRU caches for 'opts.ttl - since'
--- http_config eval: $::HttpConfig
--- config
location = /t {
content_by_lua_block {
local mlcache = require "resty.mlcache"
local cache, err = mlcache.new("cache")
if not cache then
ngx.log(ngx.ERR, err)
return
end
local function cb()
ngx.say("in callback")
return 123
end
-- from callback
local data, err = cache:get("key", { ttl = 1 }, cb)
if err then
ngx.log(ngx.ERR, err)
return
end
assert(data == 123)
ngx.sleep(0.5)
-- delete from LRU
cache.lru:delete("key")
-- from shm
data, err = cache:get("key", nil, cb)
if err then
ngx.log(ngx.ERR, err)
return
end
assert(data == 123)
ngx.sleep(0.5)
-- from callback again
data, err = cache:get("key", nil, cb)
if err then
ngx.log(ngx.ERR, err)
return
end
assert(data == 123)
}
}
--- request
GET /t
--- response_body
in callback
in callback
--- no_error_log
[error]



=== TEST 17: get() caches for 'opts.neg_ttl'
--- http_config eval: $::HttpConfig
--- config
location = /t {
Expand Down Expand Up @@ -809,3 +874,72 @@ in callback
in callback
--- no_error_log
[error]



=== TEST 18: get() shm -> LRU caches for 'opts.neg_ttl - since'
--- http_config eval: $::HttpConfig
--- config
location = /t {
content_by_lua_block {
local mlcache = require "resty.mlcache"
local cache, err = mlcache.new("cache", {
neg_ttl = 2
})
if not cache then
ngx.log(ngx.ERR, err)
return
end
local function cb()
ngx.say("in callback")
return nil
end
-- from callback
local data, err = cache:get("key", { neg_ttl = 1 }, cb)
if err then
ngx.log(ngx.ERR, err)
return
end
assert(data == nil)
ngx.sleep(0.5)
-- delete from LRU
cache.lru:delete("key")
-- from shm
data, err = cache:get("key", nil, cb)
if err then
ngx.log(ngx.ERR, err)
return
end
assert(data == nil)
ngx.sleep(0.5)
-- from callback again
data, err = cache:get("key", nil, cb)
if err then
ngx.log(ngx.ERR, err)
return
end
assert(data == nil)
}
}
--- request
GET /t
--- response_body
in callback
in callback
--- no_error_log
[error]

0 comments on commit 92c42e1

Please sign in to comment.