T2FGame.Cache.Redis
1.0.2
There is a newer version of this package available.
See the version list below for details.
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" />
<PackageReference Include="T2FGame.Cache.Redis" />
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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#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
#tool nuget:?package=T2FGame.Cache.Redis&version=1.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
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 | Versions 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.
-
net9.0
- Microsoft.Extensions.Caching.StackExchangeRedis (>= 9.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.0)
- Microsoft.Extensions.Options (>= 9.0.0)
- StackExchange.Redis (>= 2.8.16)
- T2FGame.Action (>= 1.0.2)
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.