Multiple calls to the same function to perform a calculation require a better way to handle it

advertisements

So, this is the scenario.

There is a static class which does some calculations based on the data provided.

public static class Calculations
{
  public static object Calculate(IList<Class> listOfClass)
  {
     /// going to database and do calculations....
     return calculatedValue;
  }
}

Now on page load we are making like 20 AJAX requests simultaneously (only using JQUERY) which is calling different actions in a controller. So for example.

public class ClientController
   {
      [HttpPost]
      public IActionResult GetAGraph(Object model)
      {
         var listOfClass = // get data based on the model
         var calculatedValue = Calculations.Calculate(listOfClass);
         var returnObject = //do something with calculatedValue
         return Json(returnObject);
      }

      [HttpPost]
      public IActionResult GetSecondGraph(Object model)
      {
         var listOfClass = // get data based on the model
         var calculatedValue = Calculations.Calculate(listOfClass);
         var returnObject = //do something with calculatedValue
         return Json(returnObject);
      }

      [HttpPost]
      public IActionResult GetThirdGraph(Object model)
      {
         var listOfClass = // get data based on the model
         var calculatedValue = Calculations.Calculate(listOfClass);
         var returnObject = //do something with calculatedValue
         return Json(returnObject);
      }
   }

Its not doing the same thing and the actual code I am not allowed to post because of client terms.

What I want to acheive is not doing the same calculations on every controller action, instead if we can store somewhere globally the calculated value and use that for subsequent calls than it would increase the performance and save a lot of time OR any other solution to not make the same calculation again and again.

NOTE: Cannot use memory cache as new data is uploaded every night and it will affect the calculations.

Thank you all for your help!!


There are several ways to optimize the performance, though recommendations would be a bit limited given scarcity of specifics in your question:

  1. Data uploaded every night should not be a showstopper for using memory cache. Your requests are coming nearly simultaneously, so your cache could be short-lived (say a minute). When new data is uploaded, you're only behind by about a minute which in many cases could be acceptable. You can also mark your cache with version of data (i.e. which day) it was constructed with, and thus the moment new data is loaded, ignore and/or purge the cache. So memory caching is still an option.
  2. Precalculating this common interim result (as part of data load) and storing it in the DB, so that on every controller action you start from this common base and just do what is specific for that exact action.
  3. Have your client do a pre-request that would serve as basis for the other requests. On the pre-request, you calculate the common base, issue an id, then have other actions receive this id, load the calculated base off the id, and do their action-specific calculations for final result. The con here, obviously, is enforcing client into a particular usage of the API, but may be acceptable in your case?
  4. If usage pattern is fairly stable - meaning that the set of APIs are frequently called together, simply have a fat API entry point that calculates the base and all specifics of every action in the needed set, and then returns as one massive result, but thus does the common computation only once.

More specifics would help for a more practical answer.