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 your example, as Ivan pointed out, output caching would be slightly better performance wise. But only slightly. 

From my experience the majority of performance gains come from data caching. Page processing is a relatively inexpensive operation however, database calls are not. So I always gzip and dump my data into the cache, inflating when I get it out of the cache. Hanselman has a nice example of a zipped cache if you are interested: <http://www.hanselman.com/blog/CommentView.aspx?guid=aa479008-3d1e-45fc-b89f-e9c2ffc199c1> 

Also, output caching in mvc can be a little tricky. To cache a whole page is ok, just use the [OutputCache] attribute. However, if you only want to cache partials (which the majority of time you do), it gets a little more complex. 

Caching is hopefully something MS will improve upon in mvc 3.
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!