How to delete caching after some specified time?

How to delete entity caching.

token, err = singletons.cache:get(token_cache_key, nil,
            load_token_into_memory, ngx.ctx.api,
            access_token)

In above example, It cache the token data but expiration time is not fixed.
So is there any way to passed expire time in cache:get() function or you @all have any other suggestion for delete the cache after specific time?

Thanks you

Hi,

The second parameter (nil in your example) is an options hash. You can use the ttl option there to set a “time to leave”.

singletons.cache:get(token_cache_key, { ttl = 5 },
                     load_token_into_memory, ngx.ctx.api, access_token)

Internally this method is using ngx.shared.DICT.set, where ttl is passed as the exptime parameter there - so it is in seconds.

It is worth pointing out that internally, singletons.cache actually relies on lua-resty-mlcache.

(which itself relies on lua-resty-lrucache and said lua_shared_dict as pointed out by @kikito.)

@thibaultcha,
Hello,
kong.singletons is resurrecting a stale item from the cache from shm even there is no resurrect_ttl is specified.

How to avoid this behavior?

if I need to declare my own mlcacahe instance to avoid this ( by not specifiying resurrect_ttl), what is the best way to inject in to kong’s init so this mlcache instance is shared across all the workers.

Thank you.

Hi, @rsvenkatesh, next time please consider opening a new thread instead of asking a new question in an old one - it’s easier to discover for others this way.

In case you are still having that problem: the default value for resurrect_ttl can be controlled globally by changing the relevant config option in the config file:

If you want to tweak the ressurect_ttl value for an individual request, leaving the rest unaltered, you can also do that by using the options parameter, the same way as with the ttl:

singletons.cache:get(token_cache_key, { resurrect_ttl = 0 },
                     load_token_into_memory, ngx.ctx.api, access_token)

I have tried with resurrect_ttl = 0 already, that doesn’t seems to help. Only way is not even mentioning resurrect_ttl. After I have instance of mlcache without specifying anything to resurrect_ttl, things seems to work as expected.

Thank you.