Use the N/cache module to enable temporary, short-term storage of data. Data is stored in the cache according to its time to live (ttl) specified in the Cache.put(options) method. You can use this module to build a cache to store and retrieve string values using a specific key. The N/cache module is supported by all server script types.
Can also pass data between two entry points use cache module.
Here is an example for transferring data from beforeSubmit to afterSubmit of an UserEvent script.
In this example, we’re using the N/cache module to store the sales order array in a public cache with a TTL of 1 hour.
You can optionally set the “Time to Live” (TTL). TTL determines how long the data will be stored in memory.
// Store sales order array in cache
var cacheKey = ‘salesOrdArrayCacheKey’;
var cacheObj = cache.getCache({
name: ‘myCache’,
scope: cache.Scope.PUBLIC
});
cacheObj.put({
key: cacheKey,
value: JSON.stringify(salesOrdArray),
ttl: 3600 // cache for 1 hour
});
log.debug({
title: ‘Sales Order Array Stored in Cache’,
details: cacheObj.get({ key: cacheKey })
});
In the afterSubmit we can retrieve this sales order array as:
// Retrieve sales order array from cache
var cacheKey = 'salesOrdArrayCacheKey';
var cacheObj = cache.getCache({
name: 'myCache',
scope: cache.Scope.PUBLIC
});
var salesOrdArray = JSON.parse(cacheObj.get({ key: cacheKey }));
log.debug({
title: 'Sales Order Array Retrieved from Cache',
details: salesOrdArray
});