Kemenkeu.Caching.Core
1.0.0
dotnet add package Kemenkeu.Caching.Core --version 1.0.0
NuGet\Install-Package Kemenkeu.Caching.Core -Version 1.0.0
<PackageReference Include="Kemenkeu.Caching.Core" Version="1.0.0" />
paket add Kemenkeu.Caching.Core --version 1.0.0
#r "nuget: Kemenkeu.Caching.Core, 1.0.0"
// Install Kemenkeu.Caching.Core as a Cake Addin #addin nuget:?package=Kemenkeu.Caching.Core&version=1.0.0 // Install Kemenkeu.Caching.Core as a Cake Tool #tool nuget:?package=Kemenkeu.Caching.Core&version=1.0.0
Kemenkeu.Caching.Core
Kemenkeu.Caching.Core is a caching library based on EasyCaching that supports both In-Memory and Redis caching providers with configurable options.
Installation
Install the library via NuGet:
dotnet add package Kemenkeu.Caching.Core
Configuration
Add the following configuration to your appsettings.json
file to set up caching options:
{
"Caching": {
"Provider": "Redis", // "InMemory" or "Redis"
"RedisEndpoints": [ "localhost:6379" ],
"RedisPassword": "your_redis_password",
"RedisKeyPrefix": "sample",
"RedisDbIndex": 0,
"InMemoryExpirationMinutes": 2,
"RedisExpirationMinutes": 30
}
}
Usage
The IKemenkeuCaching
interface provides methods to interact with the cache.
public interface IKemenkeuCaching
{
Task<CacheValue<T>> GetCacheAsync<T>(string key);
Task<T> GetValueAsync<T>(bool? refresh = null, params string[] keys);
Task SetAsync<T>(string key, T value, int expiration);
Task SetAsync<T>(T value, params string[] keys);
Task SetAsync<T>(T value, int? expiration = null, params string[] keys);
Task RemoveAsync(string key);
}
1. Setup in Startup.cs
In .NET 5 or Older, use Startup.cs
to configure services and middleware.
Register Kemenkeu.Caching
in the ConfigureServices
method:
public class Startup
{
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
var cachingOptions = Configuration.GetSection("Caching").Get<CachingOptions>();
services.AddKemenkeuCaching(cachingOptions);
// Other services...
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// Middleware configuration...
}
}
2. Setup in Program.cs
In .NET 6 or later, configure services directly in Program.cs
.
Configure services and add caching:
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
var cachingOptions = builder.Configuration.GetSection("Caching").Get<CachingOptions>();
builder.Services.AddKemenkeuCaching(cachingOptions);
var app = builder.Build();
// Configure the HTTP request pipeline.
app.UseAuthorization();
app.MapControllers();
app.Run();
3. Setup in your Project Controller
or Service
Sample usage IKemenkeuCaching
in your application logic:
public class SampleService
{
private readonly IKemenkeuCaching _caching;
public SampleService(IKemenkeuCaching caching)
{
_caching = caching;
}
public async Task SetDataAsync(string key, object data)
{
await _caching.SetAsync(key, data);
}
public async Task<object> GetDataAsync(string key)
{
return await _caching.GetAsync<object>(key);
}
}
Using EnableCacheAttribute
The EnableCacheAttribute
allows you to cache responses automatically by specifying the cache key type, expiration, and options like including query parameters.
Example Usage
Caching User-Specific Data (UserData) with Query Parameters
[HttpGet] [EnableCache(CacheKeyType.UserData, expirationMinutes: 60, refreshCache: false, includeQueryParams: true)] public IActionResult GetUserProfile([FromQuery] string detailLevel) { var data = GetUserProfileDataFromDataSource(detailLevel); return Ok(data); }
Caching System Data (SystemData) without Query Parameters
[HttpGet] [EnableCache(CacheKeyType.SystemData, expirationMinutes: 30, includeQueryParams: false)] public IActionResult GetSystemData([FromQuery] int page, [FromQuery] int pageSize) { var data = GetSystemDataFromDataSource(page, pageSize); return Ok(data); }
Caching Organization Data (OrganizationData) with Default Settings
[HttpGet] [EnableCache(CacheKeyType.OrganizationData)] public IActionResult GetOrganizationData() { var data = GetOrganizationDataFromDataSource(); return Ok(data); }
Custom Cache Key with Multiple Parts
[HttpGet("custom/{id}")] [EnableCache(45, refreshCache: false, includeQueryParams: true, "CustomDataCacheKey", "{id}")] public IActionResult GetCustomData(int id) { var data = GetCustomDataFromDataSource(id); return Ok(data); }
Using InvalidateCacheAttribute
The InvalidateCacheAttribute
is used to clear cached entries when data is modified. It automatically removes the cache entry for the specified CacheKeyType
after a successful action execution.
Example Usage
Invalidate User Data Cache on Profile Update
[HttpPut] [InvalidateCache(CacheKeyType.UserData,"ControllerName", "GetUserProfile")] public IActionResult UpdateUserProfile([FromBody] UserProfileModel profile) { UpdateUserProfileDataInDataSource(profile); return Ok(); }
Invalidate System Data Cache on System Update
[HttpPut("system")] [InvalidateCache(CacheKeyType.SystemData,"ControllerName", "GetSystemData")] public IActionResult UpdateSystemData([FromBody] SystemDataModel data) { UpdateSystemDataInDataSource(data); return Ok(); }
Invalidate Custom Data Cache with Flexible Key
[HttpDelete("custom/{id}")] [InvalidateCache("CustomDataCacheKey", "{id}")] public IActionResult DeleteCustomData(int id) { DeleteCustomDataFromDataSource(id); return Ok(); }
Contributing
Contributions are welcome! Please submit a pull request or open an issue to suggest improvements.
License
This project is licensed under the MIT License.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.1 is compatible. |
-
.NETCoreApp 3.1
- EasyCaching.Core (>= 1.9.2)
- EasyCaching.InMemory (>= 1.9.2)
- EasyCaching.Redis (>= 1.9.2)
- EasyCaching.Serialization.MessagePack (>= 1.9.2)
- Microsoft.AspNetCore.Mvc.NewtonsoftJson (>= 3.1.32)
-
net5.0
- EasyCaching.Core (>= 1.9.2)
- EasyCaching.InMemory (>= 1.9.2)
- EasyCaching.Redis (>= 1.9.2)
- EasyCaching.Serialization.MessagePack (>= 1.9.2)
- Microsoft.AspNetCore.Mvc.NewtonsoftJson (>= 5.0.17)
-
net6.0
- EasyCaching.Core (>= 1.9.2)
- EasyCaching.InMemory (>= 1.9.2)
- EasyCaching.Redis (>= 1.9.2)
- EasyCaching.Serialization.MessagePack (>= 1.9.2)
- Microsoft.AspNetCore.Mvc.NewtonsoftJson (>= 6.0.35)
-
net7.0
- EasyCaching.Core (>= 1.9.2)
- EasyCaching.InMemory (>= 1.9.2)
- EasyCaching.Redis (>= 1.9.2)
- EasyCaching.Serialization.MessagePack (>= 1.9.2)
- Microsoft.AspNetCore.Mvc.NewtonsoftJson (>= 7.0.20)
-
net8.0
- EasyCaching.Core (>= 1.9.2)
- EasyCaching.InMemory (>= 1.9.2)
- EasyCaching.Redis (>= 1.9.2)
- EasyCaching.Serialization.MessagePack (>= 1.9.2)
- Microsoft.AspNetCore.Mvc.NewtonsoftJson (>= 8.0.10)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0 | 59 | 11/15/2024 |