JCode.ORMX.RedisCache 1.0.3

dotnet add package JCode.ORMX.RedisCache --version 1.0.3
                    
NuGet\Install-Package JCode.ORMX.RedisCache -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="JCode.ORMX.RedisCache" Version="1.0.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="JCode.ORMX.RedisCache" Version="1.0.3" />
                    
Directory.Packages.props
<PackageReference Include="JCode.ORMX.RedisCache" />
                    
Project file
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 JCode.ORMX.RedisCache --version 1.0.3
                    
#r "nuget: JCode.ORMX.RedisCache, 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.
#:package JCode.ORMX.RedisCache@1.0.3
                    
#: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=JCode.ORMX.RedisCache&version=1.0.3
                    
Install as a Cake Addin
#tool nuget:?package=JCode.ORMX.RedisCache&version=1.0.3
                    
Install as a Cake Tool

ORMX.RedisCache

ORMX.RedisCache 是 ORMX 框架的 Redis 缓存集成项目,为数据库操作提供高性能的缓存支持。通过 Redis 缓存,可以显著减少数据库访问次数,提高应用性能,特别适用于读密集型应用场景。

核心功能

  • 自动缓存查询结果:自动缓存数据库查询结果,减少重复查询
  • 支持缓存失效策略:在数据更新后及时使相关缓存失效
  • 集成健康检查:监控 Redis 连接状态,确保缓存系统稳定运行
  • 高性能序列化:使用 System.Text.Json 进行高性能序列化
  • 灵活的配置选项:支持多种缓存策略和配置参数
  • 故障降级处理:Redis 不可用时自动降级为直接访问数据库

安装

通过 NuGet 安装

使用 .NET CLI 安装:

dotnet add package JCode.ORMX.RedisCache

或者使用 NuGet 包管理器控制台:

Install-Package JCode.ORMX.RedisCache

快速开始

基本使用

using JCode.ORMX.RedisCache;
using JCode.ORMX.RedisCache.Options;

// 创建 Redis 缓存选项
var cacheOptions = new RedisCacheOptions
{
    ConnectionString = "localhost:6379"
};

// 在数据库提供程序中配置缓存
using var provider = new SqliteDatabaseProvider("Data Source=test.db")
    .WithRedisCache(cacheOptions);

// 获取表对象
var userTable = provider.GetTableManager().Table<User>();

// 自动缓存查询结果
var users = userTable.Where(u => u.Age > 25).GetList();

// 后续相同查询会使用缓存
var cachedUsers = userTable.Where(u => u.Age > 25).GetList();

配置选项

var cacheOptions = new RedisCacheOptions
{
    // Redis 连接字符串
    ConnectionString = "localhost:6379",
    
    // 缓存键前缀
    KeyPrefix = "ormx:",
    
    // 默认缓存过期时间
    DefaultExpiration = TimeSpan.FromMinutes(10),
    
    // 连接超时时间
    ConnectionTimeout = TimeSpan.FromSeconds(5),
    
    // 启用健康检查
    EnableHealthCheck = true,
    
    // 健康检查间隔
    HealthCheckInterval = TimeSpan.FromSeconds(30)
};

详细使用

缓存失效管理

// 手动使缓存失效
var cache = provider.GetRedisCache();
cache.Invalidate("users:active");

// 使多个缓存失效
cache.InvalidatePattern("users:*");

// 数据更新后使缓存失效
userTable.Update(user);
cache.InvalidatePattern($"user:{user.Id}:*");

自定义缓存策略

// 为特定查询设置缓存策略
var users = userTable
    .Where(u => u.Age > 25)
    .WithCacheExpiration(TimeSpan.FromHours(1)) // 自定义过期时间
    .WithCacheKey("active:users") // 自定义缓存键
    .GetList();

健康状态监控

// 获取 Redis 缓存健康状态
var cache = provider.GetRedisCache();
var healthStatus = cache.GetHealthStatus();

Console.WriteLine($"Redis 连接状态: {healthStatus.IsConnected}");
Console.WriteLine($"Redis 服务器版本: {healthStatus.ServerVersion}");

// 订阅健康状态变化事件
cache.HealthStatusChanged += (sender, e) =>
{
    Console.WriteLine($"Redis 健康状态变化: {e.OldStatus.IsConnected} -> {e.NewStatus.IsConnected}");
    if (!e.NewStatus.IsConnected)
    {
        Console.WriteLine($"Redis 连接失败: {e.Reason}");
    }
};

最佳实践

缓存键设计

  • 使用有意义的前缀:如 {app}:{entity}:
  • 包含必要的上下文信息:如用户ID、分类ID等
  • 保持键长度合理:避免过长的键名
  • 使用一致的命名规范

缓存过期策略

  • 热点数据:使用较长的过期时间(如小时级)
  • 频繁更新的数据:使用较短的过期时间(如分钟级)
  • 根据数据更新频率调整:数据更新越频繁,过期时间应越短

性能优化

  • 启用压缩:对于大型对象,启用压缩以减少网络传输
  • 合理配置连接池:根据并发量调整连接池大小
  • 监控缓存命中率:定期分析缓存使用情况
  • 考虑缓存预热:在应用启动时预加载热点数据

故障处理

Redis 连接失败

当 Redis 连接失败时,ORMX.RedisCache 会:

  1. 自动重试:根据配置的重试策略尝试重新连接
  2. 故障降级:重试失败后,自动降级为直接访问数据库
  3. 健康检查:持续监控 Redis 状态,恢复后自动重新启用缓存

常见问题

问题 可能原因 解决方案
连接超时 网络问题或 Redis 服务器负载过高 检查网络连接,调整连接超时时间
缓存不一致 数据更新后未使缓存失效 确保在数据更新后调用 Invalidate 方法
内存占用过高 缓存过期时间设置过长 调整缓存过期时间,使用内存淘汰策略
序列化错误 对象包含循环引用 优化对象结构,避免循环引用

依赖项

  • ORMX:基础 ORM 框架
  • StackExchange.Redis:Redis 客户端库
  • System.Text.Json:高性能序列化库

兼容性

  • .NET Standard 2.0+:支持跨平台应用
  • .NET Core 3.1+:支持现代 .NET 应用
  • .NET Framework 4.7.2+:支持传统 .NET 应用
Product 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 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  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.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on JCode.ORMX.RedisCache:

Package Downloads
JCode.ORMX.DbProvider.Rides

Rides database provider for ORMX - Object Relational Mapping Extended for .NET Rides 数据库提供程序,用于 ORMX - .NET 的对象关系映射扩展

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.3 127 2/10/2026