Hello. I have a custom plugin which I want to use cache settings to prevent the overload in my database. My database is located in another web service, so each time a new token arrives I must confront it with that service to check wheter its a true token or not.
The idea is to maintain this token, after validation in Kong’s cache. But, when I try to save it, I receive:
attempt to index upvalue ‘cache’ (a nil value)
My code looks like this:
function loadCache(token, conf)
ngx.log(ngx.ERR, "#### LOAD CACHE ####")
ttl, err, value = cache:probe(token)
if err then
ngx.log(ngx.ERR, "Could not probe cache: ", err)
return
end
ngx.log(ngx.CRIT, ttl)
ngx.log(ngx.CRIT, value)
return ttl
end
function createTokenCache(token, conf)
ngx.log(ngx.ERR, "#### CREATE TOKEN CACHE ####")
value, err = cache:get(token, { ttl = 3600 }, function() return token end)
end
function Ctk2Handler:access(conf)
Ctk2Handler.super.access(self)
-- GET JWT FROM HEADER AND ASSIGN TO TOKEN VARIABLE
token = ngx.req.get_headers()["Authorization"]
ngx.log(ngx.CRIT, chave)
-- THE STATUS CODE RETRIEVED FROM THE SERVICE
local ok, err = checkJWT(token, conf)
ngx.log(ngx.CRIT, "########## HANDLER.LUA ######## OK")
ngx.log(ngx.CRIT, ok)
ngx.log(ngx.CRIT, "########## HANDLER.LUA ######## ERR")
ngx.log(ngx.CRIT, err)
statusCode = ok
if statusCode == 200 then
ngx.log(ngx.CRIT, "### STATUS 200 OK ###")
ngx.log(ngx.CRIT, uriRetrieved)
local criaCachetoken = createTokenCache(token, conf)
else
ngx.log(ngx.CRIT, "### NÃO AUTORIZADO ###")
return responses.send_HTTP_FORBIDDEN("You cannot consume this service")
end
end
What I’m doing wrong? I followed the methods from this page:
And tried aldo the ones in here:
The tokens have a TTL of 3600 seconds, there are no chances to store that value in cassandra or postgres database.
Any help would be welcome.