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. public void ConfigureServices(IServiceCollection services)
  5. {
  6. //other services.
  7. //Important step for In-Memory caching provider
  8. services.AddDefaultInMemoryCache();
  9. }
  10. }

Then, call the provider to handle caching.

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

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. public void ConfigureServices(IServiceCollection services)
  5. {
  6. //other services.
  7. //Important step for Redis caching provider
  8. services.AddDefaultRedisCache(option=>
  9. {
  10. option.Endpoints.Add(new ServerEndPoint("127.0.0.1", 6379));
  11. option.Password = "";
  12. //other settings of redis caching provider
  13. });
  14. }
  15. }

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. public void ConfigureServices(IServiceCollection services)
  5. {
  6. //other services.
  7. //Important step for Memcached caching provider
  8. services.AddDefaultMemcached(option=>
  9. {
  10. option.AddServer("127.0.0.1",11211);
  11. });
  12. }
  13. public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  14. {
  15. //Important step for Memcached caching provider
  16. app.UseDefaultMemcached();
  17. }
  18. }

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. public void ConfigureServices(IServiceCollection services)
  5. {
  6. //other services.
  7. //Important step for SQLite caching provider
  8. services.AddSQLiteCache(option=>{});
  9. }
  10. public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  11. {
  12. //Important step for SQLite caching provider
  13. app.UseSQLiteCache();
  14. }
  15. }

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!