scottvonduhn
04/24/2019, 4:49 PMN/cache
, and found the Netsuite documentation for it to be fairly useless. It shows an example that caches a large JSON string to a single static key. Generally I think of the cache as a non-persistent key value storage. I created a sample module which caches currency symbols by their internal ids.
define(['N/cache', 'N/search'], function (cache, search) {
const CACHE_NAME = 'currency_cache';
function getCache() {
return cache.getCache({
name: CACHE_NAME,
scope: cache.Scope.PUBLIC
});
}
function cacheLoader(context) {
return JSON.stringify(search.lookupFields({
type: search.Type.CURRENCY,
id: context.key,
columns: 'symbol'
}));
}
function get(currencyId) {
return util.extend(
{ getSymbol: function () { return this.symbol; } },
JSON.parse(getCache().get({
key: String(currencyId),
loader: cacheLoader
}))
);
}
return {
get: get
};
});
A small test script that uses it looks like this:
define(['./currencyCache'], function (currencyCache) {
function execute() {
var c;
var currency;
for (c = 1; c < 7; c += 1) {
currency = currencyCache.get(c);
log.debug(c, currency.getSymbol());
}
}
return {
execute: execute
};
});