apache / apache/openwhisk-apigateway
Improve performance by adding a 2 layered cache
- Dominant language
- Lua
- Stars
- 65
- Forks
- 43
- PR merge metrics
- No merged PRs in 30d
Description
Use this [cachemanager](https://github.com/adobe-apiplatform/api-gateway-cachemanager) module and configure a 2-layered cache in the Gateway:
1. layer 1 : local cache in lua VM using [lua_shared_dict](https://github.com/openresty/lua-nginx-module#lua_shared_dict)
2. layer 2: Redis cache
The README of the repo has this documented but I'm sharing a simplified version here:
For setting up we can add the code bellow to [routing.lua](https://github.com/openwhisk/openwhisk-apigateway/blob/master/api-gateway-config/scripts/lua/routing.lua) and other places that use Redis.
```lua
local cache_cls = require "api-gateway.cache.cache"
ngx.apiGateway.request_cache = cache_cls:new()
-- define a local cache with a max TTL of 10s
local local_cache_max_ttl = 10
local local_cache = require "api-gateway.cache.store.localCache":new({
dict = "cachedrequests",
ttl = local_cache_max_ttl
})
-- define a remote Redis cache with max TTL of 5 minutes
local redis_cache_max_ttl = 300
local redis_cache = require "api-gateway.cache.store.redisSetCache":new({
ttl = redis_cache_max_ttl
})
-- NOTE: order is important as cache stores are checked in the same order
ngx.apiGateway.request_cache:addStore(local_cache)
ngx.apiGateway.request_cache:addStore(redis_cache)
```
Then when storing something in cache use:
```lua
ngx.apiGateway.request_cache:put(key, value)
```
And when reading something from the cache use:
```lua
ngx.apiGateway.request_cache:get(key)
```
The lib will take care of using both cache stores appropriately.
Contributor guide
Assessment
This issue has not been assessed yet.