orangleliu 笔记本

lzz的编程之旅,技术,思考,分享,happy

[OpenResty]使用nginx lua清除proxy cache

分享2篇文章,介绍了nginx proxy cache的原理,以及怎么使用lua做 cache purge, 包括单一url的清除,特别的支持了通配符的purge操作。

文章

代码

gist 需要梯子,为了方面,搬运过来了。有时间会补充个case。

通过 PURGE request 清除单个item gist url

-- Tit Petric, Monotek d.o.o., Thu 27 Oct 2016 10:43:38 AM CEST
--
-- Delete nginx cached assets with a PURGE request against an endpoint
--

local md5 = require 'md5'

function file_exists(name)
        local f = io.open(name, "r")
        if f~=nil then io.close(f) return true else return false end
end

function explode(d, p)
        local t, ll
        t={}
        ll=0
        if(#p == 1) then return {p} end
                while true do
                        l=string.find(p, d, ll, true) -- find the next d in the string
                        if l~=nil then -- if "not not" found then..
                                table.insert(t, string.sub(p, ll, l-1)) -- Save it in our array.
                                ll=l+1 -- save just after where we found it for searching next time.
                        else
                                table.insert(t, string.sub(p, ll)) -- Save what's left in our array.
                                break -- Break at end, as it should be, according to the lua manual.
                        end
                end
        return t
end

function cache_filename(cache_path, cache_levels, cache_key)
        local md5sum = md5.sumhexa(cache_key)
        local levels = explode(":", cache_levels)
        local filename = ""

        local index = string.len(md5sum)
        for k, v in pairs(levels) do
                local length = tonumber(v)
                -- add trailing [length] chars to index
                index = index - length;
                filename = filename .. md5sum:sub(index+1, index+length) .. "/";
        end
        if cache_path:sub(-1) ~= "/" then
                cache_path = cache_path .. "/";
        end
        filename = cache_path .. filename .. md5sum
        return filename
end

function purge(filename)
        if (file_exists(filename)) then
                os.remove(filename)
        end
end

if ngx ~= nil then
        local cache_key = ngx.var.lua_purge_upstream .. ngx.var.request_uri
        local filename = cache_filename(ngx.var.lua_purge_path, ngx.var.lua_purge_levels, cache_key)
        purge(filename)
        ngx.say("OK")
        ngx.exit(ngx.OK)
end

通配符清除cache 核心代码 gist url

-- Tit Petric, Monotek d.o.o., Tue 03 Jan 2017 06:54:56 PM CET
--
-- Delete nginx cached assets with a PURGE request against an endpoint
-- supports extended regular expression PURGE requests (/upload/.*)
--

function file_exists(name)
        local f = io.open(name, "r")
        if f~=nil then io.close(f) return true else return false end
end

function explode(d, p)
        local t, ll
        t={}
        ll=0
        if(#p == 1) then return {p} end
                while true do
                        l=string.find(p, d, ll, true) -- find the next d in the string
                        if l~=nil then -- if "not not" found then..
                                table.insert(t, string.sub(p, ll, l-1)) -- Save it in our array.
                                ll=l+1 -- save just after where we found it for searching next time.
                        else
                                table.insert(t, string.sub(p, ll)) -- Save what's left in our array.
                                break -- Break at end, as it should be, according to the lua manual.
                        end
                end
        return t
end

function purge(filename)
        if (file_exists(filename)) then
                os.remove(filename)
        end
end

function trim(s)
        return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
end

function exec(cmd)
        local handle = io.popen(cmd)
        local result = handle:read("*all")
        handle:close()
        return trim(result)
end

function list_files(cache_path, purge_upstream, purge_pattern)
        local result = exec("/usr/bin/find " .. cache_path .. " -type f | /usr/bin/xargs --no-run-if-empty -n1000 /bin/grep -El -m 1 '^KEY: " .. purge_upstream .. purge_pattern .. "' 2>&1")
        if result == "" then
                return {}
        end
        return explode("\n", result)
end

if ngx ~= nil then
        -- list all cached items matching upstream+request_uri (extended regex)
        local files = list_files(ngx.var.lua_purge_path, ngx.var.lua_purge_upstream, ngx.var.request_uri)

        ngx.header["Content-type"] = "text/plain; charset=utf-8"
        ngx.header["X-Purged-Count"] = table.getn(files)
        for k, v in pairs(files) do
                purge(v)
        end
        ngx.say("OK")
        ngx.exit(ngx.OK)
end
阅读更多
版权声明:本文为orangleliu (http://blog.csdn.net/orangleliu/)原创文章,自由传播,文章转载请声明, 多谢。 https://blog.csdn.net/lzz957748332/article/details/78958422
个人分类: openresty
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

[OpenResty]使用nginx lua清除proxy cache

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭