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.
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:
- Script-specific cache: Accessible only by the running script.
- Bundle-wide cache: Available to all server scripts within the current bundle.
- 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 Type | Name | Return Type / Value Type | Supported Script Types | Description |
|---|---|---|---|---|
| Object | cache.Cache | Object | Server scripts | Represents a memory segment for temporary data storage. |
| Method | cache.getCache(options) | cache.Cache | Server scripts | Retrieves an existing cache object or creates a new one if it doesn’t exist. |
| Enum | cache.Scope | enum | Server scripts | Defines the cache's availability scope. |
Cache Object Members
The following members are part of the cache.Cache object:
| Member Type | Name | Return Type/Value Type | Supported Script Types | Description |
|---|---|---|---|---|
| Method | Cache.get(options) | string | Server scripts | Fetches a value from the cache using a provided key. |
| Method | Cache.put(options) | string | Server scripts | Stores a value in the cache. |
| Method | Cache.remove(options) | string | Server scripts | Deletes a value from the cache. |
| Property | Cache.name | string | Server scripts | Specifies the cache's name. |
| Property | Cache.scope | string | Server scripts | Indicates the cache's accessibility level. |
Best Practices
- Define TTLs: When using
Cache.put(options), define attlto 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.loaderparameter in theCache.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?
What permissions are required to use the N/cache module?
How can I prevent stale data in the cache when using the N/cache module?
Is the N/cache module limited to specific types of scripts?
Was this article helpful?
More in Platform
- Style Object Members for Workbook API in NetSuite
The Style object provides customizable properties for workbook formatting in NetSuite, enhancing report and visualization functionality.
- Asynchronous HTTP Requests with SuiteScript Promises
Use SuiteScript to send asynchronous HTTP requests with promises for efficient error handling and response processing.
- Setting Sublist Field Values in SuiteScript
Set values for sublist fields in SuiteScript using the setSublistValue method. Understand required parameters and error handling.
- In This Help Topic
Explore N/log module guidelines, log levels, and script execution logs for efficient logging in SuiteScript.
Advertising
Reach Platform Professionals
Put your product in front of NetSuite experts who work with Platform every day.
Sponsor This Category