Using Global Variables with Cache Module in Map/Reduce Script

Introduction

In Map/Reduce scripts, managing data efficiently is crucial for performance. One way to optimize data handling is by using global variables with the cache module. This article explains how to implement this in your Map/Reduce scripts.

What is a Global Variable?

A global variable is a variable that is accessible throughout the entire script. It retains its value across different stages of the script, making it useful for storing data that needs to be shared or reused.

What is the Cache Module?

The cache module provides a way to store and retrieve data efficiently. It helps in reducing redundant data processing by caching frequently accessed data.

Steps to Use Global Variables with Cache Module

  1. Import the Cache Module:
   const cache = require('N/cache');
  1. Define a Global Variable:
   let globalArray = [];
  1. Initialize Cache:
   const scriptCache = cache.getCache({ name: 'globalArrayCache' });
  1. Store Data in Cache:
   scriptCache.put({
       key: 'globalArray',
       value: JSON.stringify(globalArray),
       ttl: 3600 // Time to live in seconds
   });
  1. Retrieve Data from Cache:
   let globalArray = JSON.parse(scriptCache.get({ key: 'globalArray' }));
  1. Use Global Variable to Store Cached Data:
   function processData() {
       globalArray = JSON.parse(scriptCache.get({ key: 'globalArray' }));

       if (!globalArray) {
           globalArray = [];
           scriptCache.put({
               key: 'globalArray',
               value: JSON.stringify(globalArray),
               ttl: 3600
           });
       }

       // Use globalArray in your script
       log.debug('Global Array:', globalArray);
   }

Example Usage in Map/Reduce Script

/**
 * @NApiVersion 2.1
 * @NScriptType MapReduceScript
 */
define(['N/runtime', 'N/record', 'N/cache', 'N/search'],
    /**
     * @param{runtime} runtime
     * @param{record} record
     * @param{cache} cache
     * @param{search} search
     */
    (runtime, record, cache, search) => {

        const getInputData = (inputContext) => {
            // Initialize the global array with an empty array
            let globalArray = [];

            // Store the global array in the cache
            let scriptCache = cache.getCache({ name: 'globalArrayCache' });
            scriptCache.put({
                key: 'globalArray',
                value: JSON.stringify(globalArray),
                ttl: 3600 // Time to live in seconds
            });

            // Define the input data for the Map/Reduce script
            let salesorderSearchObj = search.create({
                type: "salesorder",
                settings: [{ "name": "consolidationtype", "value": "ACCTTYPE" }],
                filters: [
                    ["type", "anyof", "SalesOrd"],
                    "AND",
                    ["status", "anyof", "SalesOrd:D", "SalesOrd:B"],
                    "AND",
                    ["mainline", "is", "T"]
                ],
                columns: [
                    search.createColumn({ name: "internalid", label: "Internal ID" })
                ]
            });

            let salesOrderIds = [];
            let searchResultCountSO = salesorderSearchObj.runPaged().count;
            log.debug("salesorderSearchObj result count", searchResultCountSO);
            salesorderSearchObj.run().each(function (result) {
                let salesOrderId = result.getValue({ name: 'internalid' });
                salesOrderIds.push(salesOrderId);
                return true;
            });
            log.debug("salesOrderIds", salesOrderIds);
            return salesOrderIds;
        }

        const reduce = (reduceContext) => {
            // Retrieve the global array from the cache
            let scriptCache = cache.getCache({ name: 'globalArrayCache' });
            let globalArray = JSON.parse(scriptCache.get({ key: 'globalArray' }));

            let salesOrder = JSON.parse(reduceContext.values[0]);
            log.debug("salesOrder", salesOrder);
            let rec = record.load({
                type: 'salesorder',
                id: salesOrder,
            });
            let recId = rec.id;

            // Add the sales order ID to the global array
            globalArray.push(recId);

            // Store the updated global array in the cache
            scriptCache.put({
                key: 'globalArray',
                value: JSON.stringify(globalArray),
                ttl: 3600
            });
        }

        const summarize = (summaryContext) => {
            // Retrieve the global array from the cache
            let scriptCache = cache.getCache({ name: 'globalArrayCache' });
            let globalArray = JSON.parse(scriptCache.get({ key: 'globalArray' }));

            log.debug("globalArray", globalArray);

            // Summarize the results
            log.audit({
                title: 'Summary',
                details: 'Map/Reduce script completed successfully'
            });
        }

        return { getInputData, reduce, summarize }
    });

Conclusion

Using global variables with the cache module in Map/Reduce scripts can significantly improve performance by reducing redundant data processing. Follow the steps outlined above to implement this in your scripts.

Leave a comment

Your email address will not be published. Required fields are marked *