RedisNet, dotnet 코어용 프로덕션 준비 Redis 클라이언트
15213 단어 dotnetcsharpwebdevproductivity
dotnet add package RedisNet
public void ConfigureServices(IServiceCollection services)
{
var redis2ConfigurationOptions = ConfigurationOptions.Parse("localhost:6379");
redis2ConfigurationOptions.ReconnectRetryPolicy = new ExponentialRetry(1000);
services.AddRedisDotNet(new RedisCacheOptions()
{
ConfigurationOptions = redis2ConfigurationOptions,
CompressionOption = new CompressionOption()
{
TriggerByteSize = 100 * 1024
}
});
}
Redis 키의 값 크기가 100킬로바이트를 초과하면 gzip 압축 알고리즘을 적용하여 크기를 줄이고 검색 시 자동으로 압축을 해제하여 전체 프로세스가 클라이언트의 관점에서 투명하도록 합니다. 압축은 네트워크를 통한 데이터 패킷의 크기를 줄이므로 redis 호출의 성능이 50% 증가합니다.
압축 벤치마크 결과
BenchmarkDotNet=v0.12.1, OS=macOS 11.3 (20E232) [Darwin 20.4.0]
Intel Core i7-9750H CPU 2.60GHz, 1 CPU, 12 logical and 6 physical cores
.NET Core SDK=5.0.201
[Host] : .NET Core 3.1.13 (CoreCLR 4.700.21.11102, CoreFX 4.700.21.11602), X64 RyuJIT DEBUG
방법
평균
오류
표준 개발
0세대
1세대
2세대
할당됨
WithCompressionBenchmark
668.2ms
13.34ms
27.24ms
-
-
-
4.88MB
비압축벤치마크
1,387.1ms
26.92ms
37.74ms
-
-
-
2.39MB
사용 방법 1
단일 redis 서버
전제 조건
docker run --name redis1 -d -p 6379:6379 redis redis-server --appendonly yes
public void ConfigureServices(IServiceCollection services)
{
var redis2ConfigurationOptions = ConfigurationOptions.Parse("localhost:6379");
redis2ConfigurationOptions.ReconnectRetryPolicy = new ExponentialRetry(1000);
services.AddRedisDotNet(new RedisCacheOptions()
{
ConfigurationOptions = redis2ConfigurationOptions
});
}
public class TestController : Controller
{
private readonly IRedisService _redisService;
public TestController(IRedisService redisService){
_redisService = redisService;
}
public IActionResult Get(string key){
var result = _redisService.GetString(key);
return OK(result);
}
public async Task<IActionResult> GetAsync(string key){
var result = await _redisService.GetStringAsync(key);
return OK(result);
}
}
사용 방법 2
여러 Redis 서버
전제 조건
docker run --name redis1 -d -p 6379:6379 redis redis-server --appendonly yes
docker run --name redis2 -d -p 6380:6380 redis redis-server --appendonly yes
public void ConfigureServices(IServiceCollection services)
{
var redis1ConfigurationOptions = ConfigurationOptions.Parse("localhost:6379");
redis1ConfigurationOptions.ReconnectRetryPolicy = new ExponentialRetry(1000);
services.AddRedisDotNet<Redis1>(new RedisCacheOptions()
{
ConfigurationOptions = redis1ConfigurationOptions
});
var redis2ConfigurationOptions = ConfigurationOptions.Parse("localhost:6380");
redis2ConfigurationOptions.ReconnectRetryPolicy = new ExponentialRetry(1000);
services.AddRedisDotNet<Redis2>(new RedisCacheOptions()
{
ConfigurationOptions = redis2ConfigurationOptions
});
}
public class Redis1 : RedisService<Redis1>
{
public Redis1(IOptionsMonitor<RedisCacheOptions> cacheOptions) : base(cacheOptions)
{
}
}
public class Redis2 : RedisService<Redis2>
{
public Redis2(IOptionsMonitor<RedisCacheOptions> cacheOptions) : base(cacheOptions)
{
}
}
public class Redis1Controller : ControllerBase
{
private readonly Redis1 _redis1;
public Redis1Controller(Redis1 redis1)
{
_redis1 = redis1;
}
[HttpGet]
public async Task<IActionResult> Get()
{
return Ok(await _redis1.GetStringAsync("redis1"));
}
}
public class Redis2Controller : ControllerBase
{
private readonly Redis2 _redis2;
public Redis2Controller(Redis2 redis2)
{
_redis2 = redis2;
}
[HttpGet]
public async Task<IActionResult> Get()
{
return Ok(await _redis2.GetStringAsync("redis2"));
}
}
BenchmarkDotNet=v0.12.1, OS=macOS 11.3 (20E232) [Darwin 20.4.0]
Intel Core i7-9750H CPU 2.60GHz, 1 CPU, 12 logical and 6 physical cores
.NET Core SDK=5.0.201
[Host] : .NET Core 3.1.13 (CoreCLR 4.700.21.11102, CoreFX 4.700.21.11602), X64 RyuJIT DEBUG
단일 redis 서버
전제 조건
docker run --name redis1 -d -p 6379:6379 redis redis-server --appendonly yes
public void ConfigureServices(IServiceCollection services)
{
var redis2ConfigurationOptions = ConfigurationOptions.Parse("localhost:6379");
redis2ConfigurationOptions.ReconnectRetryPolicy = new ExponentialRetry(1000);
services.AddRedisDotNet(new RedisCacheOptions()
{
ConfigurationOptions = redis2ConfigurationOptions
});
}
public class TestController : Controller
{
private readonly IRedisService _redisService;
public TestController(IRedisService redisService){
_redisService = redisService;
}
public IActionResult Get(string key){
var result = _redisService.GetString(key);
return OK(result);
}
public async Task<IActionResult> GetAsync(string key){
var result = await _redisService.GetStringAsync(key);
return OK(result);
}
}
사용 방법 2
여러 Redis 서버
전제 조건
docker run --name redis1 -d -p 6379:6379 redis redis-server --appendonly yes
docker run --name redis2 -d -p 6380:6380 redis redis-server --appendonly yes
public void ConfigureServices(IServiceCollection services)
{
var redis1ConfigurationOptions = ConfigurationOptions.Parse("localhost:6379");
redis1ConfigurationOptions.ReconnectRetryPolicy = new ExponentialRetry(1000);
services.AddRedisDotNet<Redis1>(new RedisCacheOptions()
{
ConfigurationOptions = redis1ConfigurationOptions
});
var redis2ConfigurationOptions = ConfigurationOptions.Parse("localhost:6380");
redis2ConfigurationOptions.ReconnectRetryPolicy = new ExponentialRetry(1000);
services.AddRedisDotNet<Redis2>(new RedisCacheOptions()
{
ConfigurationOptions = redis2ConfigurationOptions
});
}
public class Redis1 : RedisService<Redis1>
{
public Redis1(IOptionsMonitor<RedisCacheOptions> cacheOptions) : base(cacheOptions)
{
}
}
public class Redis2 : RedisService<Redis2>
{
public Redis2(IOptionsMonitor<RedisCacheOptions> cacheOptions) : base(cacheOptions)
{
}
}
public class Redis1Controller : ControllerBase
{
private readonly Redis1 _redis1;
public Redis1Controller(Redis1 redis1)
{
_redis1 = redis1;
}
[HttpGet]
public async Task<IActionResult> Get()
{
return Ok(await _redis1.GetStringAsync("redis1"));
}
}
public class Redis2Controller : ControllerBase
{
private readonly Redis2 _redis2;
public Redis2Controller(Redis2 redis2)
{
_redis2 = redis2;
}
[HttpGet]
public async Task<IActionResult> Get()
{
return Ok(await _redis2.GetStringAsync("redis2"));
}
}
docker run --name redis1 -d -p 6379:6379 redis redis-server --appendonly yes
docker run --name redis2 -d -p 6380:6380 redis redis-server --appendonly yes
public void ConfigureServices(IServiceCollection services)
{
var redis1ConfigurationOptions = ConfigurationOptions.Parse("localhost:6379");
redis1ConfigurationOptions.ReconnectRetryPolicy = new ExponentialRetry(1000);
services.AddRedisDotNet<Redis1>(new RedisCacheOptions()
{
ConfigurationOptions = redis1ConfigurationOptions
});
var redis2ConfigurationOptions = ConfigurationOptions.Parse("localhost:6380");
redis2ConfigurationOptions.ReconnectRetryPolicy = new ExponentialRetry(1000);
services.AddRedisDotNet<Redis2>(new RedisCacheOptions()
{
ConfigurationOptions = redis2ConfigurationOptions
});
}
public class Redis1 : RedisService<Redis1>
{
public Redis1(IOptionsMonitor<RedisCacheOptions> cacheOptions) : base(cacheOptions)
{
}
}
public class Redis2 : RedisService<Redis2>
{
public Redis2(IOptionsMonitor<RedisCacheOptions> cacheOptions) : base(cacheOptions)
{
}
}
public class Redis1Controller : ControllerBase
{
private readonly Redis1 _redis1;
public Redis1Controller(Redis1 redis1)
{
_redis1 = redis1;
}
[HttpGet]
public async Task<IActionResult> Get()
{
return Ok(await _redis1.GetStringAsync("redis1"));
}
}
public class Redis2Controller : ControllerBase
{
private readonly Redis2 _redis2;
public Redis2Controller(Redis2 redis2)
{
_redis2 = redis2;
}
[HttpGet]
public async Task<IActionResult> Get()
{
return Ok(await _redis2.GetStringAsync("redis2"));
}
}
dotnet add package RedisNet --version 1.1.0
즐거운 코딩하세요 :)
Reference
이 문제에 관하여(RedisNet, dotnet 코어용 프로덕션 준비 Redis 클라이언트), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/alialp/redisnet-production-ready-redis-client-for-dotnet-core-2a1p텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)