修改试用代码

This commit is contained in:
hello 2024-07-11 16:39:17 +08:00
parent d5772eb0ae
commit 9f0137d2b2

View File

@ -52,7 +52,7 @@ public class MyService(IDistributedCache cache) : IService
Redis 是一个高性能、开源的分布式缓存系统它是一个内存数据库支持多种数据结构如字符串、列表、哈希表、集合等Redis 的设计目标是提供一个高性能、低延迟的分布式缓存系统以支持大规模的数据存储和访问同时提供高可用性和可扩展性。Redis 采用 RESP 协议,使用 C 语言编写,支持多种语言,如 C、C++、C#、Java、Python、Node.js 等,可以在 .NET 中使用 StackExchange.Redis 开源库对缓存进行操作。 Redis 是一个高性能、开源的分布式缓存系统它是一个内存数据库支持多种数据结构如字符串、列表、哈希表、集合等Redis 的设计目标是提供一个高性能、低延迟的分布式缓存系统以支持大规模的数据存储和访问同时提供高可用性和可扩展性。Redis 采用 RESP 协议,使用 C 语言编写,支持多种语言,如 C、C++、C#、Java、Python、Node.js 等,可以在 .NET 中使用 StackExchange.Redis 开源库对缓存进行操作。
Windows 下 Redis 不再维护,可以使用 WSL2 安装 Redis或者使用 Docker 安装 Redis。 Windows 下 Redis 不再维护,可以使用 WSL2 安装 Redis或者使用 Docker 安装 Redis。也可使用 Memurai 替代 RedisMemurai 是一个 Windows 下的 Redis 服务,支持 Redis 的大部分功能,可以在 Windows 下运行 Redis 服务。
### 容器化 Redis 服务 ### 容器化 Redis 服务
@ -148,22 +148,7 @@ catch (Exception ex)
} }
``` ```
```shell ### 在 Docker 容器中运行
dotnet add package Microsoft.Garnet.Client
```
```csharp
using Garnet.Client;
var client = new GarnetClient("localhost", 6379, "Password");
client.Set("key", "value");
var value = client.Get("key");
```
### 在 Docker 中运行
```shell ```shell
docker run --name garnet -d -p 6379:6379 --ulimit memlock=-1 ghcr.io/microsoft/garnet --auth Password --password guest docker run --name garnet -d -p 6379:6379 --ulimit memlock=-1 ghcr.io/microsoft/garnet --auth Password --password guest
@ -172,60 +157,66 @@ docker run --name garnet -d -p 6379:6379 --ulimit memlock=-1 ghcr.io/microsoft/g
### 使用 Window Service 运行 ### 使用 Window Service 运行
```shell ```shell
dotnet add package Microsoft.Garnet
dotnet add package Microsoft.Extensions.Hosting.WindowsServices dotnet add package Microsoft.Extensions.Hosting.WindowsServices
``` ```
```csharp ```csharp
using Garnet;
public class Program public class GarnetService(ILogger<GarnetService> logger) : BackgroundService
{ {
public static void Main(string[] args) protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{ {
CreateHostBuilder(args).Build().Run(); if (!stoppingToken.IsCancellationRequested)
} {
string[] commandLineArgs = Environment.GetCommandLineArgs();
public static IHostBuilder CreateHostBuilder(string[] args) => using var server = new GarnetServer(commandLineArgs);
Host.CreateDefaultBuilder(args)
.UseWindowsService() logger.LogInformation("Starting Garnet server...");
.ConfigureServices((hostContext, services) =>
try
{ {
services.AddHostedService<GarnetService>(); server.Start();
}); }
catch (Exception ex)
{
logger.LogError(ex, "An error occurred while starting the Garnet server.");
}
logger.LogInformation("Garnet server started.");
await Task.Delay(Timeout.Infinite, stoppingToken);
}
}
} }
``` ```
```csharp ```csharp
var builder = Host.CreateApplicationBuilder(args);
using System; builder.Services.AddWindowsService(options =>
using System.Threading;
using System.Threading.Tasks;
using Garnet;
using Microsoft.Extensions.Hosting;
public class GarnetService : IHostedService
{ {
private GarnetServer _server; options.ServiceName = "Garnet Service";
});
public Task StartAsync(CancellationToken cancellationToken) builder.Services.AddHostedService<GarnetService>();
{
_server = new GarnetServer();
_server.Start();
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken) var host = builder.Build();
{
_server.Stop(); host.Run();
return Task.CompletedTask;
}
}
``` ```
```shell ```shell
sc create GarnetService binPath= "C:\path\to\GarnetService.exe" sc.exe create GarnetService binpath= "C:\Users\GarnetWindowsService.exe" start= auto
sc start GarnetService sc.exe start GarnetService
sc.exe stop GarnetService
sc.exe delete GarnetService
``` ```
@ -299,4 +290,10 @@ IDistributedCache 过期时间分为:绝对过期时间和滑动过期时间
## 分布式缓存监控工具 ## 分布式缓存监控工具
可用 RedisInsight、Garnet Dashboard、Grafana 等工具监控分布式缓存系统。 可用 RedisInsight 工具监控 Redis 分布式缓存系统。
https://redis.io/docs/latest/operate/redisinsight/install
```shell
docker run -d --name redisinsight -p 5540:5540 redis/redisinsight:latest
```