mlcache 字首清理
mlcache 字首清理
概述
mlcache 字首清理功能允許您根據指定的字首
非同步清理特定 name
的 mlcache 中快取的資源。這裡的非同步
是指:系統不會立即執行清理操作,而是在發生 mlcache get
類操作時,才會對符合條件的老快取資源執行實際的清理。值得注意的是,清理操作執行後新建的快取資源不會被重複清理。
您可以在全域性配置
頁面找到mlcache 清除任務
的入口。
新增清理任務
清理任務有效期
為避免清理任務過多積累,每個清理任務都設有有效期
。一旦超過有效期,該清理任務將自動失效。建議將有效期
設定為快取資源本身的最大有效期,以確保清理操作能夠覆蓋所有需要清理的資源。
示例
假設我們有以下 Lua 程式碼,它使用 mlcache 來快取使用者資訊:
local mlcache = require "resty.mlcache"
local _M = {}
local function value_cb(username)
return "user:" .. tostring(username)
end
function _M.go()
local name = "foo"
local sd_name = "ml_cache_shared_dict"
local cache_key_prefix = "prefix-foo:"
local cache, err = mlcache.new(name, sd_name, { lru_size = 1000})
local args = ngx.req.get_uri_args()
local username = args.username
local cache_key = cache_key_prefix .. username
local ttl, err, value = cache:peek(cache_key)
if err then
ngx.say("could not peek cache: ", err)
return
end
ngx.say("cache key: ", cache_key, ", value: ", value, ", ttl: ", ttl)
if not value then
local value, err = cache:get(cache_key, { ttl = 86400 }, value_cb, username)
if err then
ngx.say("could not get cache: ", err)
return
end
ngx.say("cache key: ", cache_key, ", value: ", value, ", err: ", err)
end
end
return _M
建立清理任務
要建立新的清理任務,只需填寫相關資訊並點選建立按鈕:
檢視清理任務
您可以在清理任務列表中檢視所有當前生效的清除任務。系統會定期自動清理已過期的清除任務。