The ‘N/cache’ module is a rarely discussed SuiteScript gem. Cache is a type of temporary storage that you can use as you create scripts. Learn with us how to use this special SuiteScript module to enhance your NetSuite coding.

When to Use Cache

You may be wondering when this module might be useful. NetSuite’s help documentation says the following about ‘N/cache’:

Using a cache improves performance by eliminating the need for scripts in your account to repeatedly retrieve the same piece of data.

This tool is primarily helpful on two fronts:

  1. Using cache can reduce the memory that your script consumes when running.
  2. Using cache can improve your script performance.

This is especially applicable when trying to handle large amounts of data. It’s difficult to dictate exactly when you should use cache, but it is an excellent option to have in cases where memory and efficiency are a concern.

At times, developers can be caught between a rock and a hard place. For example, one solution in our code might be to create a giant global object at the top of our script that accumulates massive amounts of useable data. The problem is that you might run out of memory if that object becomes to large (Scheduled and Map Reduce scripts are limited to 50MB, for example). On the other hand, to gather that information every time we need it throughout the script would be quite inefficient.

This is where the cache module can come in handy. Although there may be other situations where we can use the cache, there are a few risks to keep in mind before you go too crazy with this cache module:

The Risks of Cache

Throughout the help documentation on this module, there is a general theme of uncertainty. Your cache may be deleted at any moment without warning. Because NetSuite is storing this cache for you on their servers, there is only a certain amount of bandwidth and time they will give developers. Although some have estimated that cache can live about an entire day, NetSuite doesn’t make any promises.

Imagine, for example, that two developers for the same company read this blog and decide to both use the cache module in massive amounts. One developer might make a scheduled script that repeatedly stores large amounts of data in cache. The other developer might expect his cached data to be accessible, but the scheduled script fills the cache bandwidth and unknowingly causes the deletion of that precious data. Although not common, bad things can happen if developers use cache. The loader function that we will describe below will help prevent these kinds of situations.

Because of these risks, you should use cache for static data only (i.e., unchanging and stable kinds of data).


NetSuite also allows for data storage with the ‘N/runtime’ module using runtime.getCurrentSession().set(), which we will likely cover in a future tutorial. I personally recommend using session storage for more general data about the user, rather than specific data related to record and script data.

How to Use Cache

There are essentially two steps to use cache:

  1. cache.getCache()
  2. cache.get()

Step 1: Create or retrieve a cache object with cache.getCache().

const cacheObject = cache.getCache({
    name: 'cacheData',
    scope: cache.Scope.PRIVATE

Here are a few things to note about cache.getCache():

  1. Be sure to assign a variable to the value of the method as in the example above. That variable will allow you to get and set data to and from the cache object.
  2. Create a name for the cache. This lets the function know where to create or find the cache object. You will want to make a key that is unique so another script won’t accidentally overwrite it.
  3. Finally, you can set the scope of the cache. This is optional, but it determines where the cache is accessible from. For example, a PRIVATE scope will only be accessible from the same script it was created in. If you want it to be accessible from any script, set the scope to PUBLIC. If you would like to exclude the cache only to a bundle (or to scripts not in a bundle if being called from outside a bundle), use PROTECTED.

Step 2: Get and populate cache with cache.get()

const data = cacheObject.get({
    key: 'cacheDataForScript', // Or any other key name of choice
    loader: loadCacheData, // Your cache loader to populate any missing data
    ttl: 300 // The time your cache can exist before being deleted

The cache.get() method actually handles both the populating and retrieving of the cache data. If the function can locate existing cache with that key, it will load it into the variable called “data.” If there is no existing cache with that key found, the function will call a function (“loadCacheData()“) to populate that data into the cache, and then load that data into the “data” variable.

Here are some additional details to note about cache.get():

  1. You’ll need to create a custom key. This can be anything you like.
  2. You can optionally set the “Time to Live” (TTL). TTL determines how long the data will be stored in memory. In this example, we set the TTL to 300 seconds (5 minutes). This is the minimum that can be set. If you would like to store the data as long as possible, you can exclude this optional parameter. Keep in mind, however, that there’s no absolute guarantee the data will remain in storage.

Although NetSuite does allow for separate functions to populate and get the cache (cache.put() and cache.get() minus the loader function), we recommend that you use the loader function in most cases.

Here’s a simplified example of what a loader function might look like:

function loadCacheData() {
   let data = 'hello world' // Just get ACTUAL data using this function.
   return data;

Whatever the loader function returns will populate the cache with the parameters specified in cache.get().

The loader function allows us to store data in the cache, which can improve script efficiency and memory usage. If that cache is ever lost for one reason or another, the loader function will simply repopulate it back into cache. This provides a safeguard in case any issues ever come up.

How to Remove Cache


There is also a way to remove data from cache if you ever need to for some reason.

Step 1: Retrieve the Cache object with cache.getCache.

const cacheObject = cache.getCache({
    name: 'userCacheData',
    scope: cache.Scope.PRIVATE

Step 2: Remove Data from the Cache Object.

Once you have assigned the object to a variable, you can remove data with the key you previously used to set data to the object:

    key: 'cacheDataForScript'


The ‘N/cache’ module is a powerful tool that can elevate your SuiteScript skills to an entirely new level. We hope you find this tutorial helpful. Don’t forget to subscribe to our email list to stay informed of our latest blogs!