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. |
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; } |
The JSMA cache provides the following methods:
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.
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; } } [...] } |
The key the should be read out from cache
Set this to true, if caching is disabled but you still wish to retrieve a value
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.
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; } |
The key that should be set into cache
The value the key should have in cache
Set this to true, if caching is disabled but you still wish to write into cache
The time in seconds of how long the key is kept in cache
Retrieves a key from the cache if available and returns it. If the key is not found, the supplied function is executed and the result is saved to the key and then returned.
This saves you the hassle of manually checking for a key and setting it with some value if it hasn't been found and then using that value further.
cache.getOrSet('myCacheKey', function() { // an expensive or long running function // ... }, true, 86400); |
The key that should be set into cache
The key that should be set into cache
Set this to true, if caching is disabled but you still wish to write into cache
The time in seconds of how long the key is kept in cache