【问题标题】:Why is the HttpContext.Cache count always zero?为什么 HttpContext.Cache 计数始终为零?
【发布时间】:2011-02-07 05:19:38
【问题描述】:

我使用 OutputCache 配置文件设置了几个页面,并确认它们正在通过使用多个浏览器进行缓存,并请求检索具有与所有请求匹配的时间戳的页面。当我尝试枚举 HttpContect.Cache 时,它​​始终为空。

有什么想法吗?或者我应该去哪里获取这些信息?

更新:

这不是客户端缓存,因为多个浏览器看到相同的响应。这里有一些代码来解释发生了什么。

Web.Config 缓存设置

<system.web>
    <caching>
        <outputCacheSettings>
            <outputCacheProfiles>
                <clear/>
                <add name="StaticContent" duration="1200" varyByParam="none"/>
                <add name="VaryByParam" duration="1200" varyByParam="*"/>
            </outputCacheProfiles>
        </outputCacheSettings>
    </caching>
     ...
</system.web>

**带有缓存的动作方法

[OutputCache(CacheProfile = "StaticContent")]
public ActionResult Index()
{
    return View(new CollaborateModel());
}

枚举缓存的代码,是的,这很粗糙,这是在控制器操作方法中定义的

    var sb = new StringBuilder();

    foreach (KeyValuePair<string, object> item in HttpContext.Cache)
    {
        sb.AppendFormat("{0} : {1}<br />", item.Key, item.Value.ToString());
    }

    ViewData.Add("CacheContents", sb.ToString());

HttpContext.Cache 是计数始终为空的地方,即使缓存似乎工作正常。

【问题讨论】:

  • 可能需要看一些代码。

标签: asp.net-mvc model-view-controller caching


【解决方案1】:

这可能是因为页面已经在客户端浏览器而不是服务器上缓存到下游。

【讨论】:

    【解决方案2】:

    我最终没有使用 HttpCache,而是滚动了自己的缓存模型,以便在我的数据访问层中保存数据集。如果我正在查找给定用户名的 AD 配置文件并将其转换为 DTO,那么我只需将该配置文件放入配置文件 DTO 的滚动集合中,我会在轮询 AD 以获取信息之前检查这些集合。

    【讨论】:

      猜你喜欢
      • 2014-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-02
      • 2012-01-16
      • 2020-01-07
      相关资源
      最近更新 更多