Overview
The JSMA cache allows retrieving and setting values of items in the cache.
What can JSMA cache do for me?
With the JSMA cache you can retrieve and set values of items in the cache.
Index
Usage
Here is an example of reading and writing the JSMA cache
function renderContent(contentName, cacheable, expiry) { [...] //if portal defines a remapping use the remapped contentName var mappedContentName = PortalUtils.getRemappableAttribute(contentName); if (cacheable === "true" && (system.isDeveloperCacheActive || system.isCacheActive)) { var c = cache.get("portal.renderContent#" + mappedContentName, true); if (c) { return c; } } var content = contents.findByName(mappedContentName); [...] var res = content.content; if (cacheable === "true" && (system.isDeveloperCacheActive || system.isCacheActive)) { cache.set("portal.renderContent#" + mappedContentName, res, true, parseInt(expiry)); } return res; }
Methods
The JSMA cache provides the following methods:
string cache.get(key, [force])
Retrieves value of the specified key if caching is enabled. If caching is disabled but you still wish to retrieve a value, you can set the force parameter to true.
Example
function renderContent(contentName, cacheable, expiry) { [...] var mappedContentName = PortalUtils.getRemappableAttribute(contentName); if (cacheable === "true" && (system.isDeveloperCacheActive || system.isCacheActive)) { var c = cache.get("portal.renderContent#" + mappedContentName, true); if (c) { return c; } } [...] }
Parameter key:string (required)
The key the should be read out from cache
Parameter force (optional)
Set this to true, if caching is disabled but you still wish to retrieve a value
cache.set(key, value, [force], [expiryTimeInSeconds])
Writes a (key, value) pair into cache if caching is enabled. If caching is disabled but you still wish to write into cache, you can set the force parameter to true.
Example
function renderContent(contentName, cacheable, expiry) { [...] var content = contents.findByName(mappedContentName); [...] var res = content.content; if (cacheable === "true" && (system.isDeveloperCacheActive || system.isCacheActive)) { cache.set("portal.renderContent#" + mappedContentName, res, true, parseInt(expiry)); } return res; }
Parameter key (required)
The key that should be set into cache
Parameter value (required)
The value the key should have in cache
Parameter force (optional)
Set this to true, if caching is disabled but you still wish to write into cache
Parameter expiryTimeInSeconds (optional)
The time in seconds of how long the key is kept in cache