The ZoneCache stores data in a shared cache. This cache is in the same zone (the same data center or cluster) as your API - it's not globally distributed, but rather designed to be low-latency. Common uses include caching configuration, session, or other data that needs to be frequently accessed with low latency.
The ZoneCache can store any JSON serializable data. Each cached item has a time-to-live (TTL) after which it expires and is removed from the cache. Each cached object can be up to 512 MB in size.
There's an demonstration of ZoneCache use in the Per User Rate Limits Using a Database example.
Constructor
Code
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.
Code
put
Stores a value in the cache with a time-to-live (TTL) in seconds. The data will be JSON serialized.
Code
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.
Code
Example
Code
Performance tips
When writing to the cache, you may not want to await the operation to
complete. This can improve response times:
Code
Always catch errors when using the fire-and-forget pattern to avoid unhandled promise rejections.
When to use ZoneCache
Use ZoneCache when you need low-latency access to frequently used data that's stored in a single zone. It's ideal for caching configuration data, session information, or results from external API calls that are expensive or slow to retrieve.
Avoid using ZoneCache for large datasets or data that needs to be globally distributed, as it's designed for small, simple objects within a single zone. It is also not suitable to be used as an OLTP to read/write important data, it's just a cache - very useful but not a database or key/value store.
Limits
For full Zuplo platform limits see the Zuplo Limits documentation.
- Maximum size per cached item: 512 MB
- Maximum cache calls per request: 1000