We are developing a custom plugin and in that plugin we wanted to store some data in in-memory and we want to fetch that data from in-memory. How to set and get some data to in-memory while using kong DB-less mode.
Basically we are looking for kong solution in custom plugin, where we are can set and get some data in in-memory. This data so be retained for entire pod life cycle. If pod dies, then only data can be destoryed but as long as same pod is running, this data should be retained for available for all incoming request to be used.But we are using DB less mode of kong.
You can use kong.cache
to get and save some data to cache
There are 2 levels of cache available in kong
- L1: Lua memory cache - local to an Nginx worker process. This can hold any type of Lua value.
- L2: Shared memory cache (SHM) - local to an Nginx node, but shared between all the workers. This can only hold scalar values, and hence requires (de)serialization of a more complex type such as Lua tables.
You can check out the docs here
kong.cache
has some methods to store and get data from that cache key you set
To get data from a cache key you can use the below code
-- Get
opts = {ttl = 3}
kong.cache:get(key, opts, callback_func, callback_func_args)
To set some data to cache use
-- set
opts = {ttl = 3}
kong.cache:get(key, opts, function() return values end) -- values = the value you want to save
kong
is a global object here.