Merge branch 'dev' into greenhouseCRUD
This commit is contained in:
commit
1ddc8d9556
@ -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<IBrokerService, KafkaService>();
|
||||
builder.Services.AddTransient<IGreenhouseService, GreenhouseService>();
|
||||
|
||||
//Redis configuration
|
||||
builder.Services.AddSingleton<IConnectionMultiplexer>(sp =>
|
||||
{
|
||||
var configuration = ConfigurationOptions.Parse("localhost:6379");
|
||||
return ConnectionMultiplexer.Connect(configuration);
|
||||
});
|
||||
builder.Services.AddSingleton<IRedisCacheService, RedisCacheService>();
|
||||
|
||||
//Jwt configuration
|
||||
var jwtIssuer = builder.Configuration.GetSection("Jwt:Issuer").Get<string>();
|
||||
|
13
Cloud/Services/Cache/IRedisCacheService.cs
Normal file
13
Cloud/Services/Cache/IRedisCacheService.cs
Normal 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);
|
||||
}
|
||||
}
|
37
Cloud/Services/Cache/RedisCacheService.cs
Normal file
37
Cloud/Services/Cache/RedisCacheService.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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
|
||||
driver: local
|
Loading…
Reference in New Issue
Block a user