• 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

  • 相关阅读:
    如何对一台服务器进行免密操作
    MySQL 中的自增主键
    mysql 添加多版本数据库
    Group by 优化
    join 查询优化
    CCF2020-09-Python题解
    CCF2019-09-Python题解
    CCF2019-12-Python题解
    CCF202006-Python题解
    差分约束
  • 原文地址:https://www.cnblogs.com/xivzhou/p/14494911.html
Copyright © 2020-2023  润新知