@@ -182,29 +182,9 @@ public async Task<IGetOperationResult<T>> GetAsync<T>(string key)
182182
183183 if ( commandResult . Success )
184184 {
185- if ( typeof ( T ) . GetTypeCode ( ) == TypeCode . Object && typeof ( T ) != typeof ( Byte [ ] ) )
186- {
187- result . Success = true ;
188- result . Value = this . transcoder . Deserialize < T > ( command . Result ) ;
189- return result ;
190- }
191- else
192- {
193- var tempResult = this . transcoder . Deserialize ( command . Result ) ;
194- if ( tempResult != null )
195- {
196- result . Success = true ;
197- if ( typeof ( T ) == typeof ( Guid ) )
198- {
199- result . Value = ( T ) ( object ) new Guid ( ( string ) tempResult ) ;
200- }
201- else
202- {
203- result . Value = ( T ) tempResult ;
204- }
205- return result ;
206- }
207- }
185+ result . Success = true ;
186+ result . Value = transcoder . Deserialize < T > ( command . Result ) ;
187+ return result ;
208188 }
209189 }
210190 catch ( Exception ex )
@@ -935,9 +915,14 @@ public async Task<bool> RemoveAsync(string key)
935915 /// </summary>
936916 /// <param name="keys">The list of identifiers for the items to retrieve.</param>
937917 /// <returns>a Dictionary holding all items indexed by their key.</returns>
938- public IDictionary < string , object > Get ( IEnumerable < string > keys )
918+ public IDictionary < string , T > Get < T > ( IEnumerable < string > keys )
939919 {
940- return PerformMultiGet < object > ( keys , ( mget , kvp ) => this . transcoder . Deserialize ( kvp . Value ) ) ;
920+ return PerformMultiGet < T > ( keys , ( mget , kvp ) => this . transcoder . Deserialize < T > ( kvp . Value ) ) ;
921+ }
922+
923+ public async Task < IDictionary < string , T > > GetAsync < T > ( IEnumerable < string > keys )
924+ {
925+ return await PerformMultiGetAsync < T > ( keys , ( mget , kvp ) => this . transcoder . Deserialize < T > ( kvp . Value ) ) ;
941926 }
942927
943928 public IDictionary < string , CasResult < object > > GetWithCas ( IEnumerable < string > keys )
@@ -986,7 +971,6 @@ protected virtual IDictionary<string, T> PerformMultiGet<T>(IEnumerable<string>
986971 if ( hashed . TryGetValue ( kvp . Key , out original ) )
987972 {
988973 var result = collector ( mget , kvp ) ;
989-
990974 // the lock will serialize the merge,
991975 // but at least the commands were not waiting on each other
992976 lock ( retval ) retval [ original ] = result ;
@@ -996,7 +980,7 @@ protected virtual IDictionary<string, T> PerformMultiGet<T>(IEnumerable<string>
996980 }
997981 catch ( Exception e )
998982 {
999- _logger . LogError ( "PerformMultiGet" , e ) ;
983+ _logger . LogError ( 0 , e , "PerformMultiGet" ) ;
1000984 }
1001985 } ) ) ;
1002986 }
@@ -1010,6 +994,48 @@ protected virtual IDictionary<string, T> PerformMultiGet<T>(IEnumerable<string>
1010994 return retval ;
1011995 }
1012996
997+ protected virtual async Task < IDictionary < string , T > > PerformMultiGetAsync < T > ( IEnumerable < string > keys , Func < IMultiGetOperation , KeyValuePair < string , CacheItem > , T > collector )
998+ {
999+ // transform the keys and index them by hashed => original
1000+ // the mget results will be mapped using this index
1001+ var hashed = new Dictionary < string , string > ( ) ;
1002+ foreach ( var key in keys )
1003+ {
1004+ hashed [ this . keyTransformer . Transform ( key ) ] = key ;
1005+ }
1006+
1007+ var byServer = GroupByServer ( hashed . Keys ) ;
1008+
1009+ var retval = new Dictionary < string , T > ( hashed . Count ) ;
1010+ var tasks = new List < Task > ( ) ;
1011+
1012+ //execute each list of keys on their respective node
1013+ foreach ( var slice in byServer )
1014+ {
1015+ var node = slice . Key ;
1016+ var nodeKeys = slice . Value ;
1017+ var mget = this . pool . OperationFactory . MultiGet ( nodeKeys ) ;
1018+ var task = Task . Run ( async ( ) =>
1019+ {
1020+ if ( ( await node . ExecuteAsync ( mget ) ) . Success )
1021+ {
1022+ foreach ( var kvp in mget . Result )
1023+ {
1024+ if ( hashed . TryGetValue ( kvp . Key , out var original ) )
1025+ {
1026+ lock ( retval ) retval [ original ] = collector ( mget , kvp ) ;
1027+ }
1028+ }
1029+ }
1030+ } ) ;
1031+ tasks . Add ( task ) ;
1032+ }
1033+
1034+ await Task . WhenAll ( tasks ) ;
1035+
1036+ return retval ;
1037+ }
1038+
10131039 protected Dictionary < IMemcachedNode , IList < string > > GroupByServer ( IEnumerable < string > keys )
10141040 {
10151041 var retval = new Dictionary < IMemcachedNode , IList < string > > ( ) ;
0 commit comments