How to retrieve the data stored in kong.cache

I have used kong.cache to cache my data as shown below:

local credential_cache_key = kong.db.keyauth_credentials:cache_key(key)
local credential, err = kong.cache:get(credential_cache_key, nil, load_credential,
key)

load_credential function:
local function load_credential(key)
local creds, err = kong.db.keyauth_credentials:find_all {
key = key
}
if not creds then
return nil, err
end
return creds[1]
end

After if I try to retrieve the data from cache I am not able to do it.

Please let me know what is the syntax to retrieve data from kong.cache.

Can you have a look at this @thibaultcha please.

Please look at this @jeremyjpj0916

Phew lets get some code syntax coloring in here:

local credential_cache_key = kong.db.keyauth_credentials:cache_key(key)
local credential, err = kong.cache:get(credential_cache_key, nil, load_credential,
key)

load_credential function:
local function load_credential(key)
local creds, err = kong.db.keyauth_credentials:find_all {
key = key
}
if not creds then
return nil, err
end
return creds[1]
end

As for your issue what are you trying to accomplish exactly? This seems like a bit of attempted duplication of code to the key auth plugin Kong offers.

I suggest taking a look here at their call back function here:

And where the cache:get is invoked here:

Are you intending to do some extra processing or adding different logic to the key auth stored database records? Not sure why a find_all would need to be invoked when you already have the lookup key, as kong uses select_by_key ?

The best advice I can offer is do lots of debug print statements to stdout early on for lots of variables, that tells you a lot about whats happening.

Thank you for your kind response @jeremyjpj0916, sorry for bothering you.

I have a simple use case that is to store a token(value) in database as well as in kong.cache from my customised kong plugin for the first request to my upstream server. For all the subsequent request where my plugin is enabled I want to retrieve the token only from cache(untill the data stored in database is present) to reduce some processing time. I tried to copy the logic from key-auth plugin but it was futile.

I tried all the possible ways but I am not able to make it work since, kong.cache:probe(key) as well as kong.cache:get(key) is throwing error.

Please help me in this, thank you in advance.


© 2019 Kong Inc.    Terms  •  Privacy  •  FAQ