如何实现与ASP.NET MVC自定义缓存提供商 [英] How to implement a custom cache provider with ASP.NET MVC

查看:243
本文介绍了如何实现与ASP.NET MVC自定义缓存提供商的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我迁移单轨应用程序的ASP.NET MVC 1.0 。我在原来的应用程序,我写了一个自定义缓存提供者(使用 memcached的)。在单轨这个任务很简单,因为该框架使用的接口,并有<一个href=\"http://api.castleproject.org/html/T%5FCastle%5FMonoRail%5FFramework%5FICacheProvider.htm\">ICacheProvider看起来是这样的:

I am migrating a MonoRail application to ASP.NET MVC 1.0. In my original application I wrote a custom cache provider (a distributed cache provider using memcached). In MonoRail this task was very easy because the framework used interfaces and there is ICacheProvider that looks like this:

public interface ICacheProvider : IProvider, IMRServiceEnabled
{
    void Delete(string key);
    object Get(string key);
    bool HasKey(string key);
    void Store(string key, object data);
}

此接口的实例是在每个控制器动作用。因此,所有我所要做的就是实现使用memcached的自定义缓存提供,并告诉
单轨用我的缓存提供者而不是默认的。这也是很容易的嘲笑和单元测试我的控制器。

An instance of this interface is available in every controller action. So, all I had to do was to implement a custom cache provider that uses memcached and tell MonoRail to use my cache provider instead of the default one. It was also very easy to mock and unit test my controller.

在ASP.NET MVC 1.0还有的System.Web.Abstractions组件(名称看起来很有希望)定义的 HttpContextBase 是这样的:

In ASP.NET MVC 1.0 there's the System.Web.Abstractions assembly (name looks promising) that defines the HttpContextBase like this:

public abstract class HttpContextBase : IServiceProvider
{
    ... 
    public virtual System.Web.Caching.Cache Cache { get; }
    ...
}

我不明白,这里使用的缓存性能如何是一个缓存提供者的抽象。它是密封缓存类的遗产。看来,我不是唯一的一个<一href=\"http://www.lostechies.com/blogs/jimmy%5Fbogard/archive/2008/10/29/testing-fun-with-asp-net-mvc.aspx\">struggling嘲笑出类的框架。

I don't understand how the Cache property used here is an abstraction of a cache provider. It is the legacy sealed Cache class. It seems that I am not the only one struggling to mock out the classes in the framework.

我很新的ASP.NET MVC框架,我必须在这里失去了一些东西。我可以写一个使用ICacheProvider接口定义了一个CustomBaseController和
我的所有控制器从这个基类派生的,但如果有一个更优雅(ASP.NET MVCish)解决方案,我会很高兴来实现它。我注意到,HttpContextBase实现<一个href=\"http://msdn.microsoft.com/en-us/library/system.iserviceprovider%28VS.71%29.aspx\">IServiceProvider.哪来的<一个href=\"http://msdn.microsoft.com/en-us/library/system.iserviceprovider%28VS.71%29.aspx\">GetService方法要去寻找服务?这能很容易地嘲笑?

I am very new to the ASP.NET MVC framework and I must be missing something here. I could write a CustomBaseController that uses an ICacheProvider interface that I define and all my controllers derive from this base class, but if there's a more elegant (ASP.NET MVCish) solution I would be glad to implement it. I've noticed that HttpContextBase implements IServiceProvider. Where's the GetService method going to look for services? Can this be easily mocked?

推荐答案

缓存没有正式的抽象或供应商,但你可以轻松地构建之一:

Cache doesn't have an official abstraction or provider, but you can easily build one:

ASP.NET 4.0中包括<一个href=\"http://weblogs.asp.net/scottgu/archive/2010/01/27/extensible-output-caching-with-asp-net-4-vs-2010-and-net-4-0-series.aspx\">output缓存提供抽象(据我所知的的一般抽象缓存但只为的输出的高速缓存)

ASP.NET 4.0 includes an output cache provider abstraction (AFAIK not a general cache abstraction but only for output caching)

这篇关于如何实现与ASP.NET MVC自定义缓存提供商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆