c# - 如何在不违反 MVC 模式的情况下实现缓存模型?

标签 c# asp.net-mvc caching architecture asp.net-mvc-3

我有一个 ASP.NET MVC 3 (Razor) Web 应用程序,有一个高度数据库密集型的特定页面,用户体验是重中之重。

因此,我将在此特定页面上引入缓存。

我正在尝试找出一种方法来实现这种缓存模式,同时保持我的 Controller ,就像它目前没有缓存一样:

public PartialViewResult GetLocationStuff(SearchPreferences searchPreferences)
{
   var results = _locationService.FindStuffByCriteria(searchPreferences);
   return PartialView("SearchResults", results);
}

如您所见, Controller 非常纤薄,本应如此。它不关心如何/从何处获取信息 - 这是服务的工作。

关于控制流的一些注意事项:

  1. Controller 根据其区域获得特定服务的DI。在此示例中,此 Controller 获取一个 LocationService
  2. Services 调用 IQueryable<T> Repository 并将结果具体化到 TICollection<T> .

我想如何实现缓存:

  • 我不能使用输出缓存 - 有几个原因。首先,此操作方法是通过 [HttpPost] 从客户端 (jQuery/AJAX) 调用的,根据 HTTP 标准,不应将其缓存为请求。其次,我不想纯粹基于 HTTP 请求参数进行缓存 - 缓存逻辑比这复杂得多 - 实际上正在进行两级缓存。
  • 正如我上面所暗示的,我需要使用常规数据缓存,例如 Cache["somekey"] = someObj; .
  • 我不想实现一个通用的缓存机制,其中所有通过服务的调用首先通过缓存 - 我只想缓存这个特定的操作方法

首先想到的是创建另一个服务(继承LocationService),并在那里提供缓存工作流(首先检查缓存,如果没有则调用db,添加到缓存,返回结果)。

这有两个问题:

  1. 这些服务是基本的类库 - 没有引用任何额外的内容。我需要添加对 System.Web 的引用在这里。
  2. 我必须在 Web 应用程序之外访问 HTTP 上下文,这被认为是不好的做法,不仅是为了可测试性,而且一般来说 - 对吧?

我也考虑过使用 Models Web 应用程序中的文件夹(我目前仅将其用于 ViewModels),但在模型文件夹中使用缓存服务听起来不太对。

那么 - 有什么想法吗?我可以在这里使用特定于 MVC 的东西(例如 Action Filter's)吗?

一般建议/提示将不胜感激。

最佳答案

action 属性似乎是实现此目的的好方法。这是一个示例(免责声明:我是从头到尾写这篇文章的:写这篇文章时我喝了一定量的啤酒,所以请确保您对其进行了广泛的测试:-)):

public class CacheModelAttribute : ActionFilterAttribute
{
    private readonly string[] _paramNames;
    public CacheModelAttribute(params string[] paramNames)
    {
        // The request parameter names that will be used 
        // to constitute the cache key.
        _paramNames = paramNames;
    }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        base.OnActionExecuting(filterContext);
        var cache = filterContext.HttpContext.Cache;
        var model = cache[GetCacheKey(filterContext.HttpContext)];
        if (model != null)
        {
            // If the cache contains a model, fetch this model
            // from the cache and short-circuit the execution of the action
            // to avoid hitting the repository
            var result = new ViewResult
            {
                ViewData = new ViewDataDictionary(model)
            };
            filterContext.Result = result;
        }
    }

    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
        base.OnResultExecuted(filterContext);
        var result = filterContext.Result as ViewResultBase;
        var cacheKey = GetCacheKey(filterContext.HttpContext);
        var cache = filterContext.HttpContext.Cache;
        if (result != null && result.Model != null && cache[key] == null)
        {
            // If the action returned some model, 
            // store this model into the cache
            cache[key] = result.Model;
        }
    }

    private string GetCacheKey(HttpContextBase context)
    {
        // Use the request values of the parameter names passed
        // in the attribute to calculate the cache key.
        // This function could be adapted based on the requirements.
        return string.Join(
            "_", 
            (_paramNames ?? Enumerable.Empty<string>())
                .Select(pn => (context.Request[pn] ?? string.Empty).ToString())
                .ToArray()
        );
    }
}

然后您的 Controller 操作可能如下所示:

[CacheModel("id", "name")]
public PartialViewResult GetLocationStuff(SearchPreferences searchPreferences)
{
   var results = _locationService.FindStuffByCriteria(searchPreferences);
   return View(results);
}

就您在服务层中引用 System.Web 程序集的问题而言,这在 .NET 4.0 中不再是问题。有一个全新的程序集可提供可扩展的缓存功能:System.Runtime.Caching ,因此您可以使用它直接在服务层中实现缓存。

或者如果您在服务层使用 ORM 可能更好,这个 ORM 可能提供缓存功能?我希望如此。例如 NHibernate 提供了一个 second level cache .

关于c# - 如何在不违反 MVC 模式的情况下实现缓存模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4916701/

相关文章:

c# - 如何在自定义工作流程事件中获取构建详细信息?

java - Spring 和 JUnit |如何禁用特定测试类的 spring 测试上下文缓存?

html - 如何通过 createJS 和 Flash CC 在 Movieclip 上使用缓存

firebase - 如何在 Flutter 中缓存 Firebase 数据?

c# - 获取 .tt 文件在 T4 中的位置

c# - 在包含抽象方法的抽象类中重构具体方法

jquery - 将循环结构转换为 JSON .将 html 值发送到 Controller

asp.net-mvc - MVC 5,Identity 2.0 Android Rest/Json Api

css - 在 visual studio 2012 中捆绑

C# - 当应用程序使用另一种语言时获取英语异常消息?