From 9c0aad605c626c1754277b46e9e710639e326c58 Mon Sep 17 00:00:00 2001 From: "m.zargarov" Date: Sun, 1 Dec 2024 19:33:52 +0400 Subject: [PATCH] add Redis service --- Cloud/Program.cs | 3 ++ Cloud/Services/Cache/IRedisCacheService.cs | 13 ++++++++ Cloud/Services/Cache/RedisCacheService.cs | 37 ++++++++++++++++++++++ docker-compose.yml | 9 +----- 4 files changed, 54 insertions(+), 8 deletions(-) create mode 100644 Cloud/Services/Cache/IRedisCacheService.cs create mode 100644 Cloud/Services/Cache/RedisCacheService.cs diff --git a/Cloud/Program.cs b/Cloud/Program.cs index 1c3e3a8..0a1ad0f 100644 --- a/Cloud/Program.cs +++ b/Cloud/Program.cs @@ -8,6 +8,7 @@ using FluentValidation.AspNetCore; using Cloud.Validation; using StackExchange.Redis; using Cloud.Services; +using Cloud.Services.Cache; var builder = WebApplication.CreateBuilder(args); @@ -20,6 +21,8 @@ builder.Services.AddSingleton(sp => return ConnectionMultiplexer.Connect(configuration); }); +builder.Services.AddSingleton(); + //Kafka producer service builder.Services.AddSingleton(); diff --git a/Cloud/Services/Cache/IRedisCacheService.cs b/Cloud/Services/Cache/IRedisCacheService.cs new file mode 100644 index 0000000..a5b11f6 --- /dev/null +++ b/Cloud/Services/Cache/IRedisCacheService.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Cloud.Services.Cache +{ + public interface IRedisCacheService + { + Task SetCacheAsync(string key, T value, TimeSpan? expiry = null); + Task GetCacheAsync(string key); + } +} \ No newline at end of file diff --git a/Cloud/Services/Cache/RedisCacheService.cs b/Cloud/Services/Cache/RedisCacheService.cs new file mode 100644 index 0000000..2fadf24 --- /dev/null +++ b/Cloud/Services/Cache/RedisCacheService.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using StackExchange.Redis; +using System.Text.Json; + +namespace Cloud.Services.Cache +{ + public class RedisCacheService : IRedisCacheService + { + private readonly IConnectionMultiplexer _connectionMultiplexer; + + public RedisCacheService(IConnectionMultiplexer connectionMultiplexer) + { + _connectionMultiplexer = connectionMultiplexer; + } + + public async Task SetCacheAsync(string key, T value, TimeSpan? expiry = null) + { + var database = _connectionMultiplexer.GetDatabase(); + var serializedValue = JsonSerializer.Serialize(value); + await database.StringSetAsync(key, serializedValue, expiry); + } + + public async Task GetCacheAsync(string key) + { + var database = _connectionMultiplexer.GetDatabase(); + var value = await database.StringGetAsync(key); + + if (value.IsNullOrEmpty) + return default; + + return JsonSerializer.Deserialize(value); + } + } +} \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index af1ab66..ede4fce 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -108,11 +108,4 @@ volumes: postgres_data: driver: local cloud-redis: - driver: local - zookeeper: - image: confluentinc/cp-zookeeper:7.4.0 - environment: - ZOOKEEPER_CLIENT_PORT: 2181 - ZOOKEEPER_TICK_TIME: 2000 - ports: - - 2181:2181 \ No newline at end of file + driver: local \ No newline at end of file