How can one succesfully implement cache in Kong

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.

We have a working sample here:

Specifically:

local function getKongKey(key, location)
  -- This will add a non expiring TTL on this cached value
  -- https://github.com/thibaultcha/lua-resty-mlcache/blob/master/README.md
  local pkey, err = singletons.cache:get(key, { ttl = 0 }, readFromFile, location)
	
  if err then
    ngx.log(ngx.ERR, "Could not retrieve pkey: ", err)
    return
  end
	
  return pkey
end

And its callback function for the cache:

local function readFromFile(file_location)
  local content, err = pl_file.read(file_location)
  if not content then
    ngx.log(ngx.ERR, "Could not read file contents", err)
    return nil, err
  end

  return content
end

Feel free to ask questions once you have given a good look over of the source.

1 Like

christian_chostak, did you solve this problem if it is, can I know how to do this, I got quitely similar problem. Very appreciated with your reply @christian_chostak