I have used `N/cache`, and found the Netsuite docu...
# suitescript
s
I have used
N/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.
Copy code
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:
Copy code
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
    };
});