11using System ;
2+ using System . Collections . Concurrent ;
23using System . Collections . Generic ;
34using System . Globalization ;
45using System . Linq ;
@@ -15,13 +16,13 @@ namespace Nest
1516 internal static class Extensions
1617 {
1718 internal static TReturn InvokeOrDefault < T , TReturn > ( this Func < T , TReturn > func , T @default )
18- where T : class , TReturn where TReturn : class =>
19+ where T : class , TReturn where TReturn : class =>
1920 func ? . Invoke ( @default ) ?? @default ;
20-
21+
2122 internal static TReturn InvokeOrDefault < T1 , T2 , TReturn > ( this Func < T1 , T2 , TReturn > func , T1 @default , T2 param2 )
22- where T1 : class , TReturn where TReturn : class =>
23+ where T1 : class , TReturn where TReturn : class =>
2324 func ? . Invoke ( @default , param2 ) ?? @default ;
24-
25+
2526 internal static QueryContainer InvokeQuery < T > (
2627 this Func < QueryContainerDescriptor < T > , QueryContainer > f ,
2728 QueryContainerDescriptor < T > container )
@@ -33,7 +34,7 @@ internal static QueryContainer InvokeQuery<T>(
3334 return c ;
3435
3536 //query is conditionless but the container is marked as strict, throw exception
36- if ( c != null && c . IsStrict )
37+ if ( c != null && c . IsStrict )
3738 throw new ArgumentException ( "Query is conditionless but strict is turned on" ) ;
3839
3940 //query is conditionless return an empty container that can later be rewritten
@@ -53,7 +54,7 @@ internal static string GetStringValue(this Enum enumValue)
5354
5455 return da != null ? da . Value : Enum . GetName ( enumValue . GetType ( ) , enumValue ) ;
5556 }
56-
57+
5758 internal static readonly JsonConverter dateConverter = new IsoDateTimeConverter { Culture = CultureInfo . InvariantCulture } ;
5859 internal static readonly JsonSerializer serializer = new JsonSerializer ( ) ;
5960 internal static string ToJsonNetString ( this DateTime date )
@@ -70,41 +71,40 @@ internal static IEnumerable<T> DistinctBy<T, TKey>(this IEnumerable<T> items, Fu
7071 return items . GroupBy ( property ) . Select ( x => x . First ( ) ) ;
7172 }
7273
73- internal static Dictionary < string , object > _enumCache = new Dictionary < string , object > ( ) ;
74+ internal static ConcurrentDictionary < string , object > _enumCache = new ConcurrentDictionary < string , object > ( ) ;
7475 internal static T ? ToEnum < T > ( this string str ) where T : struct
7576 {
7677 var enumType = typeof ( T ) ;
7778 var key = $ "{ enumType . Name } .{ str } ";
78- if ( _enumCache . ContainsKey ( key ) )
79- return ( T ) _enumCache [ key ] ;
79+ object value ;
80+ if ( _enumCache . TryGetValue ( key , out value ) )
81+ return ( T ) value ;
8082
81- foreach ( var name in Enum . GetNames ( enumType ) )
83+ foreach ( var name in Enum . GetNames ( enumType ) )
8284 {
83- if ( name . Equals ( str , StringComparison . OrdinalIgnoreCase ) ) return ( T ) Enum . Parse ( enumType , name , true ) ;
85+ if ( name . Equals ( str , StringComparison . OrdinalIgnoreCase ) )
86+ {
87+ var v = ( T ) Enum . Parse ( enumType , name , true ) ;
88+ _enumCache . TryAdd ( key , v ) ;
89+ return v ;
90+ }
8491
8592 var enumFieldInfo = enumType . GetField ( name ) ;
8693 var enumMemberAttribute = enumFieldInfo . GetCustomAttribute < EnumMemberAttribute > ( ) ;
87-
88- if ( enumMemberAttribute != null )
94+ if ( enumMemberAttribute ? . Value == str )
8995 {
90- if ( enumMemberAttribute . Value == str )
91- {
92- var value = ( T ) Enum . Parse ( enumType , name ) ;
93- _enumCache . Add ( key , value ) ;
94- return value ;
95- }
96+ var v = ( T ) Enum . Parse ( enumType , name ) ;
97+ _enumCache . TryAdd ( key , v ) ;
98+ return v ;
9699 }
97100
98101 var alternativeEnumMemberAttribute = enumFieldInfo . GetCustomAttribute < AlternativeEnumMemberAttribute > ( ) ;
99102
100- if ( alternativeEnumMemberAttribute != null )
103+ if ( alternativeEnumMemberAttribute ? . Value == str )
101104 {
102- if ( alternativeEnumMemberAttribute . Value == str )
103- {
104- var value = ( T ) Enum . Parse ( enumType , name ) ;
105- _enumCache . Add ( key , value ) ;
106- return value ;
107- }
105+ var v = ( T ) Enum . Parse ( enumType , name ) ;
106+ _enumCache . TryAdd ( key , v ) ;
107+ return v ;
108108 }
109109 }
110110
0 commit comments