Use the N/cache module to enable temporary, short-term storage of data. Using a cache improves performance by eliminating the need for scripts to repeatedly retrieve the same piece of data. You can use this module to build a cache to store and retrieve string values using a specific key.
You can create a cache that is available (1) to the current script only, (2) to all server scripts in the current bundle, or (3) to all server scripts in your NetSuite account. Data is stored in the cache according to its time to live (ttl) specified in the Cache.put(options) method.
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:
- Using cache can reduce the memory that your script consumes when running.
- 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).
Note:
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.
Conclusion
The ‘N/cache’ module is a powerful tool that can elevate your SuiteScript skills to an entirely new level.