WebApps 2

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

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
// 1.Retrieve an instance of the Cache API
import cache from "@sitevision/api/server/cache";

// 2. Get the cache
// a) Get local cache
const localCache = cache.getCache();

// b) Get shared cache
const sharedCache = cache.getSharedCache("aGlobalNamespace");

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.


localOrSharedCache.get('aKey', 10, () => {
   // What to do if there is no data in the cache for the requested key
});

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.

import { renderToStaticMarkup } from 'react-dom/server';
import router from '@sitevision/api/common/router';
import requester from '@sitevision/api/server/Requester';
import cache from '@sitevision/api/server/cache';
import App from './components/App';

const myCallback = () => {
   requester
      .get('https://jsonplaceholder.typicode.com/posts/1/comments')
      .done((result) => {
         return result;
      })
      .fail(() => {
         return 'No data';
      });
};

router.get('/', (req, res) => {
   const localCache = cache.getCache();
   const myData = localCache.get('aKey', 10, () => {
      return myCallback();
   });

   res.send(
      renderToStaticMarkup(
         App({
            myData,
         })
      )
   );
});

purge(key)

Removes a value from the cache


Argument

Description

key

A key to remove