@@ -261,18 +261,31 @@ public bool TryGet(string key, out object value)
261
261
return this . PerformTryGet ( key , out cas , out value ) . Success ;
262
262
}
263
263
264
+ /// <summary>
265
+ /// Tries to get an item from the cache.
266
+ /// </summary>
267
+ /// <param name="key">The identifier for the item to retrieve.</param>
268
+ /// <param name="value">The retrieved item or null if not found.</param>
269
+ /// <returns>The <value>true</value> if the item was successfully retrieved.</returns>
270
+ public bool TryGet < T > ( string key , out T value )
271
+ {
272
+ ulong cas = 0 ;
273
+
274
+ return this . PerformTryGet ( key , out cas , out value ) . Success ;
275
+ }
276
+
264
277
public CasResult < object > GetWithCas ( string key )
265
278
{
266
279
return this . GetWithCas < object > ( key ) ;
267
280
}
268
281
269
282
public CasResult < T > GetWithCas < T > ( string key )
270
283
{
271
- CasResult < object > tmp ;
284
+ CasResult < T > tmp ;
272
285
273
286
return this . TryGetWithCas ( key , out tmp )
274
- ? new CasResult < T > { Cas = tmp . Cas , Result = ( T ) tmp . Result }
275
- : new CasResult < T > { Cas = tmp . Cas , Result = default ( T ) } ;
287
+ ? new CasResult < T > { Cas = tmp . Cas , Result = tmp . Result }
288
+ : new CasResult < T > { Cas = tmp . Cas , Result = default } ;
276
289
}
277
290
278
291
public bool TryGetWithCas ( string key , out CasResult < object > value )
@@ -287,6 +300,15 @@ public bool TryGetWithCas(string key, out CasResult<object> value)
287
300
return retval . Success ;
288
301
}
289
302
303
+ public bool TryGetWithCas < T > ( string key , out CasResult < T > value )
304
+ {
305
+ var retVal = PerformTryGet ( key , out var cas , out T tmp ) ;
306
+
307
+ value = new CasResult < T > { Cas = cas , Result = tmp } ;
308
+
309
+ return retVal . Success ;
310
+ }
311
+
290
312
protected virtual IGetOperationResult PerformTryGet ( string key , out ulong cas , out object value )
291
313
{
292
314
var hashedKey = this . keyTransformer . Transform ( key ) ;
@@ -323,6 +345,40 @@ protected virtual IGetOperationResult PerformTryGet(string key, out ulong cas, o
323
345
return result ;
324
346
}
325
347
348
+ protected virtual IGetOperationResult PerformTryGet < T > ( string key , out ulong cas , out T value )
349
+ {
350
+ var hashedKey = keyTransformer . Transform ( key ) ;
351
+ var node = pool . Locate ( hashedKey ) ;
352
+ var result = GetOperationResultFactory . Create ( ) ;
353
+
354
+ cas = 0 ;
355
+ value = default ;
356
+
357
+ if ( node != null )
358
+ {
359
+ var command = pool . OperationFactory . Get ( hashedKey ) ;
360
+ var commandResult = node . Execute ( command ) ;
361
+
362
+ if ( commandResult . Success )
363
+ {
364
+ result . Value = value = transcoder . Deserialize < T > ( command . Result ) ;
365
+ result . Cas = cas = command . CasValue ;
366
+
367
+ result . Pass ( ) ;
368
+ return result ;
369
+ }
370
+
371
+ commandResult . Combine ( result ) ;
372
+ return result ;
373
+ }
374
+
375
+ result . Value = value ;
376
+ result . Cas = cas ;
377
+
378
+ result . Fail ( "Unable to locate node" ) ;
379
+ return result ;
380
+ }
381
+
326
382
327
383
#region [ Store ]
328
384
0 commit comments