-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
202 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,13 @@ | ||
local BasePlugin = require "kong.plugins.base_plugin" | ||
local plugin_name = ({...})[1]:match("^kong%.plugins%.([^%.]+)") | ||
local access = require("kong.plugins." .. plugin_name .. ".access") | ||
|
||
local plugin = BasePlugin:extend() | ||
local plugin = { | ||
PRIORITY = 799, | ||
VERSION = "0.0.2-1" | ||
} | ||
|
||
function plugin:new() | ||
plugin.super.new(self, plugin_name) | ||
function plugin:access(plugin_conf) | ||
access.execute(plugin_conf) | ||
end | ||
|
||
function plugin:access(conf) | ||
plugin.super.access(self) | ||
access.execute(conf) | ||
end | ||
|
||
plugin.PRIORITY = 899 | ||
plugin.VERSION = "0.0.1-1" | ||
|
||
return plugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
local kong = kong | ||
local reports = require "kong.reports" | ||
local redis = require "resty.redis" | ||
local cjson = require("cjson.safe").new() | ||
cjson.decode_array_with_array_mt(true) | ||
|
||
local _M = {} | ||
local sock_opts = {} | ||
|
||
local function is_present(str) | ||
return str and str ~= "" and str ~= ngx.null | ||
end | ||
|
||
function _M:connection(conf) | ||
-- https://github.com/openresty/lua-resty-redis | ||
local redis = redis:new() | ||
redis:set_timeout(conf.redis_timeout_in_ms) | ||
-- use a special pool name only if redis_database is set to non-zero | ||
-- otherwise use the default pool name host:port | ||
sock_opts.pool = conf.redis_database and | ||
conf.redis_host .. ":" .. conf.redis_port .. | ||
":" .. conf.redis_database | ||
sock_opts.backlog = 10 | ||
local ok, err = redis:connect(conf.redis_host, conf.redis_port, sock_opts) | ||
if not ok then | ||
kong.log.err("failed to connect to Redis: ", err) | ||
return nil, err | ||
end | ||
local times, err = redis:get_reused_times() | ||
if err then | ||
kong.log.err("failed to get connect reused times: ", err) | ||
return nil, err | ||
end | ||
if times == 0 then | ||
if is_present(conf.redis_password) then | ||
local ok, err = redis:auth(conf.redis_password) | ||
if not ok then | ||
kong.log.err("failed to auth Redis: ", err) | ||
return nil, err | ||
end | ||
end | ||
if conf.redis_database ~= 0 then | ||
-- only calls select first time, since we know the connection is shared | ||
-- between instances that use the same redis database | ||
local ok, err = redis:select(conf.redis_database) | ||
if not ok then | ||
kong.log.err("failed to change Redis database: ", err) | ||
return nil, err | ||
end | ||
end | ||
end | ||
return redis | ||
end | ||
|
||
function _M:get(redis, key, ttl, func, ...) | ||
reports.retrieve_redis_version(redis) | ||
local value, err = redis:get(key) | ||
if err then | ||
kong.log.err(" => Could not get key from Redis ... ", err) | ||
return nil, err, nil | ||
end | ||
|
||
if value and value ~= ngx.null then -- if retreive value from cache and it is not null | ||
ttl = redis:ttl(key) -- TTL -2 == key expired (the key may expire right just after the get) | ||
end | ||
|
||
if (value == ngx.null) or (ttl == -2) then | ||
kong.log.debug(" => Key does not exists on Redis") | ||
-- execute function and put it into the cache | ||
value = func(...) | ||
|
||
local ok, err = redis:set(key, value, "NX", "EX", ttl) | ||
if not ok then | ||
kong.log.err(" => Failed to set key in Redis ... ", err) | ||
return nil, err, nil | ||
end | ||
|
||
if ok == ngx.null then | ||
kong.log.err(" => Could not set key in Redis. ", err) | ||
return nil, err, nil | ||
end | ||
end | ||
|
||
local ok, err = redis:set_keepalive(10000, 100) | ||
if not ok then | ||
kong.log.err(" => Failed to set Redis keepalive: ", err) | ||
end | ||
|
||
return value, nil, ttl | ||
end | ||
|
||
return _M |
Oops, something went wrong.