mlcache Prefix Purging
mlcache Prefix Purging
Overview
The mlcache prefix purging feature allows you to asynchronously clean cached resources in a specific name
mlcache based on a designated prefix
. Here, asynchronous
means: the system doesn’t execute the purging operation immediately; instead, it performs the actual cleaning of eligible old cached resources only when an mlcache get
operation occurs. It’s worth noting that cache resources created after the purging operation will not be cleaned again.
You can find the mlcache Purge Jobs
entry in the Global Configuration
page.
Adding a New Purge Job
Purge Job Validity Period
To prevent the accumulation of too many purge jobs, each job has a validity period
. Once this period expires, the purge job will automatically become invalid. It is recommended to set the validity period
to the maximum validity period of the cache resources themselves, ensuring that the purging operation covers all resources that need to be cleaned.
Example
Suppose we have the following Lua code that uses mlcache to cache user information:
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
Creating a Purge Job
To create a new purge job, simply fill in the relevant information and click the create button:
Viewing Purge Jobs
You can view all currently active purge jobs in the purge job list. The system will automatically clean up expired purge jobs periodically.