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

feat(get): add option replace to cache:get options #121

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,12 @@ options:
having to repeat such transformations on every request, such as creating
tables, cdata objects, loading new Lua code, etc...
**Default:** inherited from the instance.
- `replace`: _optional_ boolean. When specified and set to `true`, `get()` will
always attempt to call the `callback` function when specified and replace the
value in caches. It will also notify other workers to invalidate their caches.
This option can be used when the cached value needs to be changed before its
expiry and the other workers be notified about it. For example a periodic or
forced rotation of a cached value.

The third argument `callback` is optional. If provided, it must be a function
whose signature and return values are documented in the following example:
Expand Down
40 changes: 31 additions & 9 deletions lib/resty/mlcache.lua
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,8 @@ end


local function run_callback(self, key, shm_key, data, ttl, neg_ttl,
went_stale, l1_serializer, resurrect_ttl, shm_set_tries, cb, ...)
went_stale, l1_serializer, resurrect_ttl,
shm_set_tries, replace, cb, ...)
local lock, err = resty_lock:new(self.shm_locks, self.resty_lock_opts)
if not lock then
return nil, "could not create lock: " .. err
Expand Down Expand Up @@ -816,6 +817,17 @@ local function run_callback(self, key, shm_key, data, ttl, neg_ttl,
return unlock_and_ret(lock, nil, err)
end

if replace then
if not self.broadcast then
error("no ipc to propagate invalidation, specify opts.ipc_shm or opts.ipc", 3)
end

local _, broadcast_err = self.broadcast(self.events.invalidation.channel, key)
if broadcast_err then
ngx_log(WARN, "could not broadcast invalidation (", broadcast_err, ")")
end
end

if data == CACHE_MISS_SENTINEL_LRU then
data = nil
end
Expand All @@ -837,13 +849,21 @@ function _M:get(key, opts, cb, ...)

-- worker LRU cache retrieval

local data = self.lru:get(key)
if data == CACHE_MISS_SENTINEL_LRU then
return nil, nil, 1
local data
local replace
if cb then
replace = opts and opts.replace == true or nil
end

if data ~= nil then
return data, nil, 1
if not replace then
data = self.lru:get(key)
if data == CACHE_MISS_SENTINEL_LRU then
return nil, nil, 1
end

if data ~= nil then
return data, nil, 1
end
end

-- not in worker's LRU cache, need shm lookup
Expand All @@ -870,10 +890,12 @@ function _M:get(key, opts, cb, ...)
data = nil
end

return data, nil, is_stale and 4 or 2
if not replace then
return data, nil, is_stale and 4 or 2
end
end

-- not in shm either
-- not in shm either, or forced callback

if cb == nil then
-- no L3 callback, early exit
Expand All @@ -884,7 +906,7 @@ function _M:get(key, opts, cb, ...)

return run_callback(self, key, namespaced_key, data, ttl, neg_ttl,
went_stale, l1_serializer, resurrect_ttl,
shm_set_tries, cb, ...)
shm_set_tries, replace, cb, ...)
end


Expand Down