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.
It allows varying the cached output by GET query string or form POST parameters. For instance, you can vary the user-control output to the cache by specifying the user-control name along with either a query string or a form POST parameter. For more information, see Caching Multiple Versions of User Control Output.
The output cache enables you to cache the content returned by a controller action. That way, the same content does not need to be generated each and every time the same controller action is invoked. Imagine, for example, that your ASP.NET MVC application displays a list of database records in a view named Index.
The output cache is located on the Web server where the request was processed. This value corresponds to the Server enumeration value. The output cache can be stored only at the origin server or at the requesting client. Proxy servers are not allowed to cache the response.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With