T2FGame.Cache.Redis 1.0.2

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

T2FGame.Cache.Redis

T2FGame 框架的 Redis 分布式缓存模块,提供高性能的分布式缓存和限流功能。

功能特性

  • 分布式 Action 缓存:基于 Redis 的 IActionCache 实现,替代内存缓存
  • 分布式限流器:基于 Redis 的 IRateLimiter 实现,支持滑动窗口和令牌桶算法
  • 一站式配置:简化 Redis 服务注册
  • 连接复用:共享 Redis 连接,减少资源消耗

安装

<PackageReference Include="T2FGame.Cache.Redis" />

快速开始

添加 Redis 缓存

services.AddActionPipeline(options =>
{
    options.UseCacheHandler = true;
});

// 使用 Redis 替代内存缓存
services.AddRedisActionCache(options =>
{
    options.ConnectionString = "localhost:6379";
    options.Database = 0;
    options.KeyPrefix = "t2f:cache";
});

添加 Redis 限流器

services.AddActionPipeline(options =>
{
    options.UseRateLimitHandler = true;
});

// 使用 Redis 替代内存限流器
services.AddRedisRateLimiter(options =>
{
    options.Database = 1;
    options.KeyPrefix = "t2f:ratelimit";
    options.Algorithm = RateLimitAlgorithm.SlidingWindow;
});

一站式配置

services.AddRedisServices(
    connectionString: "localhost:6379",
    configureCache: cache =>
    {
        cache.KeyPrefix = "game:cache";
    },
    configureRateLimiter: limiter =>
    {
        limiter.Algorithm = RateLimitAlgorithm.TokenBucket;
    });

配置选项

RedisActionCacheOptions

属性 类型 默认值 说明
ConnectionString string localhost:6379 Redis 连接字符串
Database int 0 数据库索引 (0-15)
KeyPrefix string t2f:cache 缓存键前缀
ConnectTimeoutMs int 5000 连接超时时间(毫秒)
SyncTimeoutMs int 1000 同步操作超时时间(毫秒)
UseSsl bool false 是否启用 SSL
Password string? null 密码

RedisRateLimiterOptions

属性 类型 默认值 说明
Database int 0 数据库索引
KeyPrefix string t2f:ratelimit 限流键前缀
Algorithm RateLimitAlgorithm SlidingWindow 限流算法

限流算法

滑动窗口(SlidingWindow)

适用于需要精确控制请求频率的场景:

services.AddRedisRateLimiter(options =>
{
    options.Algorithm = RateLimitAlgorithm.SlidingWindow;
});

令牌桶(TokenBucket)

适用于需要允许突发流量的场景:

services.AddRedisRateLimiter(options =>
{
    options.Algorithm = RateLimitAlgorithm.TokenBucket;
});

使用现有连接

如果应用程序已有 Redis 连接,可以复用:

var connection = ConnectionMultiplexer.Connect("localhost:6379");

services.AddRedisConnection(connection);
services.AddRedisActionCache();
services.AddRedisRateLimiter();

与 Action 缓存配合使用

[ActionController(cmd: 1)]
public class ItemController
{
    [ActionMethod(subCmd: 1)]
    [ActionCache(duration: 60, Scope = CacheScope.Global)]
    public async Task<ItemListResponse> GetItemList()
    {
        // 此响应将被缓存 60 秒
        return await _itemService.GetAllItemsAsync();
    }

    [ActionMethod(subCmd: 2)]
    [ActionCache(duration: 30, Scope = CacheScope.User)]
    public async Task<InventoryResponse> GetInventory(FlowContext context)
    {
        // 每个用户独立缓存 30 秒
        return await _inventoryService.GetByUserIdAsync(context.UserId);
    }
}

生产环境建议

Redis 集群配置

services.AddRedisActionCache(options =>
{
    options.ConnectionString = "redis-cluster-1:6379,redis-cluster-2:6379,redis-cluster-3:6379";
    options.Password = Environment.GetEnvironmentVariable("REDIS_PASSWORD");
    options.UseSsl = true;
});

分离缓存和限流数据库

services.AddRedisActionCache(options =>
{
    options.Database = 0;  // 缓存使用 DB 0
});

services.AddRedisRateLimiter(options =>
{
    options.Database = 1;  // 限流使用 DB 1
});

目录结构

T2FGame.Cache.Redis/
├── RedisActionCache.cs      # Redis 缓存实现
├── RedisRateLimiter.cs      # Redis 限流器实现
└── DependencyInjection.cs   # 服务注册扩展
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 (1)

Showing the top 1 NuGet packages that depend on T2FGame.Cache.Redis:

Package Downloads
T2FGame

T2FGame Framework - A high-performance distributed game server framework inspired by ioGame. This meta-package includes all T2FGame components.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.9 425 12/11/2025
1.0.8 423 12/11/2025
1.0.7 429 12/11/2025
1.0.6 430 12/11/2025
1.0.5 446 12/10/2025
1.0.4 444 12/10/2025
1.0.3 451 12/9/2025
1.0.2 353 12/8/2025
1.0.1 492 12/1/2025
0.0.1 360 12/8/2025