问题
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.
回答1:
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)]
来源:https://stackoverflow.com/questions/8535160/outputcache-attribute-and-jquery-ajax-not-caching