membphis commented on a change in pull request #1153: feature: support for proxy caching plugin based on disk. URL: https://github.com/apache/incubator-apisix/pull/1153#discussion_r392657923
########## File path: lua/apisix/plugins/proxy-cache.lua ########## @@ -178,6 +158,91 @@ local function match_method_and_status(conf, ctx) return false end +-- refer to https://gist.github.com/titpetric/ed6ec548af160e82c650cf39074878fb +local function file_exists(name) + local f = io_open(name, "r") + if f~=nil then io_close(f) return true else return false end +end + + +local function explode(d, p) + local t, ll + t={} + ll=0 + if(#p == 1) then return {p} end + while true do + local l=string.find(p, d, ll, true) -- find the next d in the string + if l~=nil then -- if "not not" found then.. + tab_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 + tab_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 + + +local function generate_cache_filename(cache_path, cache_levels, cache_key) + local md5sum = ngx.md5(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 + + +local function cache_purge(conf, ctx) + local cache_zone_info = ngx_re.split(ctx.var.upstream_cache_zone_info, ",") + + local filename = generate_cache_filename(cache_zone_info[1], cache_zone_info[2], + ctx.var.upstream_cache_key) + if file_exists(filename) then + os.remove(filename) Review comment: that is fine. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services