Cache Object Members in SuiteScript with N/cache Module
Cache Object Members in SuiteScript facilitate temporary data storage with the N/cache module, enhancing script performance.
TL;DR
The Cache Object Members in SuiteScript utilize the N/cache module to enable temporary storage of data, improving script performance by reducing redundant data retrievals. Caches can have specified availability and expiration times, making them an efficient tool for developers.
How Does the N/cache Module Work?
The N/cache module is used for temporary data storage, allowing scripts to cache values and avoid repeated fetching of the same data. This approach can significantly enhance the performance of server scripts by storing frequently used information in a cache that is accessible by keys.
Cache Availability
When creating a cache with the N/cache module, developers can define its scope:
- Current Script Only: Accessible only within the script where it is created.
- Current Bundle: Available to all server scripts contained within the current bundle.
- Entire Account: Usable across all server scripts in your NetSuite account.
Data in the cache has a time-to-live (ttl) value, which specifies how long the data remains in the cache before it is eligible for removal. This management helps keep cache size in check and can prevent stale data from lingering.
Cache Object Members
The following table details the cache object members available through the N/cache module:
| Member Type | Name | Return Type / Value Type | Supported Script Types | Description |
|---|---|---|---|---|
| Method | [Cache.get(options)] | string | Server scripts | Retrieves a value from the cache based on a specified key. If it's not present, it can invoke a user-defined loading function. |
| Method | [Cache.put(options)] | string | Server scripts | Stores a value in the cache; if the value is not a string, it's converted using JSON.stringify(). |
| Method | [Cache.remove(options)] | string | Server scripts | Removes a specific value from the cache. |
| Property | [Cache.name] | string | Server scripts | The assigned name of the cache for identification. |
| Property | [Cache.scope] | string | Server scripts | Indicates the cache availability (current script, bundle, or account). |
Cache operations are straightforward and are designed to streamline data retrieval, enhancing overall performance in NetSuite scripting.
Best Practices for Using Cache
- Use caching for frequently accessed data that doesn't change often.
- Regularly review cache entries and set appropriate time-to-live values to avoid using outdated data.
- Consider the scope of your cache; limit it to the appropriate level to reduce memory consumption.
Who This Affects
- Developers: Those creating server-side scripts can benefit significantly from using cache to improve execution speed.
- Administrators: Understanding how cache works can help in managing performance across scripts.
Key Takeaways
- The N/cache module allows for efficient temporary data storage.
- Caches can be scoped to different levels of script access.
- Proper management of cache time-to-live (ttl) is essential.
- Retrieving and storing data in the cache can significantly enhance script performance.
Source: This article is based on Oracle's official NetSuite documentation.
Frequently Asked Questions (4)
What scope options are available for caches created using the N/cache module in SuiteScript?
How long can data remain in a cache created with the N/cache module before it is removed?
What happens if I try to cache a non-string value with the N/cache module?
Can I use the N/cache module methods in client 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