I. Module overview

Temporary storage of short-term data, suitable for all server script types.

2. Cache module

1. Module method

Cache. GetCache (options) method

Returns the cache object with the specified name. If the cache object exists, the method returns the cache object, otherwise the system creates a new cache object

parameter type Required/optional describe
options.name string necessary The name of the cache object to retrieve or create, with a maximum size of 1 KB.

| options. The scope | string optional | | Settings cache availability. Using cache. The Scope to set this value, the default value is the cache. The Scope. The PRIVATE |

var myCache = cache.getCache({
    name: 'temporaryCache'.scope: cache.Scope.PRIVATE
});
Copy the code

2. Module attributes

cache.Scope

A string value describing the availability of a cache object

value describe
PRIVATE The cache object only applies to the current script, default

| PROTECTED | cache object only for some scripts are available: If the script is part of the bundle, the cache can be used for all the scripts in the same bundle If the script is not in the bundle, the cache can be used to not all the scripts in any bundle | | PUBLIC | cache object can be used for all the server script | in NetSuite account

Cache object

1. Object methods

Cache. The put (options) method

Put a value into a cache object Returned value: void Supported script type: server Script Usage: 1 unit It is recommended to use the options.loader parameter of the cache.get () method to put a value into a cache object

The scope of type Required/optional describe
options.key string The necessary The identifier of the value retrieved from the cache object. This value cannot be null.
options.value string The necessary Values placed in the cache. If the value is not a string, the system converts it using json.stringify () and then puts it in the cache. The maximum size of this value is 500KB.

| options. TTL | number | | the value of the optional can be retained in the cache object of maximum duration (in seconds). Note that this value may be deleted before the TTL reaches the limit. The minimum is 300 (five minutes) and there is no maximum. The default TTL value is unlimited. There is no guarantee that cached values will remain in the cache for the entire duration of the TTL value. The TTL value represents the maximum time that cached values can be stored. Cached data is not persistent data, proposed to the Cache. The get (options) method and the options. Loader | parameters are used together to set and retrieve data

var myCache = cache.getCache({
    name: 'temporaryCache'.scope: cache.Scope.PRIVATE
});
myCache.put({
    key: 'keyText'.value: 'valueText'.ttl: 300
});
Copy the code

Cache. Get (options) method

Retrieves a string value from the cache object, identified by the options.key argument. This value is returned if it exists in the cache object. Otherwise, user-defined methods that call the options.loader parameter are returned: string/null Supported script type: Server Script Usage: If the value already exists in the cache object, the usage is 1. If the method in the Loader parameter is used, the usage is 2

parameter type Required/optional describe
options.key string The necessary The identifier to retrieve a value from a cache object. The value cannot be null.

| options. Loader | function | is optional, but it is strongly recommended that | load user-defined method, if the cache object does not exist in the requested values, it will call the user custom method. When a user-defined method obtains a value, the system automatically adds the value to the cache object. Therefore, NetSuite recommends using loader as the primary method for populating cache objects. If a loaded program returns a value other than a string, the system converts the value using json.stringify (), then puts it into the cache object and returns the value. The maximum size of values that can be put into the cache is 500KB. Null is returned if no method is specified to load another program and a value is missing from the cache object. TTL | | | options. Number the | | optional by the user program loader set retrieved values can be keep in a cache of maximum duration (in seconds). Note that this value may be removed from the cache before the TTL limit is reached. The minimum is 300 (five minutes) and there is no maximum. The default TTL value is unlimited. Note: Caching is not guaranteed. |

var myCache = cache.getCache({
    name: 'temporaryCache'.scope: cache.Scope.PRIVATE
});
myCache.get({
    key: 'temporaryCache'.loader: function() {
        return file.load({
          path: 'src/res/balance_sheet/' 
        }).getContents();
    },
    ttl:300
})
Copy the code

Cache. Remove (options) method

Delete a value from the cache object Returned value: void Supported script type: Server Script Usage: 1 unit

parameter type Required/optional describe
options.key string The necessary The identifier of the value to be deleted.
var myCache = cache.getCache({
    name: 'temporaryCache'.scope: cache.Scope.PRIVATE
});
myCache.remove({
    key: 'keyText'
});
Copy the code

Four, sample code

var myPeriod = runtime.getCurrentScript().getParameter('custscript_mcds_period');
var myCache = cache.getCache({
  name : 'global_enddate_value_mtrl'
});
// Get the period end date from the cache
var periodEndDate = myCache.get({
  key : 'gPeriodEndDate_mtrl'.loader:function(){
    return search.lookupFields({
      type : 'accountingperiod'.id : myPeriod,
      columns : [ 'enddate'] }).enddate; }});// Get goods data
var endInvValue = getItemInvValue(item, periodEndDate);
Copy the code