Using EasyCaching In ASP.NET Core - Caching Intercept


In this article, you will learn how to use EasyCaching to simplify caching operations and separate business and caching operations in ASP.NET Core .

You can follow my last article Using EasyCaching In ASP.NET Core - Part One to find out  basic information about EasyCaching.

Or you can visit EasyCaching's Github page to learn more.


Let's take a look at the following code first.
  1. public Product GetProduct(int id)  
  2. {  
  3.     string cacheKey = $"product:{id}";  
  5.     var val = _cache.Get<Product>(cacheKey);  
  7.     if(val != null)  
  8.         return val;  
  10.     val = _db.GetProduct(id);  
  12.     if(val != null)  
  13.         _cache.Set<Product>(cacheKey, val, expiration);  
  15.     return val;  
  16. }  
This is a traditional way to handle caching in our code.
This is not a good way to handle caching!
Also, if we want to update or remove cached items, we need to repeat this action. It's obvious that the above code mixes business and caching code together, and this will make it hard to maintain.

In the next section, I will show you how to use EasyCaching to simplify the caching operation. This feature is called Caching Intercept.


Let's create something without caching as usual.

Create our services first.
  1. public interface ISomeServices  
  2. {  
  3.     string GetCurrentUtcTime();  
  4. }  
  6. public class SomeServices : ISomeServices  
  7. {  
  8.     public string GetCurrentUtcTime()  
  9.     {  
  10.         return DateTime.UtcNow.ToString();  
  11.     }  
  12. }  
It's a very easy service that returns the UTC time.
Use ISomeServices in controller:
  1. [Route("api/[controller]")]  
  2. public class ValuesController : Controller  
  3. {  
  4.     private readonly ISomeServices _services;  
  6.     public ValuesController(ISomeServices services)  
  7.     {  
  8.         this._services = services;  
  9.     }  
  11.     [HttpGet]  
  12.     public string Get()  
  13.     {  
  14.         return _services.GetCurrentUtcTime();  
  15.     }  
  16. }  
Don't forget to register ISomeServices in Startup class!
Enable caching
There are two types of Caching Intercept implementation in EasyCaching, one is based on AspectCore, the other one is based on Castle.
In this article, I will use the intercept that is based on AspectCore to show you how EasyCaching helps us to handle this.
Let’s enable caching on SomeServices so that the time will be cached.
First of all, we need to install the following packages via nuget.
  1. Install-Package EasyCaching.Interceptor.AspectCore -Version 0.1.2   
  2. Install-Package EasyCaching.InMemory -Version 0.1.3  
Here I used InMemory as the caching provider, you can choose this based on your need.
Edit the Startup class
  1. public class Startup  
  2. {  
  3.     //others....  
  5.     public IServiceProvider ConfigureServices(IServiceCollection services)  
  6.     {  
  7.         //Intercept In-Memory Caching  
  8.         services.AddDefaultInMemoryCache();  
  10.         services.AddMvc();  
  12.         return services.ConfigureAspectCoreInterceptor();  
  13.     }  
  14. }  
Make some changes to the interface ISomeServices.
  1. public interface ISomeServices : IEasyCaching  
  2. {  
  3.     [EasyCachingAble(Expiration = 5)]  
  4.     string GetCurrentUtcTime();  
  5. }  
We add an attribute named EasyCachingAble to the method.
There are two useful parameters you can use in EasyCachingAble.
ExpirationSpecify after some seconds, the cached item will expire.
CacheKeyPrefixSpecify the prefix of the cache key, if not use this value, it will generate the cache key based on the name of method and paramters' value of method.

Now, let's test the application.

As you can see, when I open the API for the first time, it returns 2/27/2018 2:19:07 PM, and I refreshed this page many times, and after 5 seconds, the API returns 2/27/2018 2:19:12 PM.

Apart from EasyCachingAble, EasyCaching has another two attributes: EasyCachingPut and EasyCachingEvict.

EasyCachingPut is used to update the cached item.

EasyCachingEvict is used to remove the cached item or cached items.


In this article, I showed you how to enable caching in your ASP.NET Core project with a simple way!

If you want to learn more about caching interceptor, you need some knowledge about AOP (Aspect-Oriented Programming).
I hope this article can help you!