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
创建清理任务
要创建新的清理任务,只需填写相关信息并点击创建按钮:
查看清理任务
您可以在清理任务列表中查看所有当前生效的清除任务。系统会定期自动清理已过期的清除任务。