OutputCache attribute and jQuery Ajax not caching
Asked Answered
D

1

5

I have a simple MVC3 Controller Action like this

[HttpGet]
[OutputCache(Duration = 1200,Location=System.Web.UI.OutputCacheLocation.Server)]
public string GetTheDate()
{
    return DateTime.Now.ToString();
}

And I call it from jQuery Ajax like this

jQuery.ajax({
            type: "GET",
            url: "http://localhost:60690/Public/GetTheDate",
            cache: false,
            success: function (data) {
                //alert("success");
                jQuery("#stats").append("<b>" + data + "</b>");
            },
            error: function (req, status, error) { alert("failure"); alert(error + " " + status + " " + req); }
        });

The problem is that the date is always the current date, not the cached response. My understanding was that [OutputCache( Location=Server)] means that the server (the MVC app) caches the response and when the client requests the data, the action is intercepted so as not to bother with DateTime.Now but returns the cached response.

Am I understanding it wrongly or simply doing something wrong?

Update :

3nigma's answer is right. VaryByParams="none" does the trick but..... It's obvious from my method that I don't have any parameters so why should I need to say "none". Turns out that the 'Params' I was thinking the documentation referred to were params in my method are actually not the params in my method, they are anything that the request handler could construe as params.

The MS documentation says

When this property is set to multiple parameters, the output cache contains a different version of the requested document for each specified parameter. Possible values include "none", "*", and any valid query string or POST parameter name.

See the bit in bold (my emphasis) that means that although I'm not expecting any querystring parameters, if any get sent in (like the way jQuery.ajax does when cache:false by appending to the request GET /Public/GetTheDate?_=1324047171837 ) then there is a parameter, whether I expected it or not.

Durance answered 16/12, 2011 at 13:52 Comment(0)
G
7

with cache: false, you are explicitly telling jquery not to cache set cache: true,

Edit

set the VaryByParam="none" like

[OutputCache(Duration=1200, VaryByParam="none",Location=System.Web.UI.OutputCacheLocation.Server)]
Grimsley answered 16/12, 2011 at 14:13 Comment(5)
But doesn't that only say 'don't cache on the client-side'. I mean the client always sends the request but OutputCache(location=server) says something along the lines of 'service the request as normal (from the outside) but instead of doing what's in the action just return the cached result'. No?Durance
yes caching on the server means if the same request comes from someother user instead of going through the action it will be served the cached resultGrimsley
try [OutputCache(Duration=1200, VaryByParam="none",Location=System.Web.UI.OutputCacheLocation.Server)] here is a useful link asp.net/mvc/tutorials/older-versions/controllers-and-routing/…Grimsley
I had read that page and although they use VaryByParam="none" it never dawned on me that it's a requirement to have it defined even when you have no parameters - certainly isn't mentioned in the documentation. That's done the trick, still with client side cache : false. It's nice to know I was right, while still being wrong !Durance
can you update your 'proper' answer to reflect the real fix and I'll green-up your tick. Thanks.Durance

© 2022 - 2024 — McMap. All rights reserved.