Sitevision Developer
Log in

Log in

cache API

Cache API can improve WebApp performance by offering short term in-memory storage of data. This is particularily useful when the WebApp 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 max TTL threshold. Default max threshold* is 10 minutes (1-600).
    • 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

(* Note! The system configuration can be altered to adjust the max TTL threshold but the absolute max for such customization is 1 hour, i.e. 1-3600)

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 WebApp 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


Did you find the content on this page useful?