From f7b08ce288c13e54e815a58ae718a2f7f9b0b36a Mon Sep 17 00:00:00 2001 From: "Eric J. Smith" Date: Wed, 8 May 2019 13:13:40 -0500 Subject: [PATCH] Some cleanup --- .../Messaging/ITypeNameSerializer.cs | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/Foundatio/Messaging/ITypeNameSerializer.cs b/src/Foundatio/Messaging/ITypeNameSerializer.cs index 5a5c9041e..c8622b3c1 100644 --- a/src/Foundatio/Messaging/ITypeNameSerializer.cs +++ b/src/Foundatio/Messaging/ITypeNameSerializer.cs @@ -12,17 +12,19 @@ public interface ITypeNameSerializer { } public class DefaultTypeNameSerializer : ITypeNameSerializer { - private readonly Dictionary _typeNameOverrides; private readonly ILogger _logger; + private readonly Dictionary _typeNameOverrides; + private readonly ConcurrentDictionary _typeNameCache = new ConcurrentDictionary(); + private readonly ConcurrentDictionary _typeCache = new ConcurrentDictionary(); public DefaultTypeNameSerializer(ILogger logger = null, IDictionary typeNameOverrides = null) { _logger = logger ?? NullLogger.Instance; - _typeNameOverrides = typeNameOverrides != null ? new Dictionary(typeNameOverrides) : new Dictionary(); + if (typeNameOverrides != null) + _typeNameOverrides = new Dictionary(typeNameOverrides); } - private readonly ConcurrentDictionary _knownMessageTypesCache = new ConcurrentDictionary(); public Type Deserialize(string typeName) { - return _knownMessageTypesCache.GetOrAdd(typeName, newTypeName => { + return _typeCache.GetOrAdd(typeName, newTypeName => { if (_typeNameOverrides != null && _typeNameOverrides.ContainsKey(newTypeName)) return _typeNameOverrides[newTypeName]; @@ -37,12 +39,13 @@ public Type Deserialize(string typeName) { }); } - private readonly ConcurrentDictionary _mappedMessageTypesCache = new ConcurrentDictionary(); public string Serialize(Type type) { - return _mappedMessageTypesCache.GetOrAdd(type, newType => { - var reversedMap = _typeNameOverrides.ToDictionary(kvp => kvp.Value, kvp => kvp.Key); - if (reversedMap.ContainsKey(newType)) - return reversedMap[newType]; + return _typeNameCache.GetOrAdd(type, newType => { + if (_typeNameOverrides != null) { + var reversedMap = _typeNameOverrides.ToDictionary(kvp => kvp.Value, kvp => kvp.Key); + if (reversedMap.ContainsKey(newType)) + return reversedMap[newType]; + } return String.Concat(type.FullName, ", ", type.Assembly.GetName().Name); });