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.
Constructor
ts
Creates a new cache instance for the specified zone.
name
- A unique identifier for the cachecontext
- The ZuploContext objectT
- The type of data stored in the cache (defaults tounknown
)
Methods
get
Retrieves a value from the cache. Returns undefined
if the key doesn't exist
or has expired.
ts
put
Stores a value in the cache with a time-to-live (TTL) in seconds. The data will be JSON serialized.
ts
:::note Objects that don't serialize cleanly to JSON (like the Headers
object)
won't be readable after storage. :::
delete
Removes a value from the cache.
ts
Example
ts
Performance Tips
When writing to the cache, you may not want to await
the operation to
complete. This can improve response times:
ts
Always catch errors when using the fire-and-forget pattern to avoid unhandled promise rejections.