【问题标题】:ASP.NET Web API action cachingASP.NET Web API 动作缓存
【发布时间】:2013-12-02 16:47:01
【问题描述】:

我正在尝试在 ApiControler 中缓存操作的结果,但由于某种原因,我没有看到缓存的内容。我不完全了解 MVC 缓存的工作原理,但我假设如果缓存了动作响应,那么它不应该一直被调用,但它应该只输出缓存(如果可用)。

行动:

    [HttpGet]
    public HttpResponseMessage NetworkStatusSummary()
    {
        // getting networkStatuses
        HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, networkStatuses);

        response.Headers.CacheControl = new CacheControlHeaderValue {MaxAge = new TimeSpan(0, 10, 0), Public = true};

        return response;
    }

我在这里做错了什么?

【问题讨论】:

    标签: c# asp.net asp.net-mvc-4 caching asp.net-web-api


    【解决方案1】:

    好吧,似乎没有什么花哨的内置 Web API。于是想出了:

        private IList<NetworkStatus> NetworkStatuses
        {
            get
            {
                IList<NetworkStatus> networkStatuses;
                Cache cache = HttpContext.Current.Cache;
    
                if (cache[NETWORK_STATUSES_CACHE_KEY] == null)
                {
                    networkStatuses = m_networkOwnerService.GetNetworkStatuses();
                    cache.Add(NETWORK_STATUSES_CACHE_KEY, networkStatuses, null, Cache.NoAbsoluteExpiration, new TimeSpan(0, 5, 0), CacheItemPriority.Default, null);
                }
                else
                {
                    networkStatuses = (IList<NetworkStatus>) cache[NETWORK_STATUSES_CACHE_KEY];
                }
    
                return networkStatuses;
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-08
      • 1970-01-01
      • 2014-10-06
      • 2015-02-23
      • 1970-01-01
      相关资源
      最近更新 更多