DotNetEasyCache is a simple way to handle caching items in your .Net project. It utilizes iDistributedCache
to help handle caching. It can be used with in memory, Redis, or SQL caching.
You can install DotNetEasyCache through Nuget package manager by running the following:
Install-Package EasyCache.Cache -Version 1.0.1
If you use the dotnet CLI you can use the following:
dotnet add package EasyCache.Cache --version 1.0.1
Add to appsettings.json In your appsettings.json you'll need to include the following entry to utilize DotNetEasyCache:
You can use "Memory", "Redis", or "Sql" as the CacheHandler
value.
"EasyCache": {
"CacheHandler": "Memory",
"Redis": {
"Host": "127.0.0.1:6379",
"Instance": "0"
},
"Sql": {
"CacheConnectionString": "",
"SchemaName": "",
"TableName": ""
}
}
To create a SQL Server cached item table in a SQL Server instance, you can use the sql-cache tool. The tool creates a table with the name and schema that you specify.
Create a table in SQL Server by running the sql-cache create command. Provide the SQL Server instance (Data Source), database (Initial Catalog), schema (for example, dbo), and table name (for example, TestCache):
You'll also want to use that same connection string, schema, and table name in your appsettings.json
dotnet sql-cache create "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=DistCache;Integrated Security=True;" dbo TestCache
Program.cs Changes
Add the following line to your program.cs
file:
builder.Services.SetEasyCacheServices();
This will implement all the DotNetEasyCache required services.
If you are using async method you can add "Async" to the end of the method you are using. ex:
_easyCache.Put("key", "value", seconds);
would become
await _easyCache.PutAsync("key", "value", seconds);
You can utilize dependency injection to bring the DotNetEasyCache into your project files. An example when using DotNetEasyCache in your a controller can be seen here:
private readonly IEasyCache _easyCache;
public IndexModel(IEasyCache easyCache)
{
_easyCache = easyCache;
}
You may use the Put method to store items in the cache. The Put method requires you to set an expiration in seconds.
The "key" value is the name you want your cache item to be stored as, and the "value" is what you are storing.
When using DotNetEasyCache in the DI container you can use the following:
var seconds = 10;
_easyCache.Put("key", "value", seconds);
To store an item that does not expire you can use the Forever method. This will store an item in cache until it is removed manually
_easyCache.Forever("key", "value");
The Get method is used to retrieve items from the cache. If the item does not exist in the cache, null will be returned.
_easyCache.Get("key");
You may remove items from the cache using the forget method:
_easyCache.Forget("key")
The Exists method can be used to determine if an item exists in the cache. If the value is null this method will return false
_easyCache.Exists("key");
DotNetEasyCache is open-sourced software licensed under the MIT License