#ZoneCache
The ZoneCache is used to store data in a shared cache, typically local to the Zone your gateway is running (for example, the same data center). This can be used to store small, simple objects. It's excellent for improving the latency of your gateway if you need to access remote data in your policies, such as calling another API in a policy.
There's an demonstration of ZoneCache use in the Per User Rate Limits Using a Database example.
#Constructing the Cache
// create a new cache by specifying a name and // passing current ZuploContext const cache = new ZoneCache("name-of-your-cache", context);ts
#Reading from the Cache
// read from the cache using the key const data = await cache.get("key");ts
#Writing to the Cache
// write to the cache - and keep for 60 seconds await cache.put("key", data, 60);ts
When writing to the cache the data
parameters will be JSON serialized. If your
data does not serialize cleanly to JSON (like the Headers
object does not) you
won't be able to read your data back/
#Deleting from the Cache
await cache.delete("key");ts
TIP On some code paths you may not want to await
the cache to wait for the
operation to complete. For example, when writing data you may choose to write
asynchronously. However, we recommend catching errors if you do this, for
example:
cache.put("key", data, 60).catch((err) => context.log.error(err));ts