Cache.get(options)

Note:

The content in this help topic pertains to SuiteScript 2.0.

Method Description

Retrieves a string value from the cache. The value retrieved is identified by a key that you pass using the options.key parameter. If a requested value is not present in the cache, the system calls the user-defined function identified by the options.loader parameter. This loader function should provide logic for retrieving a value that is not in the cache. For an example, see N/cache Module Script Samples.

Returns

String or null

Supported Script Types

Server scripts

For additional information, see SuiteScript 2.x Script Types.

Governance

1 unit if the value is present in the cache

2 units if the loader function is used

Module

N/cache Module

Parent Object

cache.Cache

Sibling Object Members

Cache Object Members

Since

2016.2

Parameters

Note:

The options parameter is a JavaScript object.

Parameter

Type

Required / Optional

Description

options.key

string

required

The key corresponding to the value to be retrieved from the cache. This value cannot be null. For example, a ZIP code can be used as the key to retrieve city names.

options.loader

function

optional

A user-defined loader function that is used to return the requested value when it is not present in the cache. When the loader function retrieves a value, the system automatically places that value in the cache. For this reason, you should use a loader function as the primary means of populating the cache. For an example, see N/cache Module Script Samples.

If the value returned by the loader function is not a string, the system uses JSON.stringify() to convert the value before it is placed in the cache and returned. The maximum size of a value that can be placed in the cache is 500KB.

When no loader function is specified and a value is missing from the cache, the system returns null.

options.ttl

number

optional

The maximum duration, in seconds, that a value retrieved by the loader function can remain in the cache. Note that the value may be removed from the cache before the ttl limit is reached.

The minimum value is 300 (five minutes) and there is no maximum. The default ttl value is no limit.

Important:

A cached value is not guaranteed to stay in the cache for the full duration of the ttl value. The ttl value represents the maximum time that the cached value may be stored.

Syntax

Important:

The following code sample shows the syntax for this member. It is not a functional example. For a complete script example, see N/cache Module Script Samples.

          //Add additional code 
...
var myCache = cache.getCache({
    name: 'temporaryCache',
    scope: cache.Scope.PRIVATE
});

var myValue = myCache.get({
    key: 'keyText',
    loader: loaderFunction,  // Specify the name of your loader function (see the N/cache Module Script Sample
    ttl: 18000
});
...
//Add additional code 

        

Related Topics

cache.Cache
N/cache Module
SuiteScript 2.x Modules
SuiteScript 2.x

General Notices