greenhouseCRUD #7

Merged
mfnefd merged 18 commits from greenhouseCRUD into dev 2024-12-15 20:52:57 +04:00
4 changed files with 54 additions and 8 deletions
Showing only changes of commit 1ddc8d9556 - Show all commits

View File

@ -12,18 +12,21 @@ using Cloud.Services.Broker;
using Cloud.Services; using Cloud.Services;
using Cloud.Services.Domain.Implement; using Cloud.Services.Domain.Implement;
using Cloud.Services.Domain; using Cloud.Services.Domain;
using Cloud.Services.Cache;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
// Add services to the container. // Add services to the container.
builder.Services.AddTransient<IBrokerService, KafkaService>(); builder.Services.AddTransient<IBrokerService, KafkaService>();
builder.Services.AddTransient<IGreenhouseService, GreenhouseService>(); builder.Services.AddTransient<IGreenhouseService, GreenhouseService>();
//Redis configuration //Redis configuration
builder.Services.AddSingleton<IConnectionMultiplexer>(sp => builder.Services.AddSingleton<IConnectionMultiplexer>(sp =>
{ {
var configuration = ConfigurationOptions.Parse("localhost:6379"); var configuration = ConfigurationOptions.Parse("localhost:6379");
return ConnectionMultiplexer.Connect(configuration); return ConnectionMultiplexer.Connect(configuration);
}); });
builder.Services.AddSingleton<IRedisCacheService, RedisCacheService>();
//Jwt configuration //Jwt configuration
var jwtIssuer = builder.Configuration.GetSection("Jwt:Issuer").Get<string>(); var jwtIssuer = builder.Configuration.GetSection("Jwt:Issuer").Get<string>();

View File

@ -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<T>(string key, T value, TimeSpan? expiry = null);
Task<T?> GetCacheAsync<T>(string key);
}
}

View File

@ -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<T>(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<T?> GetCacheAsync<T>(string key)
{
var database = _connectionMultiplexer.GetDatabase();
var value = await database.StringGetAsync(key);
if (value.IsNullOrEmpty)
return default;
return JsonSerializer.Deserialize<T>(value);
}
}
}

View File

@ -109,10 +109,3 @@ volumes:
driver: local driver: local
cloud-redis: cloud-redis:
driver: local driver: local
zookeeper:
image: confluentinc/cp-zookeeper:7.4.0
environment:
ZOOKEEPER_CLIENT_PORT: 2181
ZOOKEEPER_TICK_TIME: 2000
ports:
- 2181:2181