Optimize the CommerceContext cache for a long running process

Current version: 10.1

Sitecore XC integration with other systems often involves the development of long running or bulk processes that can impact performance. This topic provides you with guidance on how to use the CommerceContext cache in your integration customization in a way that optimizes the performance of long running tasks or processes.

You can optimize performance during long running tasks by refreshing the CommerceContext cache.

Note

The interval at which to refresh the CommerceContext cache ultimately depends on the workload being performed.  As a general guideline, consider refreshing the CommerceContext every 5 iterations of a given task. You can adjust the refresh interval based on test results. 

To refresh the CommerceContext :

  • Add the following snippet to your long running process code:

    RequestResponse
    var currentContext = new CommerceContext(CurrentContext.Logger, CurrentContext.TelemetryClient)
       {
        GlobalEnvironment = CurrentContext.GlobalEnvironment,
        Environment = CurrentContext.Environment,
        Headers = CurrentContext.Headers
    
         };

Example: how to refresh the CommerceContext

The following shows a code sample that generates an arbitrary number of random Price Cards using an API endpoint. The two key sections of the example are marked in bold. Notice that in this example, a context refresh is initiated every 5th item.

RequestResponse
       /// <param name="value">The value.</param>
        /// <returns>A <see cref="CommerceCommand"/></returns>
        [HttpPut]
        [Route("AddBulkPriceCards()")]
        public async Task<IActionResult> AddBulkPriceCards([FromBody] ODataActionParameters value)
        {
            if (!this.ModelState.IsValid || value == null)
            {
                return new ObjectResult("Invalid Model State"); ;
            }
            long.TryParse(value.ContainsKey("count") ? value["count"].ToString() : "10000", out var countResult);
            var priceBookName = value["bookName"].ToString();
            var priceCardDisplayName = String.Empty;
            var priceCardDescription = String.Empty;
            var priceCardName = String.Empty;
            var command = this.Command<AddPriceCardCommand>();
            var currentContext = new CommerceContext(CurrentContext.Logger, CurrentContext.TelemetryClient)
            {
                GlobalEnvironment = CurrentContext.GlobalEnvironment,
                Environment = CurrentContext.Environment,
                Headers = CurrentContext.Headers
            };
            Stopwatch sw = new Stopwatch();
            sw.Reset();
            sw.Start();
            for (long i = 0; i < countResult; i++)
            {
                priceCardName = "name_" + i;
                priceCardDescription = "description_" + i;
                priceCardDisplayName = "displayname_" + i;
                if (i % 5 == 0)
                {
                    currentContext = new CommerceContext(CurrentContext.Logger, CurrentContext.TelemetryClient)
                    {
                        GlobalEnvironment = CurrentContext.GlobalEnvironment,
                        Environment = CurrentContext.Environment,
                        Headers = CurrentContext.Headers
                    };
                }
                await command.Process(currentContext, priceBookName, priceCardName, priceCardDisplayName,
                    priceCardDescription);
            }
            sw.Stop();
            return new ObjectResult($"Done, in {sw.Elapsed.ToString()} ");
        }

Do you have some feedback for us?

If you have suggestions for improving this article,