moonming 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_r387403420
########## File path: lua/apisix/plugins/proxy-cache.lua ########## @@ -0,0 +1,278 @@ +-- +-- Licensed to the Apache Software Foundation (ASF) under one or more +-- contributor license agreements. See the NOTICE file distributed with +-- this work for additional information regarding copyright ownership. +-- The ASF licenses this file to You under the Apache License, Version 2.0 +-- (the "License"); you may not use this file except in compliance with +-- the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- + +local core = require("apisix.core") +local ngx_re = require("ngx.re") +local tab_insert = table.insert +local tab_concat = table.concat +local string = string +local io_open = io.open +local io_close = io.close +local ngx = ngx +local os = os +local ipairs = ipairs +local pairs = pairs +local tonumber = tonumber + +local plugin_name = "proxy-cache" + +local schema = { + type = "object", + properties = { + cache_zone = { + type = "string", + minLength = 1 + }, + cache_key = { + type = "array", + minItems = 1, + items = { + description = "a key for caching", + type = "string", + pattern = [[(^[^\$].+$|^\$[0-9a-zA-Z_]+$)]] + }, + }, + cache_http_status = { + type = "array", + minItems = 1, + items = { + description = "http response status", + type = "integer", + minimum = 200, + maximum = 599, + }, + uniqueItems = true, + default = {200, 301, 404}, + }, + cache_method = { + type = "array", + minItems = 1, + items = { + description = "http method", + type = "string", + enum = {"GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", + "OPTIONS", "CONNECT", "TRACE"}, + }, + uniqueItems = true, + default = {"GET", "HEAD"}, + }, + hide_cache_headers = { + type = "boolean", + default = false, + }, + cache_bypass = { + type = "array", + minItems = 1, + items = { + type = "string", + pattern = [[(^[^\$].+$|^\$[0-9a-zA-Z_]+$)]] + }, + }, + no_cache = { + type = "array", + minItems = 1, + items = { + type = "string", + pattern = [[(^[^\$].+$|^\$[0-9a-zA-Z_]+$)]] + }, + }, + }, + required = {"cache_zone", "cache_key"}, +} + +local _M = { + version = 0.1, + priority = 1009, + name = plugin_name, + schema = schema, +} + + +function _M.check_schema(conf) + local ok, err = core.schema.check(schema, conf) + if not ok then + return false, err + end + + return true +end + + +local tmp = {} +local function generate_complex_value(data, ctx) + core.table.clear(tmp) + + core.log.info("proxy-cache complex value: ", core.json.delay_encode(data)) + for i, value in ipairs(data) do + core.log.info("proxy-cache complex value index-", i, ": ", value) + + if string.sub(value, 1, 1) == "$" then + tab_insert(tmp, ctx.var[string.sub(value, 2)]) + else + tab_insert(tmp, value) + end + end + + return tab_concat(tmp, "") +end + + +-- check whether the request method and response status +-- match the user defined. +local function match_method_and_status(conf, ctx) + local match_method, match_status = false, false + + -- Maybe there is no need for optimization here. + for _, method in ipairs(conf.cache_method) do + if method == ctx.var.request_method then + match_method = true + break + end + end + + for _, status in ipairs(conf.cache_http_status) do + if status == ngx.status then + match_status = true + break + end + end + + if match_method and match_status then + return true + end + + 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; Review comment: better remove `;` in the end of line ---------------------------------------------------------------- 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: [email protected] With regards, Apache Git Services
