Skip to content

sharedCache.setValue

Signature: sharedCache.setValue(key, value, lifespan*, namespace*, global*, persist*)

Returns: void

Set the value associated with the key into an in-memory, thread-safe, cache.

When no namespace is specified, the value is only accessible from inside the channel in which it is set. When a namespace is specified, the value is accessible to all channels in the same zone as the channel in which it is set. When global is set to true, the value is accessible to all channels across all zones.

Note

When global is set to true and no namespace is supplied, one shared global namespace is used, which every channel in every zone can read and write. Supply a namespace to keep global values apart.

Note

lifespan is optional. When not specified, {never expires} defaults to 20 years.

Note

namespace is optional. When not specified, {channel ID} scopes the value to the channel running the script, so no other channel can see it.

Note

persist is optional. When not specified, {system default} is the Default Storage setting on the System Config page under Shared Cache Settings.

Parameters

Type Name Description Default
String key a unique string used to identify the cached value
Object value the value that will be stored in the cache
Long lifespan* (optional) time in seconds that the value will live in the cache before it is removed {never expires}
String namespace* (optional) a unique string used to limit the scope of the value {channel ID}
Boolean global* (optional) specifies whether or not the value is stored in the global cache false
Boolean persist* (optional) specifies whether or not the value is stored in the database {system default}

Example

// Example: Store a value in the shared cache with a time-to-live (TTL).

// Create a value to cache
var hashMap = new java.util.HashMap();
hashMap.put("Today", new java.util.Date(qie.getSystemTimeMilliseconds()));
hashMap.put("Jon's Bday", qie.deduceDate("2001-09-17"));

// Store the value in the shared cache for 600 seconds
sharedCache.setValue(
    "the map", // key
    hashMap,   // value
    600        // lifespan (seconds)
);

// Retrieve the cached value
var mapRetrieved = sharedCache.getValue("the map");

// Access values from the cached object
var today = mapRetrieved.get("Today");
var birthday = mapRetrieved.get("Jon's Bday");