|
|
@ -1,5 +1,4 @@ |
|
|
|
using Microsoft.Extensions.Logging; |
|
|
|
using Microsoft.Extensions.Options; |
|
|
|
using Newtonsoft.Json; |
|
|
|
using StackExchange.Redis; |
|
|
|
using System.Collections.Generic; |
|
|
@ -10,40 +9,33 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Model |
|
|
|
{ |
|
|
|
public class RedisBasketRepository : IBasketRepository |
|
|
|
{ |
|
|
|
private ILogger<RedisBasketRepository> _logger; |
|
|
|
private BasketSettings _settings; |
|
|
|
private readonly ILogger<RedisBasketRepository> _logger; |
|
|
|
|
|
|
|
private ConnectionMultiplexer _redis; |
|
|
|
private readonly ConnectionMultiplexer _redis; |
|
|
|
private readonly IDatabase _database; |
|
|
|
|
|
|
|
public RedisBasketRepository(IOptionsSnapshot<BasketSettings> options, ILoggerFactory loggerFactory) |
|
|
|
public RedisBasketRepository(ILoggerFactory loggerFactory, ConnectionMultiplexer redis) |
|
|
|
{ |
|
|
|
_settings = options.Value; |
|
|
|
_logger = loggerFactory.CreateLogger<RedisBasketRepository>(); |
|
|
|
_redis = redis; |
|
|
|
_database = redis.GetDatabase(); |
|
|
|
} |
|
|
|
|
|
|
|
public async Task<bool> DeleteBasketAsync(string id) |
|
|
|
{ |
|
|
|
var database = await GetDatabase(); |
|
|
|
return await database.KeyDeleteAsync(id.ToString()); |
|
|
|
return await _database.KeyDeleteAsync(id); |
|
|
|
} |
|
|
|
|
|
|
|
public async Task<IEnumerable<string>> GetUsersAsync() |
|
|
|
public IEnumerable<string> GetUsers() |
|
|
|
{ |
|
|
|
var server = await GetServer(); |
|
|
|
|
|
|
|
IEnumerable<RedisKey> data = server.Keys(); |
|
|
|
if (data == null) |
|
|
|
{ |
|
|
|
return null; |
|
|
|
} |
|
|
|
return data.Select(k => k.ToString()); |
|
|
|
var server = GetServer(); |
|
|
|
var data = server.Keys(); |
|
|
|
return data?.Select(k => k.ToString()); |
|
|
|
} |
|
|
|
|
|
|
|
public async Task<CustomerBasket> GetBasketAsync(string customerId) |
|
|
|
{ |
|
|
|
var database = await GetDatabase(); |
|
|
|
|
|
|
|
var data = await database.StringGetAsync(customerId.ToString()); |
|
|
|
var data = await _database.StringGetAsync(customerId); |
|
|
|
if (data.IsNullOrEmpty) |
|
|
|
{ |
|
|
|
return null; |
|
|
@ -54,9 +46,7 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Model |
|
|
|
|
|
|
|
public async Task<CustomerBasket> UpdateBasketAsync(CustomerBasket basket) |
|
|
|
{ |
|
|
|
var database = await GetDatabase(); |
|
|
|
|
|
|
|
var created = await database.StringSetAsync(basket.BuyerId, JsonConvert.SerializeObject(basket)); |
|
|
|
var created = await _database.StringSetAsync(basket.BuyerId, JsonConvert.SerializeObject(basket)); |
|
|
|
if (!created) |
|
|
|
{ |
|
|
|
_logger.LogInformation("Problem occur persisting the item."); |
|
|
@ -68,34 +58,10 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Model |
|
|
|
return await GetBasketAsync(basket.BuyerId); |
|
|
|
} |
|
|
|
|
|
|
|
private async Task<IDatabase> GetDatabase() |
|
|
|
{ |
|
|
|
if (_redis == null) |
|
|
|
{ |
|
|
|
await ConnectToRedisAsync(); |
|
|
|
} |
|
|
|
|
|
|
|
return _redis.GetDatabase(); |
|
|
|
} |
|
|
|
|
|
|
|
private async Task<IServer> GetServer() |
|
|
|
private IServer GetServer() |
|
|
|
{ |
|
|
|
if (_redis == null) |
|
|
|
{ |
|
|
|
await ConnectToRedisAsync(); |
|
|
|
} |
|
|
|
var endpoint = _redis.GetEndPoints(); |
|
|
|
|
|
|
|
return _redis.GetServer(endpoint.First()); |
|
|
|
} |
|
|
|
|
|
|
|
private async Task ConnectToRedisAsync() |
|
|
|
{ |
|
|
|
var configuration = ConfigurationOptions.Parse(_settings.ConnectionString, true); |
|
|
|
configuration.ResolveDns = true; |
|
|
|
|
|
|
|
_logger.LogInformation($"Connecting to database {configuration.SslHost}."); |
|
|
|
_redis = await ConnectionMultiplexer.ConnectAsync(configuration); |
|
|
|
} |
|
|
|
} |
|
|
|
} |