Skip to content

Commit

Permalink
change: make lrucache lock optional
Browse files Browse the repository at this point in the history
  • Loading branch information
spacewander committed Oct 30, 2020
1 parent 254952c commit 8388350
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion apisix/core/lrucache.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

local lru_new = require("resty.lrucache").new
local resty_lock = require("resty.lock")
local log = require("apisix.core.log")
local tostring = tostring
local ngx = ngx
local get_phase = ngx.get_phase
Expand Down Expand Up @@ -69,10 +70,11 @@ local function new_lru_fun(opts)
local item_ttl = opts and opts.ttl or GLOBAL_TTL
local item_release = opts and opts.release
local invalid_stale = opts and opts.invalid_stale
local limit_concurrency = opts and opts.limit_concurrency
local lru_obj = lru_new(item_count)

return function (key, version, create_obj_fun, ...)
if not can_yield_phases[get_phase()] then
if not limit_concurrency or not can_yield_phases[get_phase()] then
local cache_obj = fetch_valid_cache(lru_obj, invalid_stale,
item_ttl, item_release, key, version)
if cache_obj then
Expand All @@ -99,6 +101,8 @@ local function new_lru_fun(opts)
end

local key_s = tostring(key)
log.info("try to lock with key ", key_s)

local elapsed, err = lock:lock(key_s)
if not elapsed then
return nil, "failed to acquire the lock: " .. err
Expand All @@ -108,6 +112,7 @@ local function new_lru_fun(opts)
nil, key, version)
if cache_obj then
lock:unlock()
log.info("unlock with key ", key_s)
return cache_obj.val
end

Expand All @@ -116,6 +121,7 @@ local function new_lru_fun(opts)
lru_obj:set(key, {val = obj, ver = version}, item_ttl)
end
lock:unlock()
log.info("unlock with key ", key_s)

return obj, err
end
Expand Down

0 comments on commit 8388350

Please sign in to comment.