Managing caches effectively is crucial for maintaining optimal performance in applications that rely on frequent data access. This article demonstrates how to implement a function that clears all created caches in a NetSuite environment. This method ensures that outdated or unnecessary data is removed, allowing for fresh data retrieval when needed.
Removing All Created Caches
The removeAllCaches method is designed to clear all cached data related to vendors, employees, and project codes. This function uses predefined cache objects and a utility function to remove all entries from each cache.
Key Steps in the Process
- Define Cache Objects: Cache objects are created for vendors, employees, and project codes using unique names.
- Clear Cache Utility Function: A utility function,
clearCache, is defined to remove all keys from a given cache object. - Clear All Caches: The caches are cleared by invoking the
clearCachefunction with the respective cache objects and keys. - Return Status: The function returns a success status if all caches are cleared successfully, or an error status if an exception occurs.
Code Implementation
Below is the implementation of the removeAllCaches function:
/**
* Removes all created caches.
*
* @returns {Object} The status of the cache clearance.
*/
removeAllCaches() {
try {
// Define the cache objects with their unique names
let vendorCache = cache.getCache({ name: 'vendorCache' });
let employeeCache = cache.getCache({ name: 'employeeCache' });
let projectCodeCache = cache.getCache({ name: 'projectCodeCache' });
log.debug("vendorCache", vendorCache);
log.debug("employeeCache", employeeCache);
log.debug("projectCodeCache", projectCodeCache);
// Function to remove all keys from a given cache
function clearCache(cacheObject, cacheKey) {
cacheKey.forEach(key => {
cacheObject.remove(key);
});
}
// Clear all caches
clearCache(vendorCache, dataModel.getResults.getAllCacheKey("vendor"));
clearCache(employeeCache, dataModel.getResults.getAllCacheKey("employee"));
clearCache(projectCodeCache, ["projectCode"]);
return { status: "SUCCESS", reason: "ALL_CACHES_CLEARED", data: [] };
} catch (e) {
log.error("Error @apiMethods @removeAllCaches", e);
return { status: "ERROR", reason: "Error @apiMethods @removeAllCaches", data: [] };
}
}
Utility Function: Fetching All Cache Keys
To clear specific caches, the getAllCacheKey utility function retrieves all keys based on the record type. This function performs a search to collect internal IDs that represent cache keys.
/**
* Get all cache key based on the type.
* @param {string} type - The type of the record to be searched.
* @returns {Array} - An array of internal IDs.
*/
getAllCacheKey(type) {
try {
let internalIds = [];
let dataSearch = search.create({
type: type,
columns: ["internalid"]
});
dataSearch.run().each(function (result) {
internalIds.push(result.getValue({ name: "internalid" }));
return true;
});
log.debug("internalIds", internalIds);
return internalIds;
} catch (e) {
log.error("Error @getResults @getAllCacheKey", e);
return [];
}
}
Conclusion
By implementing the removeAllCaches function, you can efficiently manage and clear caches in your NetSuite environment. This approach helps in maintaining optimal performance by ensuring that outdated or unnecessary data is removed, allowing for fresh data retrieval when needed. The use of utility functions like getAllCacheKey further enhances the flexibility and scalability of your cache management strategy.