Using EasyCaching In ASP.NET Core - Part One

Introduction

In this article, you will learn how to use an open source library named EasyCaching to handle caching in ASP.NET Core.

What is EasyCaching?
EasyCaching
EasyCaching is an open source caching library that contains basic usages and some advanced usages of caching which can help us to handle caching more easily! 

EasyCaching's Github Page

https://github.com/catcherwong/EasyCaching

EasyCaching is mainly built for .NET Core projects. It contains four basic caching providers: In-Memory, Redis, Memcached, and SQLite.

Let's take a look at the basic usages of these four caching providers.

In-Memory Caching Provider

In-Memory caching provider is based on Microsoft.Extensions.Caching.Memory.

How to use it? 

First of all, we need to create an ASP.NET Core Web API project (MVC and Razor Pages are OK as well). 

Install EasyCaching.InMemory via NuGet using the following command.

Install-Package EasyCaching.InMemory

Add configuration to Startup class

  1. public class Startup  
  2. {  
  3.     //...  
  4.   
  5.     public void ConfigureServices(IServiceCollection services)  
  6.     {  
  7.         //other services.  
  8.   
  9.         //Important step for In-Memory caching provider  
  10.         services.AddDefaultInMemoryCache();   
  11.     }  
  12. }  

Then, call the provider to handle caching.

  1. [Route("api/[controller]")]  
  2. public class ValuesController : Controller  
  3. {  
  4.     private readonly IEasyCachingProvider _provider;  
  5.   
  6.     public ValuesController(IEasyCachingProvider provider)  
  7.     {  
  8.         this._provider = provider;  
  9.     }  
  10.   
  11.     [HttpGet]  
  12.     public async Task<string> Get()  
  13.     {  
  14.         //Set  
  15.         this._provider.Set("demo""123", TimeSpan.FromMinutes(1));  
  16.   
  17.         //Set Async  
  18.         await this._provider.SetAsync("demo""123", TimeSpan.FromMinutes(1));     
  19.           
  20.         //Get  
  21.         var res = this._provider.Get("demo", () => "456", TimeSpan.FromMinutes(1));  
  22.       
  23.         //Get Async      
  24.         var res = await this._provider.GetAsync("demo",async () => await Task.FromResult("456"), TimeSpan.FromMinutes(1));     
  25.           
  26.         //Get without data retriever  
  27.         var res = this._provider.Get<string>("demo");  
  28.           
  29.         //Get without data retriever Async  
  30.         var res = await this._provider.GetAsync<string>("demo");  
  31.          
  32.         //Refresh  
  33.         this._provider.Refresh("key""123", TimeSpan.FromMinutes(1));  
  34.   
  35.         //Refresh Async  
  36.         await this._provider.RefreshAsync("key""123", TimeSpan.FromMinutes(1));  
  37.           
  38.         //Remove  
  39.         this._provider.Remove("demo");  
  40.           
  41.         //Remove Async  
  42.         await this._provider.RemoveAsync("demo");  
  43.           
  44.         return "OK";  
  45.     }  
  46. }  

All of the caching providers implement an interface named IEasyCachingProvider that contains the basic caching APIs, such as Set, Get, Refresh, and Remove.

Other types of caching provider's usages are the same as In-Memory caching provider but configuration.

Redis Caching Provider 

Redis caching provider is based on StackExchange.Redis

How to configure it?

Install EasyCaching.Redis via NuGet. 
Install-Package EasyCaching.Redis

Add Configuration to Startup class

  1. public class Startup  
  2. {  
  3.     //...  
  4.   
  5.     public void ConfigureServices(IServiceCollection services)  
  6.     {  
  7.         //other services.  
  8.   
  9.         //Important step for Redis caching provider        
  10.         services.AddDefaultRedisCache(option=>  
  11.         {                  
  12.             option.Endpoints.Add(new ServerEndPoint("127.0.0.1", 6379));  
  13.             option.Password = "";  
  14.             //other settings of redis caching provider  
  15.         });  
  16.     }  
  17. }  

The most important options of redis are endpoints.

The uses of this provider are same as that of the In-Memory caching provider.

Memcached Caching Provider 

Memcached caching provider is based on EnyimMemcachedCore

How to configure it? 

Install EasyCaching.Memcached via NuGet using the following command -

Install-Package EasyCaching.Memcached 

Add Configuration In Startup Class

  1. public class Startup  
  2. {  
  3.     //...  
  4.       
  5.     public void ConfigureServices(IServiceCollection services)  
  6.     {  
  7.         //other services.  
  8.   
  9.         //Important step for Memcached caching provider        
  10.         services.AddDefaultMemcached(option=>  
  11.         {                  
  12.             option.AddServer("127.0.0.1",11211);  
  13.         });  
  14.     }  
  15.       
  16.     public void Configure(IApplicationBuilder app, IHostingEnvironment env)  
  17.     {  
  18.         //Important step for Memcached caching provider        
  19.         app.UseDefaultMemcached();  
  20.     }  
  21. }  

Note 

There are two steps to configure the Memcached caching provider. Don't forget to configure in Configure method.

The uses of this provider are same as of the In-Memory caching provider.

SQLite Caching Provider 

SQLite caching provider is based on Microsoft.Data.SQLite

How to configure it? 

Install EasyCaching.SQLite package via NuGet.
Install-Package EasyCaching.SQLite 

Add Configuration In Startup Class

  1. public class Startup  
  2. {  
  3.     //...  
  4.       
  5.     public void ConfigureServices(IServiceCollection services)  
  6.     {  
  7.         //other services.  
  8.   
  9.         //Important step for SQLite caching provider        
  10.         services.AddSQLiteCache(option=>{});  
  11.     }  
  12.       
  13.     public void Configure(IApplicationBuilder app, IHostingEnvironment env)  
  14.     {  
  15.         //Important step for SQLite caching provider        
  16.         app.UseSQLiteCache();  
  17.     }  
  18. }  

Note
There are two steps to configure the SQLite caching provider! Don't forget to configure in Configure method. 

The uses of this provider are same as of the In-Memory caching provider.

Summary

In this article, I've shown you the basic usages of EasyCaching. There are four basic caching providers of EasyCaching, you should choose the one that you need.

There are also some advanced usages of EasyCaching, such as Caching Interceptor, Hybrid caching provider. I will introduce them next time.

Hope this can help you!

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now