get
  • 02 Feb 2021
  • 1 Minute to read
  • Contributors
  • Dark
    Light
  • PDF

get

  • Dark
    Light
  • PDF

Article summary

Description

Returns an item from cache (null if doesn't exist).


Input Parameters

NameTypeRequiredDescription
keyTextYesUnique item identifier
[default]AnyNoDefault value to use if value doesn't exist

Returns

Type: Any

Returns the specified item from cache or null if it does not exist or is empty.

If a default value is specified and the item doesn't exist, then the default value is inserted into the cache and returned as the value. This offers a simple way to avoid checking if an item exists before retrieving it.


Examples

// get score value from cache
// returns null if doesn't exist
var score = cache.get("score");
// get score value from cache
// returns a default value of 85 if doesn't exist
var score = cache.get("score", 85);
// get person object from cache
// we assume a person object was previously inserted into cache
var person = cache.get("myPerson");

// print person's first name to the debug console
debug.log(person.firstName);

Remarks

Use this function when you want to retrieve information that has been stored into cache from previous script executions.

Definition

Cache is a programming term referring to a simple database of information. A cache is a collection of associated key-value pairs. The key must be a unique identifier and the value is any object you want to store. Once stored, you can retrieve these objects using their keys.


What's Next