【问题标题】:Has using the HttpRuntime.Cache changed in ASP.NET MVC?在 ASP.NET MVC 中使用 HttpRuntime.Cache 是否发生了变化?
【发布时间】:2010-09-15 19:07:55
【问题描述】:

所以,当我准备对控制器进行单元测试时,我正在查看我的标准缓存实用程序,并想,嘿,访问 HttpRuntime.Cache 在 MVC 中直接被认为是有害的吗?

我将缓存包装在一个代理类中,该代理类实现了一个类似缓存的接口(虽然简单得多),这样我就可以在测试期间模拟它。但我想知道这是否已经在新框架中为我完成了。但是,我什么也找不到。

下面是我如何做的一个想法:

public ActionResult DoStuffLol(guid id)
{
  var model = CacheUtil.GetOrCreateAndStore(
                  "DoStuffLolModel",
                  () =>
                  {
                    /* construct model here; time consuming stuff */
                    return model;
                  });
  return View("DoStuffLol", model);
}

那么,访问缓存的旧模式是否改变了?在 MVC 中缓存操作结果是否有更好的模式?

【问题讨论】:

    标签: asp.net-mvc caching httpruntime.cache


    【解决方案1】:

    将 OutputCache 属性添加到您的控制器操作中,以便告诉框架为您缓存输出。您可以在ScottGu's blog post on ASP.NET Preview 4 中阅读有关此属性的更多信息。

    Don't combine this with the Authorize attribute,然而。

    【讨论】:

    • 这仅用于缓存整个控制器动作。为了只缓存数据片段,ASP.Net MVC 中的机制没有改变。
    【解决方案2】:

    没有,但是缓存在 3.5 中发生了变化。 3.5 包含包装类,可以轻松地对 asp.net 中使用的许多静态类进行存根/模拟。

    http://www.codethinked.com/post/2008/12/04/Using-SystemWebAbstractions-in-Your-WebForms-Apps.aspx

    【讨论】:

      猜你喜欢
      • 2011-11-20
      • 1970-01-01
      • 2020-12-03
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-12
      • 2013-06-25
      • 2012-12-18
      相关资源
      最近更新 更多