At this point, we have all the dlls we need to start writing some code.

For testing purposes, I am going to rename one of the Controller classes that are added by default (ValueController.cs) to be UserController and add a couple of classes to test the actual caching code (UsersRepository and IUserRepository)

You can find the code for all these classes, in the github repository mentioned at the end of this post.

Unity Configuration to start method interception

The first step is to configure our unity container to specify which “types” need to be intercepted for our caching purposes.

Under the App_Start folder in your solution, if you open your UnityConfig.cs file, it should look like the following.

As you can see in the above code, I have added a DefaultInterceptor for the IUserRepository interface. This lets the Unity Interceptor know that we want to intercept methods defined in this interface.

Introducing Cache Attribute and Cache Attribute Handler

Now, we don’t want to intercept or cache all the methods that exist in that interface that we configured in the Unity Container, so we define some Attribute that we will put on top of some of the methods, and that will tell the interceptor which one of the methods to intercept.

And then we will add an Attribute handler for the attribute to trigger when that method is called, and that is where we actually do the Caching operation.

For that I added a class for CacheAttribute.cs that looks like the following:

And now we need to implement the class that will do the Cache Handling used in the above Cache Attribute class.

For that I add a new class called CacheAttributeHandler.cs and it will look like the following:

Cache Manager configuration

As you can see in the above snippet, the Cache Manager is configured and instantiated! Yes, it is very simple to set it up and you can start using it with the few lines of code:

var config = new ConfigurationBuilder() .WithSystemRuntimeCacheHandle() .Build();

_cache = new BaseCacheManager<object>(config); var result = _cache.Get(KeyPrefix);

Using the Cache Attribute

Now what is left is to decorate the methods that we are interested in, with the Cache attribute and we are good to go.

For instance, our UserRepository.cs class with the Attributes will look like the following:

Note: You can put the Attributes on top of the methods in the class implementation or in the interface.

As a good general practice it is better to put them on the interface methods. Because if you change the actual implementation at any point, or have a different implementation of the same interface, you dont need to worry about the attributes, because they will apply to the new implementation automatically.

This might happen specially when using Repository pattern and for example you want to switch from Entity Framework to Dapper or something.

If you decide to put your Attributes on the Interface methods, it will look like the followig:

Adding Invalidate Cache Attribute

Now, we keep coding and basically copy and paste CacheAttribute.cs and CacheAttributeHandler.cs and rename them to be InvalidateCacheAttribute.cs and InvalidateCacheAttributeHandler.cs

This is how my InvalidateCacheAttribue.cs looks like:

And then similar changes to the InvalidateCacheAttributeHandler and this is how it looks like:

That’s it, you got a Caching layer implemented that intercepts any method that is decorated with your attribute, to Cache or Invalidate cache.

Now if you want to continute to extend this code, you probably need to consider the input arguments for each method to be as part of your Cache Key (In addition to the CachePrefix that is being passed in to the attribute).

Complete Solution

As always, if you didn’t follow the steps and just want the final project, you can find it on github:

Cheers!