Yoko.Tool.Garnet
0.1.2
dotnet add package Yoko.Tool.Garnet --version 0.1.2
NuGet\Install-Package Yoko.Tool.Garnet -Version 0.1.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="Yoko.Tool.Garnet" Version="0.1.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Yoko.Tool.Garnet" Version="0.1.2" />
<PackageReference Include="Yoko.Tool.Garnet" />
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 Yoko.Tool.Garnet --version 0.1.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Yoko.Tool.Garnet, 0.1.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 Yoko.Tool.Garnet@0.1.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=Yoko.Tool.Garnet&version=0.1.2
#tool nuget:?package=Yoko.Tool.Garnet&version=0.1.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Garnet 集成扩展
提供了一个扩展方法,用于简化在 ASP.NET Core 应用中集成 Garnet 服务和 StackExchange.Redis。
功能
- 启动 Garnet 服务,并支持可选的参数配置。
- 注册
StackExchange.Redis为单例服务,用来处理 Garnet 操作。 - 提供
GarnetCacheHelper辅助类,封装常见的 Garnet 操作。
注意:Garnet 与Redis操作类基本上通用
使用方法
步骤 1:配置服务
在 Program.cs 中,调用 AddGarnetService 方法,配置 Garnet 和 Redis:
var builder = WebApplication.CreateBuilder(args);
// 启动 Garnet 服务
// 【注意garnet连接字符串、端口等配置信息】
builder.Services.AddGarnetService();
// 【高级用法】可选参数配置
builder.Services.AddGarnetService(
host: "127.0.0.1", // (可选) Garnet主机服务地址,默认为 "127.0.0.1"
port: 2380, // (可选)Garnet 服务端口,默认值为 2379
aofCommitFreq: 2000, // (可选)AOF 提交频率(毫秒),默认值为 1000
logDir: "./customlog", // (可选)Garnet 日志目录,默认值为 "./cachedata"
garnetConnectionString: "127.0.0.1:2379,password=,connectTimeout=3000,connectRetry=1,syncTimeout=10000,DefaultDatabase=0" // (可选)Garnet 连接字符串
);
var app = builder.Build();
步骤 2:使用
在服务或控制器中使用 GarnetCacheHelper 操作 Redis 数据:
public class SampleController : ControllerBase
{
private readonly GarnetCacheHelper _garnetHelper;
public SampleController(GarnetCacheHelper garnetHelper)
{
_garnetHelper = garnetHelper;
}
[HttpPost("set")]
public IActionResult SetCache(string key, string value)
{
_garnetHelper.Set(key, value);
return Ok("缓存设置成功");
}
[HttpGet("get")]
public IActionResult GetCache(string key)
{
var value = _garnetHelper.Get(key);
return Ok(value);
}
}
默认配置
如果未传入参数,将使用以下默认值:
| 参数 | 默认值 | 描述 |
|---|---|---|
host |
127.0.0.1 |
Garnet 服务运行主机地址 |
port |
2379 |
Garnet 服务运行端口 |
aofCommitFreq |
1000 |
AOF 提交频率(毫秒) |
logDir |
./cachedata |
Garnet 日志目录 |
garnetConnectionString |
127.0.0.1:2379,password=,connectTimeout=3000,connectRetry=1,syncTimeout=10000,DefaultDatabase=0 |
Garnet 连接字符串 |
GarnetCacheHelper 方法
| 方法 | 描述 |
|---|---|
Set(key, value, expiry) |
设置字符串值,并支持设置过期时间。 |
Get(key) |
获取 Redis 中的字符串值。 |
Delete(key) |
删除指定键值。 |
KeyExists(key) |
检查键是否存在。 |
Increment(key, value) |
增加指定键的值。 |
SetObject<T>(key, value, expiry) |
序列化对象并存储到 Redis。 |
GetObject<T>(key) |
从 Redis 获取对象并反序列化。 |
PushToArray(key, value) |
向 Redis 列表添加元素。 |
GetArray(key) |
获取 Redis 列表的所有元素。 |
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. 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.
-
net8.0
- Microsoft.Garnet (>= 1.0.59)
- StackExchange.Redis (>= 2.8.31)
-
net9.0
- Microsoft.Garnet (>= 1.0.59)
- StackExchange.Redis (>= 2.8.31)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.