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 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
- public class Startup
- {
-
-
- public void ConfigureServices(IServiceCollection services)
- {
-
-
-
- services.AddDefaultInMemoryCache();
- }
- }
Then, call the provider to handle caching.
- [Route("api/[controller]")]
- public class ValuesController : Controller
- {
- private readonly IEasyCachingProvider _provider;
-
- public ValuesController(IEasyCachingProvider provider)
- {
- this._provider = provider;
- }
-
- [HttpGet]
- public async Task<string> Get()
- {
-
- this._provider.Set("demo", "123", TimeSpan.FromMinutes(1));
-
-
- await this._provider.SetAsync("demo", "123", TimeSpan.FromMinutes(1));
-
-
- var res = this._provider.Get("demo", () => "456", TimeSpan.FromMinutes(1));
-
-
- var res = await this._provider.GetAsync("demo",async () => await Task.FromResult("456"), TimeSpan.FromMinutes(1));
-
-
- var res = this._provider.Get<string>("demo");
-
-
- var res = await this._provider.GetAsync<string>("demo");
-
-
- this._provider.Refresh("key", "123", TimeSpan.FromMinutes(1));
-
-
- await this._provider.RefreshAsync("key", "123", TimeSpan.FromMinutes(1));
-
-
- this._provider.Remove("demo");
-
-
- await this._provider.RemoveAsync("demo");
-
- return "OK";
- }
- }
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
- public class Startup
- {
-
-
- public void ConfigureServices(IServiceCollection services)
- {
-
-
-
- services.AddDefaultRedisCache(option=>
- {
- option.Endpoints.Add(new ServerEndPoint("127.0.0.1", 6379));
- option.Password = "";
-
- });
- }
- }
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
- public class Startup
- {
-
-
- public void ConfigureServices(IServiceCollection services)
- {
-
-
-
- services.AddDefaultMemcached(option=>
- {
- option.AddServer("127.0.0.1",11211);
- });
- }
-
- public void Configure(IApplicationBuilder app, IHostingEnvironment env)
- {
-
- app.UseDefaultMemcached();
- }
- }
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
- public class Startup
- {
-
-
- public void ConfigureServices(IServiceCollection services)
- {
-
-
-
- services.AddSQLiteCache(option=>{});
- }
-
- public void Configure(IApplicationBuilder app, IHostingEnvironment env)
- {
-
- app.UseSQLiteCache();
- }
- }
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!