在ASP.NET MVC中使用了HttpRuntime.Cache吗?

时间:2008-10-07 15:34:36

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

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

我将缓存包装在一个实现类似缓存的接口的代理类中(更简单),以便我可以在测试期间模拟它。但我想知道在新框架中是否已经为我做了这件事。然而,我找不到任何东西。

以下是我如何做到这一点的想法:

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

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

2 个答案:

答案 0 :(得分:1)

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

然而,

Don't combine this with the Authorize attribute

答案 1 :(得分:0)

不,但缓存在3.5中已更改。 3.5包含了一些包装类,它们可以对asp.net中使用的许多静态类进行存根/模拟。

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