Rabbit.Common.Redis 1.3.1

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

Rabbit.Common.Redis

Two-level caching component (Memory L1 + Redis L2 via FreeRedis) with multi-instance support via .NET Keyed Services. Inject ICacheService for cache-aside pattern or IRedisClient for raw access.

Redis 缓存组件,支持二级缓存(内存 + Redis)。

安装

dotnet add package Rabbit.Common.Redis

依赖

  • Rabbit.Common.Lite
  • FreeRedis 1.5.5
  • Microsoft.Extensions.Caching.Memory 10.0.1

功能

  • 二级缓存: 内存缓存 + Redis 分布式缓存
  • 连接管理: 支持多 Redis 实例
  • 自动同步: 内存缓存与 Redis 自动同步

使用方式

Console 项目

using Microsoft.Extensions.DependencyInjection;
using Rabbit.Common.Redis;
using FreeRedis;

// 1. 创建 DI 容器
var services = new ServiceCollection();

// 2. 注册 Redis
services.AddFreeRedisConfigure(
    "127.0.0.1:6379,password=123456,defaultDatabase=1"
);

// 3. 构建服务提供者
var provider = services.BuildServiceProvider();

// 4. 使用缓存服务(方式一:封装服务)
var cache = provider.GetRequiredService<ICacheService>();
var user = cache.StringGetOrAdd(
    $"user:{id}",
    () => GetUserFromDb(id),
    TimeSpan.FromMinutes(5)
);

// 使用 Redis 客户端(方式二:原始客户端)
var redis = provider.GetRequiredService<IRedisClient>();
redis.Set("key", "value", 3600);

Web API 项目

using Rabbit.Common.Redis;

// 单实例
services.AddFreeRedisConfigure(
    "127.0.0.1:6379,password=123456,defaultDatabase=1"
);

// 多实例
services.AddFreeRedisMapConfigure(
    "SessionRedis",
    "127.0.0.1:6379,password=123456,defaultDatabase=2"
);

多实例配置

Console 项目

// 注册多个实例
services.AddFreeRedisMapConfigure("Cache1", "127.0.0.1:6379");
services.AddFreeRedisMapConfigure("Cache2", "127.0.0.2:6379");

var provider = services.BuildServiceProvider();

// 获取指定实例
var cache1 = provider.GetRequiredKeyedService<IRedisClient>("Cache1");
var cache2 = provider.GetRequiredKeyedService<IRedisClient>("Cache2");

Web API 项目

services.AddFreeRedisMapConfigure(
    "SessionRedis",
    "127.0.0.1:6379,password=123456,defaultDatabase=2"
);

services.AddFreeRedisMapConfigure(
    "OrderRedis",
    "127.0.0.1:6379,password=123456,defaultDatabase=3"
);

使用示例

ICacheService(单实例,二级缓存)

public class UserService
{
    private readonly ICacheService _cache;

    public UserService(ICacheService cache)
    {
        _cache = cache;
    }

    public User GetUser(long id)
    {
        return _cache.StringGetOrAdd(
            $"user:{id}",
            () => _db.GetUser(id),
            TimeSpan.FromMinutes(5)
        );
    }

    public void SetUser(User user)
    {
        _cache.StringSet($"user:{user.Id}", user, TimeSpan.FromMinutes(5));
    }

    public void RemoveUser(long id)
    {
        _cache.Remove($"user:{id}");
    }
}

IRedisClient(支持多实例)

// 单实例
public class OrderService
{
    private readonly IRedisClient _redis;

    public OrderService(IRedisClient redis)
    {
        _redis = redis;
    }

    public void SetOrder(Order order)
    {
        _redis.Set("order:1", order.JsonSerialize(), 3600);
    }
}

// 多实例
using Microsoft.Extensions.DependencyInjection;

public class OrderService
{
    private readonly IRedisClient _session;
    private readonly IRedisClient _order;

    public OrderService(
        [FromKeyedServices("SessionRedis")] IRedisClient session,
        [FromKeyedServices("OrderRedis")] IRedisClient order)
    {
        _session = session;
        _order = order;
    }
}

API 参考

ICacheService

方法 说明
StringGet 获取字符串
StringGet<T> 获取对象
StringSet<T> 设置缓存
StringGetOrAdd<T> 获取或添加
Remove 删除缓存
Exists 检查是否存在

连接字符串格式

127.0.0.1:6379,password=123456,defaultDatabase=1

参数说明:

  • password: Redis 密码
  • defaultDatabase: 默认数据库(0-15)
  • ssl: 是否使用 SSL
  • connectTimeout: 连接超时(毫秒)

许可证

MIT

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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 (2)

Showing the top 2 NuGet packages that depend on Rabbit.Common.Redis:

Package Downloads
Rabbit.Common.Full

Meta-package referencing all Rabbit.Common utility packages. / Rabbit.Common 全家桶,引用所有子包

Rabbit.Common.RateLimit

Rate limiting with token bucket and sliding window algorithms, memory or Redis storage. / 限流组件,支持令牌桶和滑动窗口算法,可用于分布式限流

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.3.1 118 5/28/2026
1.3.0 116 5/19/2026
1.2.1 118 4/7/2026
1.2.0 136 3/27/2026
1.1.0 129 3/12/2026
1.0.0 122 3/12/2026