diff --git a/Cloud/Program.cs b/Cloud/Program.cs index 9cec515..98799f2 100644 --- a/Cloud/Program.cs +++ b/Cloud/Program.cs @@ -12,18 +12,21 @@ using Cloud.Services.Broker; using Cloud.Services; using Cloud.Services.Domain.Implement; using Cloud.Services.Domain; +using Cloud.Services.Cache; var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddTransient(); builder.Services.AddTransient(); + //Redis configuration builder.Services.AddSingleton(sp => { var configuration = ConfigurationOptions.Parse("localhost:6379"); return ConnectionMultiplexer.Connect(configuration); }); +builder.Services.AddSingleton(); //Jwt configuration var jwtIssuer = builder.Configuration.GetSection("Jwt:Issuer").Get(); 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