OutputCache Location=Client does not appear to work
Asked Answered
N

1

19

I am trying to use the OutputCache attribute in my MVC app and it doesn't appear to work when I use OutputCacheLocation.Client:

public class HomeController : Controller
{
    [OutputCache(Duration=15, Location=OutputCacheLocation.Client)]
    public ActionResult Client()
    {
        ViewBag.Message = "The current time is " + DateTime.Now.ToString("hh:mm:ss");

        return View();
    } 

    [OutputCache(Duration=15, Location=OutputCacheLocation.Any)]
    public ActionResult Any()
    {
        ViewBag.Message = "The current time is " + DateTime.Now.ToString("hh:mm:ss");

        return View();
    }        
}

The first one does not cache. I hit the page every second and it changes the time. The second one works. It only changes the time every 15 seconds. Is there something I am missing? I'm debugging this using IE8 and the built in development server in Visual Studio.

Nissie answered 6/3, 2012 at 21:34 Comment(4)
"Client" literally means the client, i.e. IE. So if you hit the page manually, you're not playing by the rules; client-side caching means you're not even supposed to hit the page.Aciniform
@Aciniform what do you mean? If I navigate to /home/client then to another page, and back to /home/client. Shouldn't I get served the cached page? (as long as I am within the time frame?)Vastha
@Aciniform wait, I see. hitting F5 invalidates the cache, navigating from/to serves the same page. I didn't know that. you should post that as answer. I have been searching all over the internet and couldn't find that tidbitVastha
@Joe, Darin writes all my answers for me. :)Aciniform
G
21

If you hit F5 you are evicting the client cache. The way client cache is supposed to work is that you have links on the site pointing to the Client action from some other views and when the user clicks on those links the cached version will get served (assuming of course he does that in the interval for which the page is cached).

Grill answered 7/3, 2012 at 7:11 Comment(3)
Makes sense. It also turns out that if you just type the address in again it will serve up the cached version, but you're right, the F5 just clears the cache.Nissie
@Dismissile, I think this behavior differs slightly by browser, but yeah, generally just typing in the address will respect the cache and F5 will either disregard the cache temporarily or evict the cached resource.Aciniform
@Darin, I have cached a page on the client, but when I hit F5 or I enter the URL manually in both content is not getting served from client cache. So when i.e. on which user operation content will get served from client? I mean that when it is useful to cache content on client. I read in MSDN that user specific data should be cached on the client, but if it's not getting served from client then what should be done? Could you please mention an example here.Antidisestablishmentarianism

© 2022 - 2024 — McMap. All rights reserved.