cache.lua--自带加锁功能方式缓存击穿

local key = ngx.re.match(ngx.var.request_uri,"/([0-9]+).html")

local mlcache = require "resty.mlcache"
local function fetch_shop(aaa)
ngx.log(ngx.ERR,'-------L3----------',aaa)
return 'id=10'
end

if type(key) == "table" then
local cache,err = mlcache.new("cache_name","my_cache",{
lur_size = 500, --设置缓存的个数
ttl = 5, --缓存过期时间
neg_ttl = 6, --L3返回nil的保存时间
ipc_shm = "ipc_cache" --用于将L2的缓存保存到L1

})
if not cache then
	ngx.log(ngx.ERR,"-------------mlcache err")
end
local shop_detail,err,level = cache:get(key[1],nil,fetch_shop,"123")
    if level == 3 then
        math.randomseed(tostring(os.time()))
    local expire_time = math.random(1,6)
        local res,err = cache:set(key[1],{ttl=expire_time},shop_detail)
    end
    ngx.say(shop_detail)

end

posted on 2021-03-07 18:46  木林coder  阅读(147)  评论(0编辑  收藏  举报

导航