nginx+lua实现后台服务的缓存

最后记得加个定时任务清空缓存,好让他更新

local http = require "resty.http"

local FilePath = "/opt/www/gethome/"

function IfSave(FileName)

    local f = io.open(FileName,"r")

    if(f == nil) then

       return false

    end

    f:close()

    return true

end

ngx.req.read_body()

local post_body_tab = ngx.req.get_body_data()

local FilePathEnd = FilePath .. post_body_tab

--local post_body_tab = ngx.req.get_post_args()

--for k, v in pairs(post_body_tab) do

--   ngx.say(k,":",v)

--end

--local FilePathEnd = FilePath .. "test"

--local file = io.open(FilePathEnd,"w+")

--file:write(post_body_tab)

--file:close()

local request_url = "http://172.20.4.35:31023/home/getHome"

::lable1:: if IfSave(FilePathEnd) then

    local file = io.open(FilePathEnd,"r")

    local content = file:read('*all')

    file:close()

    ngx.say(content)

  else

     local httpc = http.new()

     local restry

     local file = assert(io.open(FilePathEnd, "w+"))

     local res, err = httpc:request_uri(request_url, {  

         method = "POST",  

         headers = {

         ["Content-Type"] = "application/x-www-form-urlencoded",

         ["access_token"] = "ad93e134-5b79-4dc5-93e1-345b79fdc522"

         },

         body = FilePathEnd

     })

     restry = res.body

     file:write(restry)

     file:close()

     goto lable1

end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值