JSMA cache

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:boolean (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:string (required)

The key that should be set into cache

Parameter value:string (required)

The value the key should have in cache

Parameter force:bool (optional)

Set this to true, if caching is disabled but you still wish to write into cache

Parameter expiryTimeInSeconds:int (optional)

The time in seconds of how long the key is kept in cache

cache.getOrSet(key, fun, [force], [expiryTimeInSeconds])

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.

Example

cache.getOrSet('myCacheKey', function() {
	// an expensive or long running function
	// ...
}, true, 86400);

Parameter key:string (required)

The key that should be set into cache

Parameter fun:function (required)

The key that should be set into cache

Parameter force:bool (optional)

Set this to true, if caching is disabled but you still wish to write into cache

Parameter expiryTimeInSeconds:int (optional)

The time in seconds of how long the key is kept in cache