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.
Deforked from original project in january of 2021. In addition to original version have decorators for resilience, various performance improvements and bug fixes.
Nuget Packages
Basic Usages
Step 1 : Install the package
Choose caching provider that you need and install it via Nuget.
Install-Package Mindbox.EasyCaching.InMemory
Install-Package Mindbox.EasyCaching.Redis
Install-Package Mindbox.EasyCaching.HybridCache
Step 2 : Configure Startup class
Each caching provider has it's own configuration options.
Here is a sample configuration for InMemory and Redis caching provider.
public class Startup
{
//...
public void ConfigureServices(IServiceCollection services)
{
//configuration
services.AddEasyCaching(options =>
{
//use memory cache that named default configuration
options.UseInMemory("MemoryCache");
// use memory cache with your own configuration
options.UseInMemory(
"CustomMemoryCache",
config =>
{
config.DBConfig = new InMemoryCachingOptions
{
// scan time, default value is 60s
ExpirationScanFrequency = 60,
// total count of cache items, default value is 10000
SizeLimit = 100
};
// the max random second will be added to cache's expiration, default value is 120
config.MaxRdSecond = 120;
// whether enable logging, default is false
config.EnableLogging = false;
// mutex key's alive time(ms), default is 5000
config.LockMs = 5000;
// when mutex key alive, it will sleep some time, default is 300
config.SleepMs = 300;
// whether null values should be cached, default is false
config.CacheNulls = true;
});
//use redis cache that named redisCache
options.WithJson("Json");
options.UseRedis(
"RedisCache",
config =>
{
// StackExchange Redis connection string
config.ConnectionString = "127.0.0.1:6379";
// with json serialization
config.SerializerName = "Json";
});
//use redis cache with circuit breaker and fallback
options.WithJson("Json");
options.UseRedis(
"ResilentRedisCache",
config =>
{
config.ConnectionString = "127.0.0.1:6379";
config.SerializerName = "Json";
var circuitBreakerParameters = new CircuitBreakerParameters(
exceptionsAllowedBeforeBreaking: 10,
durationOfBreak: TimeSpan.FromMinutes(1));
config
.DecorateWithCircuitBreaker(
initParameters: circuitBreakerParameters,
executeParameters: circuitBreakerParameters,
RedisOptionsExtensions.RedisExceptionFilter)
.DecorateWithFallback(
(name, _) => new NullCachingProvider(name, config),
RedisOptionsExtensions.RedisExceptionFilter);
});
});
}
}
Step 3 : Write code in your controller
[Route("api/[controller]")]
public class ValuesController : Controller
{
// //when using single provider
// private readonly IEasyCachingProvider _provider;
//when using multiple providers
private readonly IEasyCachingProviderFactory _factory;
public ValuesController(
//IEasyCachingProvider provider,
IEasyCachingProviderFactory factory
)
{
//this._provider = provider;
this._factory = factory;
}
[HttpGet]
public string Handle()
{
//var provider = _provider;
//get the provider from factory with its name
var provider = _factory.GetCachingProvider("redis1");
//Set
provider.Set("demo", "123", TimeSpan.FromMinutes(1));
//Set Async
await provider.SetAsync("demo", "123", TimeSpan.FromMinutes(1));
}
}
Documentation
Detailed EasyCaching documentation can be found here.
Examples
See sample
Contributing
Pull requests, issues and commentary are welcomed!