Skip to content

Commit e124b56

Browse files
authored
Merge pull request #124 from safetree/improve-TryGet_TryGetWithCas_ExecuteTryGet-generic-support
improve TryGet<T> TryGetWithCas<T> ExecuteTryGet<T> generic support
2 parents f7900d4 + 8843fbe commit e124b56

File tree

7 files changed

+118
-11
lines changed

7 files changed

+118
-11
lines changed

Enyim.Caching.Tests/MemcachedClientGetTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ public void When_TryGetting_Existing_Item_Value_Is_Not_Null_And_Result_Is_Succes
4545
GetAssertPass(getResult, temp);
4646
}
4747

48+
[Fact]
49+
public void When_Generic_TryGetting_Existing_Item_Value_Is_Not_Null_And_Result_Is_Successful()
50+
{
51+
var key = GetUniqueKey("get");
52+
var value = GetRandomString();
53+
var storeResult = Store(key: key, value: value);
54+
StoreAssertPass(storeResult);
55+
56+
string temp;
57+
var getResult = _client.ExecuteTryGet(key, out temp);
58+
GetAssertPass(getResult, temp);
59+
}
60+
4861
[Fact]
4962
public void When_Generic_Getting_Existing_Item_Value_Is_Not_Null_And_Result_Is_Successful()
5063
{

Enyim.Caching/IMemcachedClient.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ public interface IMemcachedClient : IDisposable
2626
Task<IDictionary<string, T>> GetAsync<T>(IEnumerable<string> keys);
2727

2828
bool TryGet(string key, out object value);
29+
bool TryGet<T>(string key, out T value);
2930
bool TryGetWithCas(string key, out CasResult<object> value);
31+
bool TryGetWithCas<T>(string key, out CasResult<T> value);
3032

3133
CasResult<object> GetWithCas(string key);
3234
CasResult<T> GetWithCas<T>(string key);

Enyim.Caching/IMemcachedResultsClient.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public interface IMemcachedResultsClient
1818
IDictionary<string, IGetOperationResult> ExecuteGet(IEnumerable<string> keys);
1919

2020
IGetOperationResult ExecuteTryGet(string key, out object value);
21+
IGetOperationResult ExecuteTryGet<T>(string key, out T value);
2122

2223
IStoreOperationResult ExecuteStore(StoreMode mode, string key, object value);
2324
IStoreOperationResult ExecuteStore(StoreMode mode, string key, object value, DateTime expiresAt);

Enyim.Caching/MemcachedClient.Results.cs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,16 +145,29 @@ public IGetOperationResult ExecuteTryGet(string key, out object value)
145145
ulong cas = 0;
146146

147147
return this.PerformTryGet(key, out cas, out value);
148-
}
148+
}
149149

150-
/// <summary>
151-
/// Retrieves the specified item from the cache.
152-
/// </summary>
153-
/// <param name="key">The identifier for the item to retrieve.</param>
154-
/// <returns>The retrieved item, or <value>default(T)</value> if the key was not found.</returns>
155-
public IGetOperationResult<T> ExecuteGet<T>(string key)
150+
/// <summary>
151+
/// Tries to get an item from the cache.
152+
/// </summary>
153+
/// <param name="key">The identifier for the item to retrieve.</param>
154+
/// <param name="value">The retrieved item or null if not found.</param>
155+
/// <returns>The <value>true</value> if the item was successfully retrieved.</returns>
156+
public IGetOperationResult ExecuteTryGet<T>(string key, out T value)
157+
{
158+
ulong cas = 0;
159+
160+
return this.PerformTryGet(key, out cas, out value);
161+
}
162+
163+
/// <summary>
164+
/// Retrieves the specified item from the cache.
165+
/// </summary>
166+
/// <param name="key">The identifier for the item to retrieve.</param>
167+
/// <returns>The retrieved item, or <value>default(T)</value> if the key was not found.</returns>
168+
public IGetOperationResult<T> ExecuteGet<T>(string key)
156169
{
157-
object tmp;
170+
T tmp;
158171
var result = new DefaultGetOperationResultFactory<T>().Create();
159172

160173
var tryGetResult = ExecuteTryGet(key, out tmp);

Enyim.Caching/MemcachedClient.cs

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,18 +261,31 @@ public bool TryGet(string key, out object value)
261261
return this.PerformTryGet(key, out cas, out value).Success;
262262
}
263263

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+
264277
public CasResult<object> GetWithCas(string key)
265278
{
266279
return this.GetWithCas<object>(key);
267280
}
268281

269282
public CasResult<T> GetWithCas<T>(string key)
270283
{
271-
CasResult<object> tmp;
284+
CasResult<T> tmp;
272285

273286
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 };
276289
}
277290

278291
public bool TryGetWithCas(string key, out CasResult<object> value)
@@ -287,6 +300,15 @@ public bool TryGetWithCas(string key, out CasResult<object> value)
287300
return retval.Success;
288301
}
289302

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+
290312
protected virtual IGetOperationResult PerformTryGet(string key, out ulong cas, out object value)
291313
{
292314
var hashedKey = this.keyTransformer.Transform(key);
@@ -323,6 +345,40 @@ protected virtual IGetOperationResult PerformTryGet(string key, out ulong cas, o
323345
return result;
324346
}
325347

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+
326382

327383
#region [ Store ]
328384

Enyim.Caching/MemcachedClientT.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,19 @@ public bool TryGet(string key, out object value)
280280
return _memcachedClient.TryGet(key, out value);
281281
}
282282

283+
public bool TryGet<T1>(string key, out T1 value)
284+
{
285+
return _memcachedClient.TryGet(key, out value);
286+
}
287+
283288
public bool TryGetWithCas(string key, out CasResult<object> value)
284289
{
285290
return _memcachedClient.TryGetWithCas(key, out value);
286291
}
292+
293+
public bool TryGetWithCas<T1>(string key, out CasResult<T1> value)
294+
{
295+
return _memcachedClient.TryGetWithCas(key, out value);
296+
}
287297
}
288298
}

Enyim.Caching/NullMemcachedClient.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,24 @@ public bool TryGet(string key, out object value)
228228
return false;
229229
}
230230

231+
public bool TryGet<T>(string key, out T value)
232+
{
233+
value = default;
234+
return false;
235+
}
236+
231237
public bool TryGetWithCas(string key, out CasResult<object> value)
232238
{
233239
value = new CasResult<object>();
234240
return false;
235241
}
236242

243+
public bool TryGetWithCas<T>(string key, out CasResult<T> value)
244+
{
245+
value = new CasResult<T>();
246+
return false;
247+
}
248+
237249
public bool Add(string key, object value, int cacheSeconds)
238250
{
239251
return true;

0 commit comments

Comments
 (0)