greenhouseCRUD #7
@ -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>();
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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
|
|
Loading…
Reference in New Issue
Block a user