Skip to content

Commit

Permalink
redis database
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikus1993 committed Feb 13, 2024
1 parent 8694956 commit 1de6bc6
Showing 1 changed file with 5 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@ namespace Basket.Infrastructure.Repositories;
internal sealed class RedisCustomerBasketRepository : ICustomerBasketReader, ICustomerBasketWriter
{
private readonly IConnectionMultiplexer _connectionMultiplexer;
private readonly IDatabase _database;
private readonly IRedisObjectDeserializer _redisObjectDeserializer;
public RedisCustomerBasketRepository(IConnectionMultiplexer connectionMultiplexer, IRedisObjectDeserializer redisObjectDeserializer)
{
_connectionMultiplexer = connectionMultiplexer;
_redisObjectDeserializer = redisObjectDeserializer;
_database = connectionMultiplexer.GetDatabase();
}

public async Task<CustomerBasket?> Find(CustomerId customerId, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
var db = _connectionMultiplexer.GetDatabase();
var result = await db.StringGetAsync(customerId.ToRedisKey());
var result = await _database.StringGetAsync(customerId.ToRedisKey());
if (!_redisObjectDeserializer.Deserialize(result, out var model))
{
return null;
Expand All @@ -41,10 +42,8 @@ public async Task<Result<UpdateBasketSuccess>> Update(CustomerBasket basket, Can
var redisBasket = new RedisCustomerBasket(basket);

var json = _redisObjectDeserializer.Serialize(redisBasket);

var db = _connectionMultiplexer.GetDatabase();

await db.StringSetAsync(basket.CustomerId.ToRedisKey(), json);
await _database.StringSetAsync(basket.CustomerId.ToRedisKey(), json);

return Result.Ok(UpdateBasketSuccess.Instance);
}
Expand All @@ -53,8 +52,7 @@ public async Task<Result<RemoveBasketSuccess>> Remove(CustomerId customerId, Can
{
cancellationToken.ThrowIfCancellationRequested();
var key = customerId.ToRedisKey();
var db = _connectionMultiplexer.GetDatabase();
await db.KeyDeleteAsync(key);
await _database.KeyDeleteAsync(key);

return Result.Ok(RemoveBasketSuccess.Instance);
}
Expand Down

0 comments on commit 1de6bc6

Please sign in to comment.