The N/cache module allows SuiteScript to store and retrieve data in the NetSuite platform cache. Caching reduces repeated API calls, database lookups, and governance usage β significantly improving script performance.
Opening a Cache and Storing Data
define(["N/cache", "N/log"], (cache, log) => {
const myCache = cache.getCache({
name: "myAppCache",
scope: cache.Scope.PROTECTED
});
myCache.put({ key: "configData", value: JSON.stringify({ maxLines: 100, mode: "batch" }), ttl: 3600 });
const val = myCache.get({ key: "configData", loader: () => JSON.stringify({ maxLines: 50 }) });
log.debug("Cached value", val);
return {};
});
Cache Scope Options
| Scope | Description |
|---|---|
cache.Scope.PRIVATE | Accessible only to the current script and deployment |
cache.Scope.PROTECTED | Accessible to all scripts in the same bundle |
cache.Scope.PUBLIC | Accessible to all scripts in the account |
Key Methods
| Method | Description |
|---|---|
cache.getCache(options) | Get or create a named cache partition |
cacheObj.put(options) | Store a value (max 500KB, TTL in seconds) |
cacheObj.get(options) | Retrieve a cached value (with optional loader) |
cacheObj.remove(options) | Remove a specific key from cache |