OutputCache not working
I want to cache the returned data from an activity. For this purpose I am using OutPutCacheAttribute
. Here is my client code:
$(document).ready(function() {
$.get('@Url.Action("GetMenu", "Home")', null,
function(data) {
parseMenu(data);
});
}
And here is my server code:
[HttpGet]
[OutputCache(Duration = 86400, Location = OutputCacheLocation.Server)]
public ContentResult GetMenu()
{
string jsonText = GetData(); //some code
return new ContentResult
{
Content = jsonText,
ContentType = "text/json"
};
}
As you can see I am using OutPutCacheAttribute
server caching to respond. But it doesn't work. Every time I load the page an action is called Home/GetMenu
. And it gets called even if I type in the browser address bar directly "localhost / Home / GetMenu". Where am I going wrong?
UPD I created a second activity to test this attribute without debugging. Here is its code:
[HttpGet]
[OutputCache(Duration = 86400, Location = OutputCacheLocation.ServerAndClient, VaryByParam = "none")]
public JsonResult GetJson()
{
return Json(new
{
random = new Random().Next(100)
},
JsonRequestBehavior.AllowGet);
}
I assumed that if the OutputCache attribute is working correctly (and I am using it correctly), then the action is called once and I get the same response each time. But if not, I get different answers every time, because every time a random number is generated. And when I called this action several times, I always got different responses like {"random":36}
, {"random":84}
etc.
source to share
In its default implementation, the output cache is associated with a process and stored in memory. As a result, if you do something like stop and start debugging, you've destroyed everything you previously cached. In fact, more precisely, you killed the process and started a new process, and since the cache is associated with the process, it went away with the old process.
source to share
Please, try
[OutputCache(Duration = 86400, Location = OutputCacheLocation.ServerAndClient,VaryByParam = "none")]
If it doesn't work Please try:
[HttpGet]
[OutputCache(Duration = 86400, Location = OutputCacheLocation.ServerAndClient, VaryByParam = "none")]
public JsonResult GetJson()
{
return Json(new{message = "Record created successfully!!!"},JsonRequestBehavior.AllowGet);
}
NB: more info on Outputcache
source to share
Have you noted web.config
that it is not disabled?
https://msdn.microsoft.com/en-us/library/ms228124(v=vs.100).aspx
<caching>
<outputCache enableOutputCache="false">
</outputCache>
</caching>
source to share