Easy.Cache.Core
2026.4.24
dotnet add package Easy.Cache.Core --version 2026.4.24
NuGet\Install-Package Easy.Cache.Core -Version 2026.4.24
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Easy.Cache.Core" Version="2026.4.24" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Easy.Cache.Core" Version="2026.4.24" />
<PackageReference Include="Easy.Cache.Core" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Easy.Cache.Core --version 2026.4.24
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Easy.Cache.Core, 2026.4.24"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Easy.Cache.Core@2026.4.24
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Easy.Cache.Core&version=2026.4.24
#tool nuget:?package=Easy.Cache.Core&version=2026.4.24
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
6. 通过Aop使用内存缓存对接口、方法进行缓存
1. nuget包引入
<PackageReference Include="Easy.Cache.Core" Version="2026.4.24" />
2. 配置
//如果使用内存缓存则不需要配置,如果使用redis缓存则需要配置redis连接字符串
"RedisConfigurations": {
"DefaultRedis": "TestCache",//默认缓存客户端,不填则随便取一个当默认库
"RedisClients": {
"TestCache": {//单机模式配置(与集群模式配置是一样的)
"ConnectionString": "localhost:6379,abortConnect=false,ssl=false,password=123456,defaultDatabase=0"
},
"SentinelCache": {//哨兵模式配置
"ConnectionString": "mymaster,abortConnect=false,ssl=false,password=123456",
"SentinelNodes":[
"localhost:26379",
"localhost:26380",
]
},
"ClusterCache": {//集群模式配置
"ConnectionString": "localhost:6379,abortConnect=false,ssl=false,password=123456"
}
}
}
使用缓存
//开启内存缓存,如果有redis会使用redis,如果没有则会使用内存缓存
builder.Services.AddEasyCacheServiceSetup(configuration,"RedisConfigurations");//默认不传就是RedisConfigurations,如果是其他名称需要在这里修改,
2. 使用缓存\缓存切换
public class CacheResultService : ICacheResultService
{
private readonly IEasyCacheService _cacheService;
public CacheResultService(IEasyCacheService cacheService)
{
_cacheService = cacheService;
}
public Student GetStudentAsync(string name)
{
//直接使用缓存对象进行缓存操作
return _cacheService.Get($"student:{name}", "1", 30);
}
}
//缓存客户端切换
public class CacheResultService : ICacheResultService
{
private readonly IEasyCacheService _cacheService;
public CacheResultService(IEasyCacheServiceFactory cacheServiceFactory)
{
_cacheService = cacheServiceFactory.Create("TestCache");//切换到redis缓存客户端进行缓存操作
}
public Student GetStudentAsync(string name)
{
//切换到redis缓存客户端进行缓存操作
return _cacheService.Get($"student:{name}", "1", 30);
}
}
3. 对控制器接口使用内存缓存
builder.Services.AddControllers(options =>
{
//添加自定义的缓存过滤器
options.Filters.Add<EasyCacheResultFilter>();
});
方法缓存使用示例
//同一个方法不同参数会进行多次缓存
//接口注入直接继承IProxyService接口(继承接口后就不需要再手动注入了),方法上使用EasyCacheResultAttribute特性即可
public class CacheResultService : ICacheResultService
{
//缓存5秒
[EasyCacheResult(5)]
public Student GetStudentAsync(string name)
{
return new Student { Name = name };
}
}
public interface ICacheResultService : IProxyService
{
Student GetStudentAsync(string name);
}
控制器缓存使用示例
[HttpGet]
[EasyCacheResult(5)]//缓存5秒
public object Get()
{
_cacheResultService.GetStudentAsync("这是参数");
return "ok";
}
EasyCacheService中方法使用示例
// 从 DI 获取缓存服务(已在 AddEasyCacheServiceSetup 中注册)
var cache = serviceProvider.GetRequiredService<IEasyCacheService>();
// 基本写入与读取
cache.Add("student:1", new Student { Name = "张三" }, 60); // 60 秒后过期
var s1 = cache.Get<Student>("student:1");
await cache.AddAsync("student:2", new Student { Name = "李四" }, 30);
var s2 = await cache.GetAsync<Student>("student:2");
// GetOrAdd:未命中时回源并写入
var stu = cache.GetOrAdd("student:3", () => new Student { Name = "王五" }, 120);
var stuAsync = await cache.GetOrAddAsync("student:4", async () =>
{
await Task.Delay(10);
return new Student { Name = "赵六" };
}, 120);
// Hash 操作(在内存实现中以 ConcurrentDictionary 模拟;在 Redis 中为真实 Hash)
cache.HSet("user:100:props", "age", 30, 3600);
var age = cache.HGet<int>("user:100:props", "age");
var hasAge = cache.HExists("user:100:props", "age");
// 批量写入(推荐在 Redis 下使用以获得更好性能)
cache.Batch(new Dictionary<string, object>
{
["k1"] = "v1",
["k2"] = 123
}, 60);
// 删除与判断
cache.Remove("k1");
var exists = cache.Exists("k2");
// 递增与分布式锁(仅 Redis 实现支持;内存实现会抛出 NotSupportedException)
try
{
var newVal = cache.Increment("counter:pageviews", 1);
}
catch (NotSupportedException)
{
// 内存缓存不支持递增,请使用 Redis 客户端或切换到 Redis
}
// 分布式锁(仅 Redis 实现可跨实例安全)
using (var locker = cache.Lock("job:lock", 30))
{
// 在锁内执行需要互斥的逻辑
}
// 发布/订阅(仅 Redis 实现支持跨实例)
cache.Publish("notifications", "hello world");
cache.Subscribe("notifications", msg => Console.WriteLine($"收到消息: {msg}"));
// 如果需要显式切换到某个 Redis 客户端
var factory = serviceProvider.GetRequiredService<IEasyCacheServiceFactory>();
var redisClient = factory.Create("TestCache");
// 接下来使用 redisClient 与上面相同的方法签名进行调用
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net6.0
- Castle.Core (>= 5.1.1)
- CSRedisCore (>= 3.8.804)
- Easy.Common.Core (>= 2026.4.18.1)
- Microsoft.AspNetCore.Mvc.Abstractions (>= 2.3.0)
- Microsoft.Extensions.Caching.Memory (>= 8.0.1)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 8.0.0)
- WBC66.Autofac.Core (>= 2024.11.7)
-
net7.0
- Castle.Core (>= 5.1.1)
- CSRedisCore (>= 3.8.804)
- Easy.Common.Core (>= 2026.4.18.1)
- Microsoft.AspNetCore.Mvc.Abstractions (>= 2.3.0)
- Microsoft.Extensions.Caching.Memory (>= 8.0.1)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 8.0.0)
- WBC66.Autofac.Core (>= 2024.11.7)
-
net8.0
- Castle.Core (>= 5.1.1)
- CSRedisCore (>= 3.8.804)
- Easy.Common.Core (>= 2026.4.18.1)
- Microsoft.AspNetCore.Mvc.Abstractions (>= 2.3.0)
- Microsoft.Extensions.Caching.Memory (>= 8.0.1)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 8.0.0)
- WBC66.Autofac.Core (>= 2024.11.7)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
直接使用CsRedis/MemoryCache缓存内置自动切换