I have an ecommerce working in ASP.Net MVC. I’m using Caching to improve performance in my pages and it’s working.
I’d link to know what offers better performance, for example, I can set OutputCache in my views and and use this cache for all page OR I could get my List of Products in controller, put it on cache (like the code below) and send it to View to render for the user?
private IEnumerable<Products> GetProductsCache(string key, ProductType type)
{
if (HttpContext.Cache[key] == null)
HttpContext.Cache.Insert(key, ProductRepository.GetProducts(type), null, DateTime.Now.AddMinutes(10), Cache.NoSlidingExpiration);
return (IEnumerable<Products>)HttpContext.Cache[key];
}
public ActionResult Index()
{
var home = new HomeViewModel()
{
Products = GetProductsCache("ProductHomeCache", ProductType.Product)
Services = GetProductsCache("ServiceHomeCache", ProductType.Service)
};
return View(home);
}
Both work, but I’d like to know what is the preferred method to improve performance, or are there other, better ways to do this?
In a real ASP.NET MVC site you will hardly have the chance to use OutPutCache.
The problem is that there is no save way to use donut caching. This means you cache most of the page but have some parts that are user specific that you dont cache. This works in old school ASP.NET.
Phil Haack wrote about donut caching but it turned out to be not feasible in MVC2
http://haacked.com/archive/2008/11/05/donut-caching-in-asp.net-mvc.aspx
Often I started using OutputCaching not thinking about this little user specific detail everybody forgott.
You might think: I can work around it by doing some Javascript stuff that loads dynamic data…bad idea for a public facing website. There are non javascript clients and there is the google bot.
First I was frightened that this might bring our high traffic site down: But it works very well by caching the results of calls into the datalayer.