Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a custom cache provider with ASP.NET MVC

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?

like image 216
Darin Dimitrov Avatar asked May 16 '09 21:05

Darin Dimitrov


People also ask

How cache is implemented in ASP.NET MVC?

In this, you learn how to cache the output returned from a controller action so that a new user doesn't need to create the same content every time the action is called. Using the OutputCache attribute, you can enable output caching functionality by applying an individual controller action or an entire controller class.

What are the different caching techniques available in .NET MVC?

Any (Default)- Content is cached in three locations- the Web Server, any proxy Servers and the Web Browser. Client- Content is cached on the Web Browser. Server- Content is cached on the Web Server. ServerAndClient- Content is cached on the Web Server and the Web Browser.

What is OutputCache MVC?

The output cache enables you to cache the content returned by a controller action. That way, the same content does not need to be generated each and every time the same controller action is invoked. Imagine, for example, that your ASP.NET MVC application displays a list of database records in a view named Index.


2 Answers

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

  • http://weblogs.asp.net/zowens/archive/2008/08/04/cache-abstraction.aspx
  • http://memcachedproviders.codeplex.com/SourceControl/changeset/view/15983#58762

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

like image 61
Mauricio Scheffer Avatar answered Sep 30 '22 23:09

Mauricio Scheffer


This answer is outdated. See ASP.NET 4.0: Writing custom output cache providers.

like image 33
M. Mennan Kara Avatar answered Sep 30 '22 22:09

M. Mennan Kara