An implementation of IDistributedCache backed by MinIO with optional fallback to in-memory cache
$ dotnet add package MinIO-IDistributedCacheA high-performance distributed cache implementation for ASP.NET Core using MinIO object storage.
This library provides an IDistributedCache implementation that uses MinIO, an S3-compatible object storage, as its backend. It's designed to be a scalable and reliable caching solution for distributed applications.
dotnet add package MinioDistributedCache
Program.cs or Startup.cs:builder.Services.AddMinioDistributedCache(options =>
{
options.Endpoint = "minio:9000";
options.AccessKey = "minioadmin";
options.SecretKey = "minioadmin";
options.BucketName = "aspnetcore-cache";
options.UseSSL = false;
options.UseFallbackCache = true; // Use in-memory fallback if MinIO is unavailable
});
public class WeatherForecastController : ControllerBase
{
private readonly IDistributedCache _cache;
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(
IDistributedCache cache,
ILogger<WeatherForecastController> logger)
{
_cache = cache;
_logger = logger;
}
[HttpGet]
public async Task<IActionResult> Get()
{
// Try to get from cache
var cachedData = await _cache.GetStringAsync("WeatherForecast");
if (cachedData != null)
{
_logger.LogInformation("Returning cached weather data");
return Ok(JsonSerializer.Deserialize<WeatherForecast[]>(cachedData));
}
// Cache miss - generate new data
var forecast = GenerateForecasts();
// Cache the result
await _cache.SetStringAsync(
"WeatherForecast",
JsonSerializer.Serialize(forecast),
new DistributedCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(10)
});
return Ok(forecast);
}
}
| Option | Description | Default |
|---|---|---|
Endpoint | MinIO server endpoint | "" |
AccessKey | MinIO access key | "" |
SecretKey | MinIO secret key | "" |
BucketName | Bucket name for cache storage | "aspnetcore-distributed-cache" |
UseSSL | Whether to use SSL for MinIO connection | false |
Region | MinIO region (optional) | null |
UseFallbackCache | Whether to use in-memory fallback cache | true |
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.