Skip to content

Commit

Permalink
refactor(proxy-cache) use kong pdk and localize vars
Browse files Browse the repository at this point in the history
### Summary

Also fixes #17 and closes #18
  • Loading branch information
bungle committed Jan 22, 2020
1 parent 839f5a7 commit 25a8691
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 146 deletions.
18 changes: 8 additions & 10 deletions kong/plugins/proxy-cache/api.lua
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
local STRATEGY_PATH = "kong.plugins.proxy-cache.strategies"


local require = require
local kong = kong
local cluster_events = kong.cluster_events
local fmt = string.format


local function broadcast_purge(plugin_id, cache_key)
local data = string.format("%s:%s", plugin_id, cache_key or "nil")
ngx.log(ngx.DEBUG, "[proxy-cache] broadcasting purge '", data, "'")
return cluster_events:broadcast("proxy-cache:purge", data)
local data = fmt("%s:%s", plugin_id, cache_key or "nil")
kong.log.debug("broadcasting purge '", data, "'")
return kong.cluster_events:broadcast("proxy-cache:purge", data)
end


Expand Down Expand Up @@ -53,8 +54,7 @@ return {
then
local ok, err = broadcast_purge(plugin.id, nil)
if not ok then
ngx.log(ngx.ERR, "failed broadcasting proxy cache purge to ",
"cluster: ", err)
kong.log.err("failed broadcasting proxy cache purge to cluster: ", err)
end
end

Expand Down Expand Up @@ -112,8 +112,7 @@ return {
then
local ok, err = broadcast_purge(plugin.id, self.params.cache_key)
if not ok then
ngx.log(ngx.ERR,
"failed broadcasting proxy cache purge to cluster: ", err)
kong.log.err("failed broadcasting proxy cache purge to cluster: ", err)
end
end

Expand Down Expand Up @@ -189,8 +188,7 @@ return {
if require(STRATEGY_PATH).LOCAL_DATA_STRATEGIES[conf.strategy] then
local ok, err = broadcast_purge(plugin.id, self.params.cache_key)
if not ok then
ngx.log(ngx.ERR, "failed broadcasting proxy cache purge to cluster: ",
err)
kong.log.err("failed broadcasting proxy cache purge to cluster: ", err)
end
end

Expand Down
17 changes: 12 additions & 5 deletions kong/plugins/proxy-cache/cache_key.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
local fmt = string.format
local md5 = ngx.md5
local type = type
local pairs = pairs
local sort = table.sort
local insert = table.insert
local concat = table.concat


local _M = {}


local EMPTY = {}


Expand All @@ -28,16 +35,16 @@ local function generate_key_from(args, vary_fields)
local arg = args[field]
if arg then
if type(arg) == "table" then
table.sort(arg)
table.insert(cache_key, field .. "=" .. table.concat(arg, ","))
sort(arg)
insert(cache_key, field .. "=" .. concat(arg, ","))

else
table.insert(cache_key, field .. "=" .. arg)
insert(cache_key, field .. "=" .. arg)
end
end
end

return table.concat(cache_key, ":")
return concat(cache_key, ":")
end


Expand All @@ -48,7 +55,7 @@ end
local function params_key(params, plugin_config)
if not (plugin_config.vary_query_params or EMPTY)[1] then
local actual_keys = keys(params)
table.sort(actual_keys)
sort(actual_keys)
return generate_key_from(params, actual_keys)
end

Expand Down
Loading

0 comments on commit 25a8691

Please sign in to comment.