OutputCache不起作用

时间:2017-04-13 12:45:08

标签: c# asp.net-mvc caching

我想缓存一个动作的返回数据。为此,我使用<link href="https://material.angularjs.org/HEAD/docs.css" rel="stylesheet"/> <link href="https://material.angularjs.org/HEAD/angular-material.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular-animate.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular-aria.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular-messages.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular-route.min.js"></script> <script src="https://material.angularjs.org/HEAD/angular-material.js"></script> <script src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/t-114/assets-cache.js"></script> <div ng-app="MyApp" ng-controller="AppCtrl"> <md-content> <md-datepicker ng-model="person.birthDate" md-placeholder="Enter date"></md-datepicker> </md-content> <big>ng-model value is: <strong>{{person.birthDate}}</strong></big> </div>。这是我的客户代码:

OutPutCacheAttribute

这是我的服务器代码:

$(document).ready(function() {
    $.get('@Url.Action("GetMenu", "Home")', null, 
        function(data) {
            parseMenu(data);                  
    });
}

如您所见,我使用[HttpGet] [OutputCache(Duration = 86400, Location = OutputCacheLocation.Server)] public ContentResult GetMenu() { string jsonText = GetData(); //some code return new ContentResult { Content = jsonText, ContentType = "text/json" }; } 来缓存服务器响应。但它不起作用。每次加载页面时,都会调用操作OutputCacheAttribute。即使我直接在浏览器的地址栏中键入&localhost / Home / GetMenu&#39;也会调用它。我哪里弄错了?

UPD 我创建了第二个操作来测试此属性而不进行调试。这是它的代码:

Home/GetMenu

我认为如果OutputCache属性正常工作(并且我正确使用它),那么就会调用一次动作,每次都得到相同的响应。但如果没有,那么我每次都会得到不同的响应,因为每次都会产生随机数。 当我多次调用此动作时,我总是收到不同的响应,例如[HttpGet] [OutputCache(Duration = 86400, Location = OutputCacheLocation.ServerAndClient, VaryByParam = "none")] public JsonResult GetJson() { return Json(new { random = new Random().Next(100) }, JsonRequestBehavior.AllowGet); } {"random":36}等等

3 个答案:

答案 0 :(得分:1)

在其默认实现中,输出缓存是进程绑定的并存储在内存中。因此,如果您执行诸如停止和开始调试之类的操作,则会破坏先前缓存的任何内容。实际上,更确切地说,你已经杀死了这个进程并启动了一个新进程,并且由于缓存是受进程限制的,所以它随旧流程而消失。

答案 1 :(得分:0)

请试试这个

  [OutputCache(Duration = 86400, Location = OutputCacheLocation.ServerAndClient,VaryByParam = "none")]

如果不能正常工作请尝试:

        [HttpGet]
    [OutputCache(Duration = 86400, Location = OutputCacheLocation.ServerAndClient, VaryByParam = "none")]
    public JsonResult GetJson()
    {
        return Json(new{message = "Record created successfully!!!"},JsonRequestBehavior.AllowGet);
    }

N.B。 :有关Outputcache

的更多信息

答案 2 :(得分:0)

Have you checked web.config that it isn't disabled?

https://msdn.microsoft.com/en-us/library/ms228124(v=vs.100).aspx

<caching>
    <outputCache enableOutputCache="false">
    </outputCache>
</caching>