ArgMultiLevelCache 1.0.3
.NET Standard 2.0
This package targets .NET Standard 2.0. The package is compatible with this framework or higher.
.NET Framework 4.6.2
This package targets .NET Framework 4.6.2. The package is compatible with this framework or higher.
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
The owner has unlisted this package.
This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package ArgMultiLevelCache --version 1.0.3
NuGet\Install-Package ArgMultiLevelCache -Version 1.0.3
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="ArgMultiLevelCache" Version="1.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ArgMultiLevelCache" Version="1.0.3" />
<PackageReference Include="ArgMultiLevelCache" />
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 ArgMultiLevelCache --version 1.0.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: ArgMultiLevelCache, 1.0.3"
#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.
#addin nuget:?package=ArgMultiLevelCache&version=1.0.3
#tool nuget:?package=ArgMultiLevelCache&version=1.0.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
ArgMultiLevelCache 多级缓存类库
项目简介
ArgMultiLevelCache 是一个灵活的.NET多级缓存类库,支持多种缓存策略的组合使用。通过分层缓存机制,可以有效地平衡性能和资源使用。
主要特性
- 支持多级缓存策略,自动按优先级查找和同步数据
- 灵活的缓存层级配置,支持自定义缓存实现
- 内置内存缓存和Redis缓存实现
- 异步操作支持,提供完整的异步API
- 支持数据源回退和自动缓存填充
- 可扩展的接口设计,易于集成和扩展
工作原理
- 多级缓存查询:按照缓存级别优先级(数字越小优先级越高)依次查找数据
- 自动同步机制:当在高优先级缓存中找到数据时,自动同步到低优先级缓存
- 数据源回退:当所有缓存层都未命中时,支持通过委托从数据源获取数据
- 统一过期策略:支持为不同缓存层设置统一的过期时间
使用方法
包导入
dotnet add package ArgMultiLevelCache
平台使用说明
.NET Core/.NET 5+
在.NET Core和.NET 5+平台中,推荐使用依赖注入方式配置和使用缓存:
// 在Startup.cs中配置服务
public void ConfigureServices(IServiceCollection services)
{
services.AddMultiLevelCache(options =>
{
options.DefaultExpiration = TimeSpan.FromMinutes(30);
options.AddCacheLevel(new MemoryCache(), 1);
options.AddCacheLevel(new RedisCache("localhost:6379"), 2);
});
}
// 在服务中使用
public class UserService
{
private readonly IMultiLevelCache _cache;
public UserService(IMultiLevelCache cache)
{
_cache = cache;
}
public async Task<UserData> GetUserAsync(string userId)
{
return await _cache.GetAsync<UserData>(
$"user:{userId}",
async () => await _userRepository.GetByIdAsync(userId)
);
}
}
.NET Framework
在.NET Framework平台中,推荐使用静态工厂类进行全局访问,确保线程安全:
// 在应用程序启动时配置缓存(如Global.asax.cs中)
CacheFactory.Configure(options =>
{
options.DefaultExpiration = TimeSpan.FromMinutes(30);
options.AddCacheLevel(new MemoryCache(), 1); // 一级缓存:内存缓存
options.AddCacheLevel(new RedisCache("localhost:6379"), 2); // 二级缓存:Redis缓存
});
// 在任意位置使用缓存(线程安全)
public class UserService
{
public async Task<UserData> GetUserAsync(string userId)
{
return await CacheFactory.Default.GetAsync<UserData>(
$"user:{userId}",
async () => await GetUserFromDatabaseAsync(userId)
);
}
private async Task<UserData> GetUserFromDatabaseAsync(string userId)
{
// 从数据库获取用户数据的实现
return await Task.FromResult(new UserData());
}
}
// 在WebForm页面中使用
public partial class UserProfile : System.Web.UI.Page
{
private UserService _userService = new UserService();
protected async void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
await LoadUserDataAsync();
}
}
private async Task LoadUserDataAsync()
{
var userId = Request.QueryString["userId"];
var userData = await _userService.GetUserAsync(userId);
// 使用userData更新页面控件
}
}
基本用法
// 创建多级缓存管理器
var options = new MultiLevelCacheOptions
{
DefaultExpiration = TimeSpan.FromMinutes(30)
};
var cacheManager = new MultiLevelCacheManager(options);
// 添加内存缓存作为一级缓存
var memoryCache = new MemoryCache();
cacheManager.AddCache(memoryCache, 1);
// 使用缓存
await cacheManager.SetAsync("key", "value", TimeSpan.FromMinutes(10));
var value = await cacheManager.GetAsync<string>("key");
Redis缓存配置示例
// 创建Redis缓存实例
var redisCache = new RedisCache("localhost:6379");
// 添加到缓存管理器
cacheManager.AddCache(redisCache, 2); // 作为二级缓存
// 使用Redis缓存
await cacheManager.SetAsync("user:profile", userProfile, TimeSpan.FromHours(1));
var profile = await cacheManager.GetAsync<UserProfile>("user:profile");
多级缓存配置示例
// 创建多级缓存管理器
var options = new MultiLevelCacheOptions
{
DefaultExpiration = TimeSpan.FromHours(1)
};
var cacheManager = new MultiLevelCacheManager(options);
// 配置多级缓存(数字越小优先级越高)
cacheManager.AddCache(new MemoryCache(), 1); // 一级缓存:内存缓存
cacheManager.AddCache(new RedisCache("localhost:6379"), 2); // 二级缓存:Redis缓存
// 使用数据源回退
var userData = await cacheManager.GetAsync<UserData>(
"user:1",
async () => await userRepository.GetByIdAsync("1")
);
扩展开发
要实现自定义缓存提供者,只需实现ICache
接口:
public class CustomCache : ICache
{
public Task<T?> GetAsync<T>(string key) { ... }
public Task SetAsync<T>(string key, T value, TimeSpan? expiration = null) { ... }
public Task RemoveAsync(string key) { ... }
public Task<bool> ExistsAsync(string key) { ... }
}
性能优化建议
缓存层级优化
- 将热点数据放在内存缓存(一级缓存)中,减少网络请求
- 根据数据访问频率和重要性合理分配缓存层级
- 避免过多的缓存层级,一般2-3层即可满足大多数需求
数据访问优化
- 使用数据源回退机制,避免缓存未命中时的性能损失
// 示例:带有数据源回退的缓存访问
var data = await _cache.GetAsync<UserData>(
key,
async () => await _repository.GetDataAsync() // 仅在缓存未命中时执行
);
内存管理
- 为内存缓存设置合理的容量限制,避免内存溢出
- 使用滑动过期时间处理热点数据
- 定期清理过期数据,避免内存泄漏
最佳实践
缓存键设计
- 使用冒号分隔的命名方式,便于管理和查找
// 推荐的键命名方式
const string userProfileKey = "user:profile:{0}";
const string userSettingsKey = "user:settings:{0}";
const string userFriendsKey = "user:friends:{0}";
// 使用示例
var key = string.Format(userProfileKey, userId);
缓存过期策略
- 使用适当的过期时间,避免数据过期风暴
- 对不同类型的数据采用不同的过期策略
// 不同数据类型的过期时间示例
var shortTermExpiration = TimeSpan.FromMinutes(5); // 适用于变化频繁的数据
var mediumTermExpiration = TimeSpan.FromHours(1); // 适用于一般数据
var longTermExpiration = TimeSpan.FromDays(1); // 适用于相对稳定的数据
异常处理
- 实现降级策略,在缓存故障时保证系统可用性
- 使用Try-Catch处理缓存操作异常
public async Task<T?> GetWithFallbackAsync<T>(string key)
{
try
{
return await _cache.GetAsync<T>(key);
}
catch (Exception ex)
{
// 记录异常并降级到数据源
_logger.LogError(ex, "Cache access failed");
return await _repository.GetDataAsync<T>();
}
}
缓存预热
- 系统启动时预加载重要数据
- 使用异步方式进行缓存预热,避免阻塞
public async Task WarmUpCacheAsync()
{
var tasks = new List<Task>();
foreach (var key in _importantKeys)
{
tasks.Add(_cache.GetAsync<T>(
key,
async () => await _repository.GetDataAsync(key)
));
}
await Task.WhenAll(tasks);
}
注意事项
- 缓存级别数字越小,优先级越高(1级优先于2级)
- 避免缓存穿透:对空值也进行缓存,设置较短的过期时间
- 避免缓存雪崩:为过期时间添加随机偏移
- 在分布式环境中注意缓存一致性问题
- 定期监控缓存命中率,根据实际情况调整缓存策略
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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 was computed. 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 was computed. 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. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 is compatible. net463 was computed. net47 was computed. net471 was computed. net472 is compatible. net48 is compatible. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETFramework 4.6.2
- Microsoft.Extensions.DependencyInjection (>= 6.0.0)
- Microsoft.Extensions.Options (>= 6.0.0)
- Newtonsoft.Json (>= 13.0.3)
- StackExchange.Redis (>= 2.6.122)
-
.NETFramework 4.7.2
- Microsoft.Extensions.DependencyInjection (>= 6.0.0)
- Microsoft.Extensions.Options (>= 6.0.0)
- Newtonsoft.Json (>= 13.0.3)
- StackExchange.Redis (>= 2.6.122)
-
.NETFramework 4.8
- Microsoft.Extensions.DependencyInjection (>= 6.0.0)
- Microsoft.Extensions.Options (>= 6.0.0)
- Newtonsoft.Json (>= 13.0.3)
- StackExchange.Redis (>= 2.6.122)
-
.NETStandard 2.0
- Microsoft.Extensions.DependencyInjection (>= 6.0.0)
- Microsoft.Extensions.Options (>= 6.0.0)
- Newtonsoft.Json (>= 13.0.3)
- StackExchange.Redis (>= 2.6.122)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.29 | 141 | 5/29/2025 |