Cache API
Cache API can improve RESTApp performance by offering short term in-memory storage of data. This is particularily useful when the RESTApp is executing time-consuming tasks, e.g. fetching data from third party over http.
The Cache API is only accessible server-side.
Restrictions
- Only objects which can be represented by JSON can be cached
- An object can be cached up to a maximum of 10 minutes (1-600 second)
- Posts with higher TTL will use the maximum TTL
- A cache post can be maximum 10 000 characters
- Posts larger than the maximum will be fetched but not cached
- Posts larger than the maximum will be fetched but not cached
Caches resides in a volatile, shared memory and data can be evicted whenever memory resources are scarce.
Caching should be used only when actually needed. This applies for expensive or time-consuming tasks (e.g. data fetched over http).
Caching fast/cheap data is contra productive. The processing overhead (locking, eviction etc) might outweigh presumed performance gains and it will consume/pollute memory for nothing.
Accessing a cache
A cache can be either local or shared.
- A local cache is private to all instances of the WebApp or RESTApp
- A shared cache can be accessed by all WebApps and RESTApps on the site
- To access a shared cache a global namespace must be provided
Methods
get(key, TTL, callback)
Returns data from a cache, or if there is no data for the provided key; executes a provided callback function used to populate the cache and return the data.
Argument | Description |
---|---|
key | A key to retrive data from |
TTL | Time To Live (max time until the cached post will be deleted) in seconds (1-3600) |
callback | A function executed if no cached data is found for the key. Should be used to populate the cache with data. The return value from the callback function will be cached and returned. |
A typical scenario where the Cache API will be useful is when the RESTApp is executing time-consuming tasks, e.g. fetching data from third party over http. In such a case the call to the REST-API is made inside the callback argument of the get method.
The example below uses a local cache.
purge(key)
Removes a value from the cache
Argument | Description |
---|---|
key | A key to remove |