N/cache Module for Temporary Data Storage in NetSuite

The N/cache module enables temporary data storage in NetSuite, improving script performance by caching frequently accessed values.

·3 min read·View Oracle Docs

The N/cache module in NetSuite allows developers to utilize temporary, short-term storage of data, significantly improving script performance by reducing the need to repeatedly access the same data. By leveraging this caching mechanism, scripts can retrieve and store string values associated with specific keys, enhancing efficiency and responsiveness.

Why Use the N/cache Module?

Using caching can dramatically enhance performance in scenarios where scripts often access the same piece of data, creating a better user experience and optimizing resource use. The N/cache module offers three levels of data storage:

  1. Script-specific cache: Accessible only by the running script.
  2. Bundle-wide cache: Available to all server scripts within the current bundle.
  3. Account-wide cache: Usable by all server scripts throughout the NetSuite account.

Data is stored according to a defined time-to-live (ttl), which is specified in the Cache.put(options) method, allowing for controlled cache expiry.

N/cache Module Members Overview

The following members comprise the N/cache module:

Member TypeNameReturn Type / Value TypeSupported Script TypesDescription
Objectcache.CacheObjectServer scriptsRepresents a memory segment for temporary data storage.
Methodcache.getCache(options)cache.CacheServer scriptsRetrieves an existing cache object or creates a new one if it doesn’t exist.
Enumcache.ScopeenumServer scriptsDefines the cache's availability scope.

Cache Object Members

The following members are part of the cache.Cache object:

Member TypeNameReturn Type/Value TypeSupported Script TypesDescription
MethodCache.get(options)stringServer scriptsFetches a value from the cache using a provided key.
MethodCache.put(options)stringServer scriptsStores a value in the cache.
MethodCache.remove(options)stringServer scriptsDeletes a value from the cache.
PropertyCache.namestringServer scriptsSpecifies the cache's name.
PropertyCache.scopestringServer scriptsIndicates the cache's accessibility level.

Best Practices

  • Define TTLs: When using Cache.put(options), define a ttl to avoid stale data lingering in the cache beyond its usefulness.
  • Use JSON.stringify(): For non-string values, leverage JSON.stringify() to ensure proper data storage in your cache.
  • Loader Functions: Use the options.loader parameter in the Cache.get(options) method for efficient data retrieval, allowing your script to dynamically load data when needed. This greatly improves performance and reduces memory overhead.

Who This Affects

This module is particularly relevant for:

  • Developers: Utilizing SuiteScript for custom script development.
  • Administrators: Managing performance settings and script efficiency.

Key Takeaways

  • The N/cache module improves script performance by caching frequently accessed data.
  • Offers three cache availability options: script-specific, bundle-wide, and account-wide.
  • Properly defined TTLs and data handling methods are crucial for efficient caching.

Source: This article is based on Oracle's official NetSuite documentation.

Frequently Asked Questions (4)

Do I need to enable a feature flag to use the N/cache module in NetSuite?
The article does not mention any requirement for enabling a feature flag to use the N/cache module. It can be accessed directly in server scripts.
What permissions are required to use the N/cache module?
The article does not specify any particular permissions required to use the N/cache module, but it is primarily intended for developers working with SuiteScript in NetSuite.
How can I prevent stale data in the cache when using the N/cache module?
To prevent stale data, define a time-to-live (TTL) using the `Cache.put(options)` method to control cache expiry, ensuring data does not linger beyond its usefulness.
Is the N/cache module limited to specific types of scripts?
No, the N/cache module can be used in all server scripts, as it is designed to work with server-side SuiteScript development within NetSuite.
Source: N/cache Module Members Oracle NetSuite Help Center. This article was generated from official Oracle documentation and enriched with additional context and best practices.

Was this article helpful?

More in Platform

View all Platform articles →