如何使用 ASP.NET MVC 实现自定义缓存提供程序

发布于 2024-07-20 18:05:04 字数 1950 浏览 5 评论 0 原文

我正在将 MonoRail 应用程序迁移到 ASP.NET MVC 1.0。 在我原来的应用程序中,我编写了一个自定义缓存提供程序(使用 memcached 的分布式缓存提供程序)。 在 MonoRail 中,这个任务非常简单,因为框架使用了接口,并且有 ICacheProvider ,看起来像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 的自定义缓存提供程序并告诉 MonoRail 使用我的缓存提供程序而不是默认的。 模拟和单元测试我的控制器也非常容易。

在 ASP.NET MVC 1.0 中,有一个 System.Web.Abstractions 程序集(名称看起来很有前途),它定义了 HttpContextBase 像这样:

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

我不明白这里使用的 Cache 属性如何是缓存提供程序的抽象。 它是遗留的密封 Cache 类。 看来我不是唯一一个 努力模拟框架中的类。

我对 ASP.NET MVC 框架非常陌生,我一定错过了一些东西。 我可以编写一个使用我定义的 ICacheProvider 接口的 CustomBaseController 我的所有控制器都派生自这个基类,但如果有更优雅的(ASP.NET MVCish)解决方案,我将很高兴实现它。 我注意到 HttpContextBase 实现了 IServiceProviderGetService 方法将在哪里查找服务? 这能轻易被嘲笑吗?

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);
}

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.

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; }
    ...
}

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

做个ˇ局外人 2024-07-27 18:05:04

缓存没有官方的抽象或提供程序,但您可以轻松构建一个:

ASP.NET 4.0 包含一个 输出缓存提供程序抽象(AFAIK不是通用缓存抽象,但仅用于输出缓存)

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文