Tenon.Caching.RedisStackExchange 0.0.1-alpha-202502101554

This is a prerelease version of Tenon.Caching.RedisStackExchange.
dotnet add package Tenon.Caching.RedisStackExchange --version 0.0.1-alpha-202502101554                
NuGet\Install-Package Tenon.Caching.RedisStackExchange -Version 0.0.1-alpha-202502101554                
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="Tenon.Caching.RedisStackExchange" Version="0.0.1-alpha-202502101554" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Tenon.Caching.RedisStackExchange --version 0.0.1-alpha-202502101554                
#r "nuget: Tenon.Caching.RedisStackExchange, 0.0.1-alpha-202502101554"                
#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.
// Install Tenon.Caching.RedisStackExchange as a Cake Addin
#addin nuget:?package=Tenon.Caching.RedisStackExchange&version=0.0.1-alpha-202502101554&prerelease

// Install Tenon.Caching.RedisStackExchange as a Cake Tool
#tool nuget:?package=Tenon.Caching.RedisStackExchange&version=0.0.1-alpha-202502101554&prerelease                

Tenon.Caching.RedisStackExchange

NuGet version License: MIT

基于 StackExchange.Redis 的高性能 Redis 缓存实现,为 .NET 应用程序提供分布式缓存解决方案。

✨ 功能特性

  • 🚀 基于 StackExchange.Redis 的高性能实现
  • 🔧 支持自定义缓存配置
  • 💉 集成依赖注入框架
  • 🎯 统一的 ICacheProvider 接口
  • 🔄 支持命名服务注入
  • 📊 完整的单元测试覆盖
  • 🛡️ 异常重试和容错处理

📦 安装方式

通过 NuGet 包管理器安装:

dotnet add package Tenon.Caching.RedisStackExchange

🚀 快速入门

1. 配置 appsettings.json

{
  "RedisCache": {
    "MaxRandomSecond": 5,
    "Redis": {
      "ConnectionString": "localhost:6379,defaultDatabase=0,connectTimeout=4000,allowAdmin=true,abortConnect=false,syncTimeout=5000"
    }
  }
}

2. 注册服务

// 使用默认配置
services.AddRedisStackExchangeCache(
    configuration.GetSection("RedisCache:Redis"));

// 或使用命名服务
services.AddKeyedRedisStackExchangeCache(
    "CustomCache",
    configuration.GetSection("RedisCache:Redis"),
    options => 
    {
        // 自定义缓存选项
    });

3. 使用缓存服务

public class ProductService
{
    private readonly ICacheProvider _cache;

    public ProductService(ICacheProvider cache)
    {
        _cache = cache;
    }

    public async Task<Product> GetProductAsync(int id)
    {
        var cacheKey = $"product:{id}";
        
        // 尝试从缓存获取数据
        if (_cache.TryGet(cacheKey, out Product? product))
            return product;

        // 缓存未命中,从数据库获取
        product = await GetProductFromDbAsync(id);
        
        // 存入缓存,设置 1 小时过期
        _cache.Set(cacheKey, product, TimeSpan.FromHours(1));
        
        return product;
    }
}

📖 高级用法

使用命名服务

public class MultiCacheService
{
    private readonly ICacheProvider _defaultCache;
    private readonly ICacheProvider _customCache;

    public MultiCacheService(
        ICacheProvider defaultCache,
        [FromKeyedServices("CustomCache")] ICacheProvider customCache)
    {
        _defaultCache = defaultCache;
        _customCache = customCache;
    }

    public async Task<Product> GetProductWithBackupAsync(int id)
    {
        // 先从主缓存获取
        var cacheKey = $"product:{id}";
        if (_defaultCache.TryGet(cacheKey, out Product? product))
            return product;

        // 从备份缓存获取
        if (_customCache.TryGet(cacheKey, out product))
        {
            // 同步到主缓存
            _defaultCache.Set(cacheKey, product, TimeSpan.FromHours(1));
            return product;
        }

        // 都未命中,从数据源获取
        return await GetProductFromSourceAsync(id);
    }
}

批量操作

public class BulkOperationExample
{
    private readonly ICacheProvider _cache;

    public BulkOperationExample(ICacheProvider cache)
    {
        _cache = cache;
    }

    public void BatchSetProducts(List<Product> products)
    {
        foreach (var product in products)
        {
            _cache.Set(
                $"product:{product.Id}", 
                product, 
                TimeSpan.FromHours(1));
        }
    }

    public List<Product> GetProductsByIds(List<int> ids)
    {
        return ids
            .Select(id => _cache.TryGet($"product:{id}", out Product? product) 
                ? product 
                : null)
            .Where(p => p != null)
            .ToList();
    }
}

⚙️ 配置选项说明

Redis 连接配置

配置项 说明 示例值
ConnectionString Redis 连接字符串 localhost:6379
DefaultDatabase 默认数据库编号 0
ConnectTimeout 连接超时时间(ms) 4000
SyncTimeout 同步操作超时时间(ms) 5000
AllowAdmin 允许管理员操作 true
AbortConnect 连接失败时终止 false

缓存选项

配置项 说明 默认值
MaxRandomSecond 过期时间随机偏移最大秒数 5
RetryCount 操作重试次数 3
RetryInterval 重试间隔(ms) 1000

🔨 项目依赖

  • StackExchange.Redis
  • Tenon.Caching.Abstractions
  • Tenon.Caching.Redis
  • Tenon.Serialization.Json
  • Microsoft.Extensions.Configuration
  • Microsoft.Extensions.DependencyInjection

📝 使用注意事项

1. 连接管理

  • 合理配置连接池大小
  • 设置适当的超时时间
  • 启用连接复用

2. 性能优化

  • 使用批量操作减少网络往返
  • 合理设置数据序列化格式
  • 避免存储过大的数据

3. 最佳实践

  • 实现缓存穿透保护
  • 使用分布式锁避免缓存击穿
  • 采用合理的缓存更新策略

✅ 单元测试

项目包含完整的单元测试:Tenon.Caching.RedisStackExchangeTests

[TestClass]
public class RedisStackExchangeCacheTests
{
    [TestMethod]
    public void Set_And_Get_Should_Work()
    {
        // 测试代码...
    }

    [TestMethod]
    public void Remove_Should_Work()
    {
        // 测试代码...
    }
}

🤝 参与贡献

欢迎参与项目贡献!请阅读我们的贡献指南了解如何参与项目开发。

📄 开源协议

本项目采用 MIT 开源协议 - 详情请查看 LICENSE 文件。

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
0.0.1-alpha-202502101554 33 2/10/2025
0.0.1-alpha-202502101448 36 2/10/2025
0.0.1-alpha-202502101434 38 2/10/2025
0.0.1-alpha-202501130258 38 1/13/2025
0.0.1-alpha-202412311524 72 12/31/2024
0.0.1-alpha-202412061617 56 12/6/2024
0.0.1-alpha-202412051527 57 12/5/2024
0.0.1-alpha-202412051431 52 12/5/2024
0.0.1-alpha-202412041445 59 12/4/2024
0.0.1-alpha-202412021409 45 12/2/2024
0.0.1-alpha-202411301019 52 11/30/2024
0.0.1-alpha-202411170525 56 11/17/2024
0.0.1-alpha-202411161308 52 11/16/2024
0.0.1-alpha-202411131604 53 11/13/2024
0.0.1-alpha-202411111439 66 11/11/2024
0.0.1-alpha-202411051434 55 11/5/2024
0.0.1-alpha-202410281339 54 10/28/2024
0.0.1-alpha-202410131500 59 10/13/2024
0.0.1-alpha-202407261457 64 7/26/2024
0.0.1-alpha-202407261325 60 7/26/2024
0.0.1-alpha-202406271301 60 6/27/2024
0.0.1-alpha-202406251508 61 6/25/2024
0.0.1-alpha-202406251310 57 6/25/2024
0.0.1-alpha-202406141611 60 6/14/2024
0.0.1-alpha-202406141550 62 6/14/2024
0.0.1-alpha-202406121515 59 6/12/2024
0.0.1-alpha-202406061553 75 6/6/2024
0.0.1-alpha-202406041519 60 6/4/2024
0.0.1-alpha-202406011613 66 6/1/2024
0.0.1-alpha-202406011238 66 6/1/2024
0.0.1-alpha-202405311458 59 5/31/2024
0.0.1-alpha-202405291213 71 5/29/2024
0.0.1-alpha-202405190457 65 5/19/2024
0.0.1-alpha-202405161229 63 5/16/2024
0.0.1-alpha-202405141510 59 5/14/2024
0.0.1-alpha-202405101323 63 5/10/2024
0.0.1-alpha-202405081356 69 5/8/2024
0.0.1-alpha-202405021337 29 5/2/2024
0.0.1-alpha-202405021336 28 5/2/2024
0.0.1-alpha-202405020452 47 5/2/2024
0.0.1-alpha-202405011443 49 5/1/2024
0.0.1-alpha-202404291541 65 4/29/2024
0.0.1-alpha-202404281218 66 4/28/2024