Tenon.Caching.RedisStackExchange 0.0.1-alpha-202502241449

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

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.  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.

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-202502241449 64 4 months ago
0.0.1-alpha-202502101554 69 5 months ago
0.0.1-alpha-202502101448 72 5 months ago
0.0.1-alpha-202502101434 76 5 months ago
0.0.1-alpha-202501130258 57 6 months ago
0.0.1-alpha-202412311524 89 6 months ago
0.0.1-alpha-202412061617 68 7 months ago
0.0.1-alpha-202412051527 67 7 months ago
0.0.1-alpha-202412051431 62 7 months ago
0.0.1-alpha-202412041445 69 7 months ago
0.0.1-alpha-202412021409 58 7 months ago
0.0.1-alpha-202411301019 63 7 months ago
0.0.1-alpha-202411170525 67 8 months ago
0.0.1-alpha-202411161308 61 8 months ago
0.0.1-alpha-202411131604 61 8 months ago
0.0.1-alpha-202411111439 77 8 months ago
0.0.1-alpha-202411051434 64 8 months ago
0.0.1-alpha-202410281339 66 8 months ago
0.0.1-alpha-202410131500 67 9 months ago
0.0.1-alpha-202407261457 73 7/26/2024
0.0.1-alpha-202407261325 66 7/26/2024
0.0.1-alpha-202406271301 71 6/27/2024
0.0.1-alpha-202406251508 79 6/25/2024
0.0.1-alpha-202406251310 65 6/25/2024
0.0.1-alpha-202406141611 71 6/14/2024
0.0.1-alpha-202406141550 70 6/14/2024
0.0.1-alpha-202406121515 71 6/12/2024
0.0.1-alpha-202406061553 81 6/6/2024
0.0.1-alpha-202406041519 66 6/4/2024
0.0.1-alpha-202406011613 75 6/1/2024
0.0.1-alpha-202406011238 74 6/1/2024
0.0.1-alpha-202405311458 70 5/31/2024
0.0.1-alpha-202405291213 78 5/29/2024
0.0.1-alpha-202405190457 73 5/19/2024
0.0.1-alpha-202405161229 70 5/16/2024
0.0.1-alpha-202405141510 65 5/14/2024
0.0.1-alpha-202405101323 68 5/10/2024
0.0.1-alpha-202405081356 86 5/8/2024
0.0.1-alpha-202405021337 36 5/2/2024
0.0.1-alpha-202405021336 37 5/2/2024
0.0.1-alpha-202405020452 54 5/2/2024
0.0.1-alpha-202405011443 59 5/1/2024
0.0.1-alpha-202404291541 73 4/29/2024
0.0.1-alpha-202404281218 74 4/28/2024