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.

·3 min read·View Oracle Docs

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 TypeNameReturn Type / Value TypeSupported Script TypesDescription
Method[Cache.get(options)]stringServer scriptsRetrieves 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)]stringServer scriptsStores a value in the cache; if the value is not a string, it's converted using JSON.stringify().
Method[Cache.remove(options)]stringServer scriptsRemoves a specific value from the cache.
Property[Cache.name]stringServer scriptsThe assigned name of the cache for identification.
Property[Cache.scope]stringServer scriptsIndicates 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?
Caches created with the N/cache module can be scoped to the current script, the current bundle, or the entire NetSuite account, allowing developers to tailor cache availability based on their needs.
How long can data remain in a cache created with the N/cache module before it is removed?
Data remains in a cache as long as its time-to-live (ttl) value specifies, after which it becomes eligible for removal. Setting ttl helps prevent stale data and manage cache size.
What happens if I try to cache a non-string value with the N/cache module?
If a non-string value is cached using the N/cache module, it is automatically converted to a string using JSON.stringify() before storage.
Can I use the N/cache module methods in client scripts?
No, the methods provided by the N/cache module, such as get, put, and remove, are supported only in server scripts.
Source: Cache Object 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 →