Question: 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?

Answer: 
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.
OutputCache will give better results. But it is not always possible to be used - for example if you have many dynamic ( user specific ) controls. 

Caching the result is also a choice when the result set is used in **many** views. Your current implementation is **not** cache safe - i.e. if two or more threads get simultaneously in the if, which one will win when saving the result? Protect your cache with **[ReaderWriterLockSlim](http://msdn.microsoft.com/en-us/library/system.threading.readerwriterlockslim.aspx)** if you chose this way. 

Also be careful with choosing cache time! I would suggest you build your application with **sense of ability to cache**, but do not cache unless totally sure something will take lots of time to execute ( may be your method needs improving? ). After your project is ready, profile it, and put cache where only needed!

But I suggest you investigate first - i.e. which one works better for you in given situation. Don't **overuse** one over the other, and don't overuse both!