DynaCache- just like page output caching, but for classes

Anyone who has done any serious work with any ASP.NET based framework will know that page output caching is a great feature. For those not familiar with it, the basic premise is that it makes sure that the generation of content is done only once for a set of parameters, and that all subsequent requests with the same parameters are served up from the cache for a specified period of time.

Wouldn't it be nice if you could do a similar thing for methods on classes, just by applying an attribute like this?

public virtual string GetData(int id)

Thanks to a little library called DynaCache, you can!

How does it work?

Say you have a class called TestClass:


The LoadData method is marked as virtual, and has an attribute called CacheableMethod applied to it, indicating the number of seconds the results should be cached for.

The CachableMethod attribute is the first interaction with the DynaCache framework - the second is with a class called Cacheable:


Calling Cacheable.CreateType<TestClass>() at runtime creates a new class called CacheableTestClass, deriving from TestClass and overriding any methods with the CachableMethod attribute, resulting in a class hierarchy like this:


This new class is created using Reflection.Emit and only exists in memory, so you can't use a reflector-like program to see what's generated, but if you could it would look something like this:

public class CacheableTestClass : TestClass
    private IDynaCacheService cacheService;
    public TestClass(IDynaCacheService cacheService)
        this.cacheService = cacheService;

    public override string LoadData(int id)
        string cacheKey = String.Format(CultureInfo.InvariantCulture, "TestClass_LoadData(Int32).{0}", id);
        object result;
        if (!this.cacheService.TryGetCachedObject(cacheKey, out result))
            result = base.LoadData(id);
            this.cacheService.SetCachedObject(cacheKey, result, 200);
        return (string)result;

Notice the IDynaCacheService parameter in the constructor? That's the third piece of the DynaCache framework - an instance of a class capable of interacting with whatever backing cache is being used. Out of the box DynaCache includes a concrete implementation of this called MemoryCacheService - it's just a wrapper around a .NET 4 MemoryCache instance. There's no reason why you shouldn't create your own though, e.g. for the ASP.NET or Windows Azure cache.

Making it simple with dependency injection

What this all means is that at runtime you need to be using CacheableTestClass rather than TestClass, otherwise all the generated caching code will never be used. Although it's possible to construct and use these types yourself, the simplest and best way to do that is to use a dependency injection framework, such as Ninject, StructureMap, etc.

For the sake of illustration, I'm going to use Ninject. The original configuration would have simply mapped ITestClass to TestClass, like this:


Using DynaCache is only marginally more complicated, you just configure your kernel like this:


The first line configures the cache service to pass to instances of CacheableTestClass, whilst the second binds ITestClass to the cacheable version of TestClass.

That's all there is to it! Now every time an instance of ITestClass is required, Ninject will construct and return an instance of CacheableTestClass - the rest of your code that consumes ITestClass will automatically make use of the dynamically constructed caching code.

Where to get DynaCache

You can get it from the CodePlex project site, or you can install it into your project using the Nuget command:

Install-Package DynaCache


Hopefully all the detail hasn't put you off - this whole article essentially boils down to just these three steps:

  1. Make the methods overridable and apply the CacheableMethod attribute to them.
  2. Configure your DI framework to return an instance of IDynaCacheService, either MemoryCacheService or one you have implemented yourself.
  3. Configure your DI framework to return the result of Cacheable.CreateType for your type.

I'd be really interested in feedback for DynaCache - let me know your thoughts in comments below, or on the project discussion board.


  • Sebas said

    Hello, I'm trying to implement DynaCache in Unity (DI). The problem I'm having is that I can't implement this method public static IUnityContainer RegisterType<TFrom, TTo>() where TTo : TFrom. The compiler is giving me a type mismatch error.
    Do you have any ideas on know how to resolve this?

    Thanks in advance.

  • Mike said

    Hi Sebas - I've put together an example on the dynacache documentation: https://dynacache.codeplex.com/wikipage?title=Example%20using%20Unity

    Hope that helps.

  • Igor said

    works great. how come that "protected virtual" methods caching doesn't work for me? I think this is valid scenario:
    public interface IC { T GetT(); }
    public class C : IC
    public T GetT()
    return GetTInternal();
    protected virtual T GetTInternal()
    //some time-expensive logic

  • Mike said

    Hi Igor,

    It's because currently only public methods are considered. I don't think that technically there's any reason why protected/internal virtual methods shouldn't be considered, there would just need to be a bit more care building up the proxying methods.

    I assume that in your scenario GetTInternal is used by other methods on the class? Otherwise there's nothing gained over putting the CacheableMethod attribute on the public method.


Comments have been disabled for this content.