1

I'm looking to improve performance by using some king of cache. For example users login and access an appointment system, I would like to Cache this data, query this data from the cache and then update the cache should new appointments be added

For example, I was writing this but I think this is application caching and not session (dont want other users seeing other peoples data)

public IEnumerable<ScheduledItem> GetCache(bool reload, DateTime start, DateTime end, Account account)
        {
            if (System.Web.HttpContext.Current.Cache["ScheduleData"] == null)
            {
                var model = _eventService.FindAllEventsForAccountId(account.Id, start.Date, end.Date)
               .Where(IsAppointmentNotCancelled)
               .Select(a => new Models.ScheduledItem()
               {
                   id = a.Id,
                   start = a.StartTime.ToString("H:mm:ss").Length < 8 ? string.Format(a.StartTime.ToString("yyyy-MM-dd {0}H:mm:ss"), 0) : a.StartTime.ToString("yyyy-MM-dd H:mm:ss"),
                   end = a.EndTime.ToString("H:mm:ss").Length < 8 ? string.Format(a.EndTime.ToString("yyyy-MM-dd {0}H:mm:ss"), 0) : a.EndTime.ToString("yyyy-MM-dd H:mm:ss"),
                   backgroundColor = GetColour(a),
                   title = GetTitle(a, account),
                   textColor = "#fff",
                   AppointmentType = GetType(a),
                   resourceId = GetResource(a)

               }).ToList();


                return model;
            }

            return null;
        }

Any pointers, examples would be great

Andrei
  • 42,814
  • 35
  • 154
  • 218
D-W
  • 5,201
  • 14
  • 47
  • 74
  • Have you looked at http://stackoverflow.com/questions/343899/how-to-cache-data-in-a-mvc-application? Then there's also the OutputCacheAttribute which caches the output of an action with various settings. Imho you shouldn't reinvent things like this. – Peter Jan 15 '15 at 11:02
  • Yeah saw that, this seems like the newer way to do it? http://msdn.microsoft.com/en-us/library/vstudio/ff919782(v=vs.100).aspx – D-W Jan 15 '15 at 11:18
  • https://codethatway.wordpress.com/2014/11/18/caching-in-asp-net-mvc-using-outputcache-attribute-and-its-impact-on-performance/ DO you mind going through this, possibly might help you – Swaraj Jan 15 '15 at 11:37

1 Answers1

5

There are different caching strategies. You must decide, which one suits better for your application. The easiest way to enable cache is to set up page output caching (maybe varied by param/header/etc). This is done by special ASP.NET MVC OutputCache attribute.

If you need to do more specific caching, then you can implement it by yourself. There are different ways how to do it. First of all, you have to decide, where and how to store cached data. Would it be common for all users? You can put some cache data into a Session to cache it for a single user and for this particular Session. Please notice, if you have a lot of users, your app would not be able to handle this load. I actually would not recommend you to do it.

If you have a common data for all users, you could use standard HttpRuntime.Cache object to store data there. It is just a key-value storage as well. You could store user-specific data in it by composing special key like SomeData_{UserID}. Problem - it makes your application stateful, when normally your application should be stateless and all the state should be in storages like SQL or Cache Server or Session Server etc. If your app is stateless - you can easily scale it to many instances to accommodate high-load.

There are a lot of NoSQL key-value stores that could be used as an external cache. It could be shared across different applications and instances. I personally like Redis. This one is recommended by Microsoft for high-load projects.

Sometimes caching leads to data denormalization. You would have to always keep cache up-to-date. So before developing caching strategy I'd recommend you to think, what could be optimized in your application. Usually the bottle-neck is database interactions. Try to analyze database queries and see what could be improved there.

Andrei
  • 42,814
  • 35
  • 154
  • 218