Attribute driven caching in ASP.NET (using Ninject)

So it turns out Ninject is AWESOME.

As such, I’ve implemented a completely attribute driven caching scheme for my data service classes. My previous post mentioned a way to condense the code needed to retrieve data from ASP.NET’s data cache. That was great and all, but I didn’t like having to add all that extra code for each method I wanted cached. It’d be much better if I could just “intercept” all method calls to a particular class and put the caching code in there, and control the timeout through attributes.

Enter Ninject.extensions.interception.

The basic overview is that you can use this Ninject extension to intercept calls to methods or entire classes in your code. It does this by dynamically creating a proxy class to your underlying class/methods and calls that instead of your class directly. (At least… this is the mental model that I’ve formed).. In any case, it works.

In the older version of Ninject there was an extension to do exactly what I’m doing here except that it used its own in memory cache. So to build my solution I just took the parts that were useful from this older solution and made it work with ASP.NET’s data cache instead.

First thing to do is download the caching library source.

UPDATE: The code has been moved to GitHub and some sample code has been added to show how to set it up in a WebForms application

Here’s how you set it up:

  1. Add “CacheAllVirtualMethods” attribute to the class
  2. Make all methods you want cached “virtual” methods
  3. Add “CacheTimeout” attribute to any methods whose timeout you want different than the default
  4. If you don’t want a method cached, don’t make it virtual… or add the “DoNotCache” attribute to it

[CacheAllVirtualMethods(DefaultTimeoutMinutes = 10)]
public class MyDataService : BaseService, IMyDataService{
 public virtual User GetUser(string userid)    {
 //.....
 }
 [CacheTimeout(TimeoutMinutes = 2)]
 public virtual IEnumerable<Account> GetccountBalances(User user)    {
 //.....
 }
 [CacheTimeout(TimeoutMinutes = 60)]
 public virtual IEnumerable<Announcement> GetAnnouncements()    {
 return GetAnnouncements(null);
 }
}

Advertisements

About Paul Martin

I enjoy rock climbing, playing guitar, writing code...
This entry was posted in .NET, cache, ninject. Bookmark the permalink.

8 Responses to Attribute driven caching in ASP.NET (using Ninject)

  1. Andrew says:

    hey Paul, I think that this is exactly what I’m looking for..awesome! However this post is a little old and I notice the code hasn’t changed since you uploaded it.. are you using this code or is it being maintained anywhere? I cant believe there arent millions of others who need it too. I see people talk about postsharp but that is a commercial product.

  2. Hi Andrew, this method based caching is the best invention since sliced bread.
    Where do you do the binding, and how does do you invoke the virtual method? Any chance of posting an updated version of the code?

    • Paul Martin says:

      @gerrieberrie72 – binding and invoking the virtual method is all taken care of by Ninject Interception. If you look at the “CacheAllVirtualMethodsAttribute” class, you see that it inherits from the Ninject library class “InterceptAttribute”. Ninject takes care of “noticing” that attribute on your method and does all the work of setting up a proxy class to that method. Then, when you eventually call that method from your code, it calls the Interceptor proxy class instead, CacheInterceptor. Specifically, the “Intercept” method. Inside that method, its the line “invocation.Proceed();” that actually calls the original code method. Hope that answers your question.

      I don’t plan on updating this code because I never used it in this form. It was part of a larger core library we use that has changed a lot since then.

  3. Thanks Paul, this should work now!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s