lock.lua

local rety_lock = require "resty.lock"
local cache = ngx.shared.my_cache
local key = ngx.re.match(ngx.var.request_uri,"/([0-9]+).html")

if type(key) == "table" then
local res,err = cache:get(key[1])
if res then
ngx.say('val res')
return
end
local lock,err = rety_lock:new("my_locks",{exptime=10,timeout=1})
if not lock then
ngx.log(ngx.ERR,'--------------new lock err')
end
local flag_lock,err = lock:lock(key[1])
if err then
ngx.log(ngx.ERR,'--------------lock err')
end
if not flag_lock then
local res = cache:get_stale(key[1])
return res
end

local res,err = cache:get(key[1])
    if res then
	lock:unlock()
            return res
    end
--ngx.sleep(2)


local req_data
local method = ngx.var.request.method
if method == "POST" then
	req_data = {method=ngx.HTTP_POST, body=ngx.req.read_body()}

elseif method == "PUT" then
	req_data = {method=ngx.HTTP_PUT, body=ngx.req.read_body()}

else
	req_data = {method=ngx.HTTP_GET}

end
local res,err = ngx.location.capture(
	'index.php',
	req_data
)
if res.status == 200 then
	ngx.say(res.body)
end
lock:unlock()

end

原文地址:https://www.cnblogs.com/xivzhou/p/14494911.html