0

I have an ASP.Net MVC 3 web application, and within one particular page there is quite a bit of data processing happening when the page loads. Please see my sample code below.

Basically the code is querying my database and pulling back data into the variable shiftDates, this could be anything up to a thousand records. Next I perform a group by and distinct on that data and place it into a variable called groupOrgs . Then I iterate through that data, and place each record and its related data into a ViewModel which my Razor View requires.

You may read this and think that this code shouldn't take too long to load, query and display to the user, however, there are two other pieces of code, very similar to that below, also within my Controller method which also run when the user loads the page. Therefore, picture the code below being asked to run three times within one page load. This then causes a noticeable delay on the page load time.

I guess I would like any help or advice on how to restructure my code below or that when the page loads, initially it pulls it from the database and does all the querying etc, but if the page is reloaded, then the data is pulled from a cached source.

Really, any help with this is greatly appreciated.

Thanks.

Controller

public ActionResult Index()
{
            ViewModelHomepage model = new ViewModelHomepage();
            IList<ViewModelHomepageCounters> orgShiftDates = new List<ViewModelHomepageCounters>();

            //Get all available dates
            IList<ShiftDate> shiftDates = _shiftDateService.GetAllShiftDatesToBeFilled();

            //Get total dates per Organisation
            var groupOrgs = shiftDates
                .GroupBy(x => x.Shift.Organisation.description)
                .Select(x => new
                {
                    _id = x.FirstOrDefault().Shift.organisationID,
                    _orgName = x.Key,
                    _shiftDateCount = x.Count()
                }
                ).OrderBy(x => x._orgName);

            foreach (var item in groupOrgs )
            {
                ViewModelHomepageCounters t = new ViewModelHomepageCounters();
                t.id = item._id;
                t.OrgName = item._orgName.ToString();
                t.totalShiftDates = Convert.ToInt32(item._shiftDateCount);
                orgShiftDates.Add(t);
            }
            model.OrgShiftDates = orgShiftDates;
}
tcode
  • 5,055
  • 19
  • 65
  • 124
  • `OutputCacheAttribute`? Though you may want to consider caching it at the service layer (maybe both). – Brad Christie Mar 26 '15 at 14:51
  • @BradChristie Thanks for your help. I'll do a bit of research here, although, do you know of any examples or code that could help? Thanks again. – tcode Mar 26 '15 at 14:55
  • 1
    As far...caching output? `[OutputCache]` is pretty easy, and there's a lot of write-up here on SO. As far as service layer, [this question may help](http://stackoverflow.com/questions/873355/how-to-implement-a-custom-cache-provider-with-asp-net-mvc). – Brad Christie Mar 26 '15 at 15:03

0 Answers0