In this tutorial, you will learn several strategies to implement a global caching solution within an ASP.NET project. The cache provider we will be creating will allow you to switch the cache between string memory, session, or, simply turned off via a config value. Implementing a good caching strategy on your project is key to good performance. It goes without saying, website users will go elsewhere if a page takes too long to load. Caching objects and API calls that are expensive to generate is one trick to help you speed things up πŸ’¨πŸ’¨πŸ’¨

The Caching Code

We want to create a cache that allows for value types and reference types to be stored within the cache. The code to enable this in ASP.NET is pretty straightforward. To create a cache provider we will need an interface and some classes that implement the different caching strategies. To do this, I will use an interface called ICacher to allow for the location of the cache to be swapped at run-time, or compile-time. We will create a class named MemoryCache to store things in memory:

ICacher: To create a swappable cache, the caching provider will need to be written against an interface. This interface can look like this:

Memory Caching: This class will store data into memory:

Session Caching: This class will store data into session:

Now we have the code to handle memory and session caching, the next step is to create a factory to allow us to switch between the different cache providers:

Cache Factory

Following good design patterns, we can do this by creating a factory class. I've used an enum as the switch that flips between the two providers. I think this makes for more robust unit tests, however, you could use a string if you wanted to:

To follow good practice, you should write the factory against an interface as well. The interface in this example looks likes this:

For completeness, the code for the enum is shown below:

Using The Cache Provider

In this section, I will show you how I used the cache provider in my project. In my situation, I needed to cache user data. When storing user data I could not guarantee that all keys would be unique. I created an abstract caching helper that generated a unique ID for me. When storing global data you need the key to be unique, otherwise, the data could accidentally be overridden. One simple way to ensure unique keys is to prefix the key with a unique identifier. I created an abstract caching helper to do this. The sole point of this class is to generate a unique cache key:

The cache factory is being passed into this class within the constructor. This means we can now swap between the different types of cache providers within this helper without having to modify the UsernameCachingHelper class. Open/Closed principle in action πŸ”₯πŸ”₯πŸ”₯This was very useful to help to debug on different environments, e.g. development, staging and production! This class implements fromBaseCachingHelper. The code for this base class looks like this:

I used a value within app settings to set the type of cache provider to use. This allowed me to switch between the cache without having to update any of your code πŸ’₯


If you want to create a cache provider in Microsoft .NET that allows data to be globally cached or sessions cache, I hope this helped you out. Happy Coding 🀘