一個(gè).Net Core開(kāi)源緩存中間件,讓你更加簡(jiǎn)單、方便使用緩存
項(xiàng)目簡(jiǎn)介
這是一個(gè)基于.Net Core開(kāi)發(fā)的緩存中間件,它支持各種緩存并提供了很多高級(jí)功能。它的主要目標(biāo)是讓開(kāi)發(fā)人員開(kāi)發(fā)更簡(jiǎn)單、特別是一些復(fù)雜的緩存場(chǎng)景。
項(xiàng)目特色功能
1、統(tǒng)一緩存接口:方便我們隨時(shí)調(diào)整緩存策略;
2、支持多種緩存:可以滿足我們多種業(yè)務(wù)場(chǎng)景;
3、支持多種緩存系列化:BinaryFormatter、Newtonsoft.Json,MessagePack和Protobuf;
4、支持緩存AOP:able, put,evict,可以簡(jiǎn)化我們的代碼量;
5、多實(shí)例支持;
6、支持Diagnostics:方便我們跟蹤定位;
7、針對(duì)Redis支持特殊Provider:比如原子遞增遞減的操作等等;
8、二級(jí)緩存。
技術(shù)架構(gòu)
1、跨平臺(tái):這是基于.Net Core開(kāi)發(fā)的系統(tǒng),可以部署在Docker, Windows, Linux。
2、基于Net 6.0開(kāi)發(fā)。
3、支持緩存類別:本地緩存:InMemory,SQLite;分布式緩存:StackExchange.Redis,csredis,EnyimMemcachedCore。
項(xiàng)目結(jié)構(gòu)

===
使用方法
**配置緩存
**
在Startup.cs,配置緩存
public void ConfigureServices(IServiceCollection services)
{
? ?......
? ?services.AddEasyCaching(option =>
? ?{ ? ? ? ?//內(nèi)存緩存:default
? ? ? ?option.UseInMemory("default"); ? ? ? ?//內(nèi)存緩存: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");
? ?});
}
**使用方式
**
public class CusController : Controller{ ? ?//緩存
? ?private readonly IEasyCachingProviderFactory _factory; ? ?public CusController(IEasyCachingProviderFactory factory)
? ?{ ? ? ? ?this._factory = factory;
? ?} ? ?// GET api/cus/inmem?name=Default
? ?[ ]
? ?[ ] ? ?public string Get(string name = EasyCachingConstValue.DefaultInMemoryName)
? ?{ ? ? ? ?//根據(jù)name,獲取緩存實(shí)例
? ? ? ?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緩存
[ResponseCache(Duration = 30, VaryByQueryKeys = new string[] { "page" })]public IActionResult List(int page = 0){return Content(page.ToString());
}
AOP緩存
[string GetCurrentUtcTime();
[ ]string PutSomething(string str);
[ ]void DeleteSomething(int id);
]