Merge pull request #2123 from eerhardt/FixRedisBasketRepository

Cache JsonSerializerOptions
This commit is contained in:
David Fowler 2023-06-13 13:52:36 -07:00 committed by GitHub
commit c7c2d1ca2f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 33 additions and 52 deletions

View File

@ -23,9 +23,6 @@ public class OrderApiClient : IOrderApiClient
var ordersDraftResponse = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<OrderData>(ordersDraftResponse, new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
});
return JsonSerializer.Deserialize<OrderData>(ordersDraftResponse, JsonDefaults.CaseInsensitiveOptions);
}
}

View File

@ -23,9 +23,6 @@ public class OrderApiClient : IOrderApiClient
var ordersDraftResponse = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<OrderData>(ordersDraftResponse, new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
});
return JsonSerializer.Deserialize<OrderData>(ordersDraftResponse, JsonDefaults.CaseInsensitiveOptions);
}
}

View File

@ -5,6 +5,9 @@ public class EventBusRabbitMQ : IEventBus, IDisposable
{
const string BROKER_NAME = "eshop_event_bus";
private static readonly JsonSerializerOptions s_indentedOptions = new() { WriteIndented = true };
private static readonly JsonSerializerOptions s_caseInsensitiveOptions = new() { PropertyNameCaseInsensitive = true };
private readonly IRabbitMQPersistentConnection _persistentConnection;
private readonly ILogger<EventBusRabbitMQ> _logger;
private readonly IEventBusSubscriptionsManager _subsManager;
@ -69,10 +72,7 @@ public class EventBusRabbitMQ : IEventBus, IDisposable
channel.ExchangeDeclare(exchange: BROKER_NAME, type: "direct");
var body = JsonSerializer.SerializeToUtf8Bytes(@event, @event.GetType(), new JsonSerializerOptions
{
WriteIndented = true
});
var body = JsonSerializer.SerializeToUtf8Bytes(@event, @event.GetType(), s_indentedOptions);
policy.Execute(() =>
{
@ -256,7 +256,7 @@ public class EventBusRabbitMQ : IEventBus, IDisposable
var handler = scope.ServiceProvider.GetService(subscription.HandlerType);
if (handler == null) continue;
var eventType = _subsManager.GetEventTypeByName(eventName);
var integrationEvent = JsonSerializer.Deserialize(message, eventType, new JsonSerializerOptions() { PropertyNameCaseInsensitive = true });
var integrationEvent = JsonSerializer.Deserialize(message, eventType, s_caseInsensitiveOptions);
var concreteType = typeof(IIntegrationEventHandler<>).MakeGenericType(eventType);
await Task.Yield();

View File

@ -2,16 +2,16 @@
public class IntegrationEventLogEntry
{
private static readonly JsonSerializerOptions s_indentedOptions = new() { WriteIndented = true };
private static readonly JsonSerializerOptions s_caseInsensitiveOptions = new() { PropertyNameCaseInsensitive = true };
private IntegrationEventLogEntry() { }
public IntegrationEventLogEntry(IntegrationEvent @event, Guid transactionId)
{
EventId = @event.Id;
CreationTime = @event.CreationDate;
EventTypeName = @event.GetType().FullName;
Content = JsonSerializer.Serialize(@event, @event.GetType(), new JsonSerializerOptions
{
WriteIndented = true
});
Content = JsonSerializer.Serialize(@event, @event.GetType(), s_indentedOptions);
State = EventStateEnum.NotPublished;
TimesSent = 0;
TransactionId = transactionId.ToString();
@ -30,7 +30,7 @@ public class IntegrationEventLogEntry
public IntegrationEventLogEntry DeserializeJsonContent(Type type)
{
IntegrationEvent = JsonSerializer.Deserialize(Content, type, new JsonSerializerOptions() { PropertyNameCaseInsensitive = true }) as IntegrationEvent;
IntegrationEvent = JsonSerializer.Deserialize(Content, type, s_caseInsensitiveOptions) as IntegrationEvent;
return this;
}
}

View File

@ -35,15 +35,12 @@ public class RedisBasketRepository : IBasketRepository
return null;
}
return JsonSerializer.Deserialize<CustomerBasket>(data, new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
});
return JsonSerializer.Deserialize<CustomerBasket>(data, JsonDefaults.CaseInsensitiveOptions);
}
public async Task<CustomerBasket> UpdateBasketAsync(CustomerBasket basket)
{
var created = await _database.StringSetAsync(basket.BuyerId, JsonSerializer.Serialize(basket));
var created = await _database.StringSetAsync(basket.BuyerId, JsonSerializer.Serialize(basket, JsonDefaults.CaseInsensitiveOptions));
if (!created)
{
@ -51,7 +48,7 @@ public class RedisBasketRepository : IBasketRepository
return null;
}
_logger.LogInformation("Basket item persisted succesfully.");
_logger.LogInformation("Basket item persisted successfully.");
return await GetBasketAsync(basket.BuyerId);
}

View File

@ -0,0 +1,11 @@
using System.Text.Json;
namespace Services.Common;
public static class JsonDefaults
{
public static readonly JsonSerializerOptions CaseInsensitiveOptions = new()
{
PropertyNameCaseInsensitive = true
};
}

View File

@ -29,10 +29,7 @@ public class BasketService : IBasketService
var responseString = await response.Content.ReadAsStringAsync();
return string.IsNullOrEmpty(responseString) ?
new Basket() { BuyerId = user.Id } :
JsonSerializer.Deserialize<Basket>(responseString, new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
});
JsonSerializer.Deserialize<Basket>(responseString, JsonDefaults.CaseInsensitiveOptions);
}
public async Task<Basket> UpdateBasket(Basket basket)
@ -82,10 +79,7 @@ public class BasketService : IBasketService
var jsonResponse = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<Basket>(jsonResponse, new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
});
return JsonSerializer.Deserialize<Basket>(jsonResponse, JsonDefaults.CaseInsensitiveOptions);
}
public async Task<Order> GetOrderDraft(string basketId)
@ -94,10 +88,7 @@ public class BasketService : IBasketService
var responseString = await _apiClient.GetStringAsync(uri);
var response = JsonSerializer.Deserialize<Order>(responseString, new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
});
var response = JsonSerializer.Deserialize<Order>(responseString, JsonDefaults.CaseInsensitiveOptions);
return response;
}

View File

@ -23,10 +23,7 @@ public class CatalogService : ICatalogService
var responseString = await _httpClient.GetStringAsync(uri);
var catalog = JsonSerializer.Deserialize<Catalog>(responseString, new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
});
var catalog = JsonSerializer.Deserialize<Catalog>(responseString, JsonDefaults.CaseInsensitiveOptions);
return catalog;
}

View File

@ -23,10 +23,7 @@ public class OrderingService : IOrderingService
var responseString = await _httpClient.GetStringAsync(uri);
var response = JsonSerializer.Deserialize<Order>(responseString, new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
});
var response = JsonSerializer.Deserialize<Order>(responseString, JsonDefaults.CaseInsensitiveOptions);
return response;
}
@ -37,10 +34,7 @@ public class OrderingService : IOrderingService
var responseString = await _httpClient.GetStringAsync(uri);
var response = JsonSerializer.Deserialize<List<Order>>(responseString, new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
});
var response = JsonSerializer.Deserialize<List<Order>>(responseString, JsonDefaults.CaseInsensitiveOptions);
return response;
}

View File

@ -14,10 +14,7 @@ public class WebhooksClient : IWebhooksClient
var client = _httpClientFactory.CreateClient("GrantClient");
var response = await client.GetAsync(_options.WebhooksUrl + "/api/v1/webhooks");
var json = await response.Content.ReadAsStringAsync();
var subscriptions = JsonSerializer.Deserialize<IEnumerable<WebhookResponse>>(json, new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
});
var subscriptions = JsonSerializer.Deserialize<IEnumerable<WebhookResponse>>(json, JsonDefaults.CaseInsensitiveOptions);
return subscriptions;
}
}