Question

When attempting to set a different OutputCache property on a partial view I find that the PartialView cache is using the parents output cache duration. With the following code I would hope that the RenderPartial would result in a shorter OutputCache duration but I find that it is the same as the parent view (10 seconds)

public class HomeController : Controller
{
    [OutputCache(Duration=10, VaryByParam="none")]
    public ActionResult Index()
    {
        ViewBag.Message = "Time now: "+ DateTime.Now.ToString();

        return View();
    }

    [ChildActionOnly]
    [OutputCache(Duration=5, VaryByParam="none")]
    public PartialViewResult LogonPartial()
    {
        return PartialView("_LogOnPartial");
    }
}

With this simple example showing the DateTime.Now in the partial view I find that the PartialView does not clear it's cache until the parent view flushes his where I would hope that the Partial view clear's cache every 5 seconds (not every 10 as the parent view does). With the examples that I have seen using OutputCache on a PartialView the cache is implemented on the PartialView not the containing view. Does anyone know if this is a limitation of caching in MVC3 or if there is another way to handle different caching mechanisms on the same page? Thanks in advance!

Was it helpful?

Solution

You have cached the entire parent view for 10 seconds. This means that during those 10 seconds the child action wouldn't ever be hit and the entire view will be served from cache. Even if the cache of the child action expires after 5 seconds it still won't be hit.

In ASP.NET MVC 3 only donut hole caching is supported (cache a portion of the page by using the OutputCache attribute on a child action). Donut caching is not supported (exclude portions of a cached page from this cache).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top