Rabbit.Common.Redis 1.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Rabbit.Common.Redis --version 1.1.0
                    
NuGet\Install-Package Rabbit.Common.Redis -Version 1.1.0
                    
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.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Rabbit.Common.Redis" Version="1.1.0" />
                    
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.1.0
                    
#r "nuget: Rabbit.Common.Redis, 1.1.0"
                    
#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.1.0
                    
#: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.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Rabbit.Common.Redis&version=1.1.0
                    
Install as a Cake Tool

Rabbit.Common.Redis

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 自动同步

快速开始

1. 配置 Redis

using Rabbit.Common.Redis;

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

// 多实例(使用 [FromKeyedServices] 注入)
services.AddFreeRedisMapConfigure(
    "SessionRedis",
    "127.0.0.1:6379,password=123456,defaultDatabase=2"
);
services.AddFreeRedisMapConfigure(
    "OrderRedis",
    "127.0.0.1:6379,password=123456,defaultDatabase=3"
);

2. 使用缓存服务(单实例)

注意:ICacheService 仅支持单实例场景,提供内存 + Redis 二级缓存。

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}");
    }
}

3. 直接使用 RedisClient

多实例场景请直接使用 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;
    }

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

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.4.1 139 8/11/2026
1.4.0 137 7/16/2026
1.3.1 159 5/28/2026
1.3.0 142 5/19/2026
1.2.1 137 4/7/2026
1.2.0 158 3/27/2026
1.1.0 150 3/12/2026
1.0.0 140 3/12/2026