Last time I recommended a caching middleware to you "a very practical caching middleware developed by C# ", and today I recommend a caching middleware to you. Caching, support for multiple Providers, etc.
Project Description
This is a cache middleware developed based on .Net Core, which supports various caches and provides many advanced functions. Its main goal is to make it easier for developers to develop, especially some complex caching scenarios.
Project features
1. Unified cache interface: it is convenient for us to adjust the cache strategy at any time;
2. Support multiple caches: it can meet our various business scenarios;
3. Support multiple cache serialization: BinaryFormatter, Newtonsoft.Json, MessagePack and Protobuf;
4. Support caching AOP: able, put, evict, which can simplify our code volume;
5. Multi-instance support;
6. Support Diagnostics: it is convenient for us to track and locate;
7. Support special Providers for Redis: such as atomic increment and decrement operations, etc.;
8. Secondary cache.
Technology Architecture
1. Cross-platform: This is a system developed based on .Net Core, which can be deployed on Docker, Windows, and Linux.
2. Developed based on Net 6.0.
3. Supported cache categories: local cache: InMemory, SQLite; distributed cache: StackExchange.Redis, csredis, EnyimMemcachedCore.
project structure
Instructions
configuration cache
In Startup.cs, configure caching
public void ConfigureServices(IServiceCollection services)
{
......
services.AddEasyCaching(option =>
{
//内存缓存:default
option.UseInMemory("default");
//内存缓存:cus
option.UseInMemory("cus");
//redis缓存:redis1
option.UseRedis(config =>
{
config.DBConfig.Endpoints.Add(new ServerEndPoint("127.0.0.1", 6379));
config.DBConfig.SyncTimeout = 10000;
config.DBConfig.AsyncTimeout = 10000;
config.SerializerName = "mymsgpack";
}, "redis1")
.WithMessagePack("mymsgpack")//with messagepack serialization
;
//redis缓存:redis2
option.UseRedis(config =>
{
config.DBConfig.Endpoints.Add(new ServerEndPoint("127.0.0.1", 6380));
}, "redis2");
//sqlite缓存
option.UseSQLite(config =>
{
config.DBConfig = new SQLiteDBOptions { FileName = "my.db" };
});
//memcached 缓存
option.UseMemcached(config =>
{
config.DBConfig.AddServer("127.0.0.1", 11211);
});
option.UseMemcached(Configuration);
//fasterKv缓存
option.UseFasterKv(config =>
{
config.SerializerName = "msg";
})
.WithMessagePack("msg");
});
}
How to use
public class CusController : Controller
{
//缓存
private readonly IEasyCachingProviderFactory _factory;
public CusController(IEasyCachingProviderFactory factory)
{
this._factory = factory;
}
// GET api/cus/inmem?name=Default
[HttpGet]
[Route("inmem")]
public string Get(string name = EasyCachingConstValue.DefaultInMemoryName)
{
//根据name,获取缓存实例
var provider = _factory.GetCachingProvider(name);
var val = name.Equals("cus") ? "cus" : "default";
var res = provider.Get("demo", () => val, TimeSpan.FromMinutes(1));
return $"cached value : {res}";
}
......
}
ResponseCache cache
[ResponseCache(Duration = 30, VaryByQueryKeys = new string[] { "page" })]
public IActionResult List(int page = 0)
{
return Content(page.ToString());
}
AOP caching
[EasyCachingAble(Expiration = 10)]
string GetCurrentUtcTime();
[EasyCachingPut(CacheKeyPrefix = "Castle")]
string PutSomething(string str);
[EasyCachingEvict(IsBefore = true)]
void DeleteSomething(int id);
project address
https://github.com/dotnetcore/EasyCaching
- End -
recommended reading
A .Net Core open source monitoring solution that supports Redis, Elasticsearch, SqlServer
A WinForm open source UI component framework supporting .Net 7
Inventory of 3 WMS warehouse management systems developed by .Net
Recommend a configuration center open source project developed by .Ner Core
An open source project for EF Core lightweight sub-table sub-library, read-write separation