Skip to content

Commit a49c79f

Browse files
committed
Refactory GetAsync<T> and Get<T>
1 parent 193c30b commit a49c79f

File tree

1 file changed

+68
-83
lines changed

1 file changed

+68
-83
lines changed

Enyim.Caching/MemcachedClient.cs

Lines changed: 68 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
using System;
2-
using System.Linq;
3-
using Enyim.Caching.Configuration;
1+
using Enyim.Caching.Configuration;
42
using Enyim.Caching.Memcached;
5-
using System.Collections.Generic;
6-
using System.Threading;
7-
using System.Net;
8-
using System.Diagnostics;
93
using Enyim.Caching.Memcached.Results;
10-
using Enyim.Caching.Memcached.Results.Factories;
114
using Enyim.Caching.Memcached.Results.Extensions;
12-
using System.Threading.Tasks;
5+
using Enyim.Caching.Memcached.Results.Factories;
6+
using Microsoft.Extensions.Caching.Distributed;
137
using Microsoft.Extensions.Logging;
148
using Microsoft.Extensions.Options;
15-
using Microsoft.Extensions.Caching.Distributed;
9+
using System;
10+
using System.Collections.Generic;
11+
using System.Diagnostics;
12+
using System.Linq;
13+
using System.Net;
14+
using System.Threading;
15+
using System.Threading.Tasks;
1616

1717
namespace Enyim.Caching
1818
{
@@ -113,7 +113,6 @@ public async Task SetAsync(string key, object value, int cacheSeconds)
113113
/// </summary>
114114
/// <param name="key">The identifier for the item to retrieve.</param>
115115
/// <returns>The retrieved item, or <value>null</value> if the key was not found.</returns>
116-
[Obsolete]
117116
public object Get(string key)
118117
{
119118
object tmp;
@@ -129,95 +128,85 @@ public object Get(string key)
129128
[Obsolete]
130129
public T Get<T>(string key)
131130
{
132-
var hashedKey = this.keyTransformer.Transform(key);
133-
var node = this.pool.Locate(hashedKey);
131+
var result = PerformGet<T>(key);
132+
return result.Success ? result.Value : default(T);
133+
}
134134

135-
if (node != null)
135+
public IGetOperationResult<T> PerformGet<T>(string key)
136+
{
137+
if (!CreateGetCommand<T>(key, out var result, out var node, out var command))
136138
{
137-
try
138-
{
139-
var command = this.pool.OperationFactory.Get(hashedKey);
140-
var commandResult = node.Execute(command);
139+
return result;
140+
}
141+
142+
try
143+
{
144+
var commandResult = node.Execute(command);
145+
return BuildGetCommandResult<T>(result, command, commandResult);
141146

142-
if (commandResult.Success)
143-
{
144-
if (typeof(T).GetTypeCode() == TypeCode.Object && typeof(T) != typeof(Byte[]))
145-
{
146-
return this.transcoder.Deserialize<T>(command.Result);
147-
}
148-
else
149-
{
150-
var tempResult = this.transcoder.Deserialize(command.Result);
151-
if (tempResult != null)
152-
{
153-
if (typeof(T) == typeof(Guid))
154-
{
155-
return (T)(object)new Guid((string)tempResult);
156-
}
157-
else
158-
{
159-
return (T)tempResult;
160-
}
161-
}
162-
}
163-
}
164-
}
165-
catch (Exception ex)
166-
{
167-
_logger.LogError(0, ex, $"{nameof(GetAsync)}(\"{key}\")");
168-
throw ex;
169-
}
170147
}
171-
else
148+
catch (Exception ex)
172149
{
173-
_logger.LogError($"Unable to locate memcached node");
150+
_logger.LogError(0, ex, $"{nameof(PerformGet)}(\"{key}\")");
151+
result.Fail(ex.Message);
152+
return result;
174153
}
175-
176-
return default(T);
177154
}
178155

179-
public async Task<IGetOperationResult<T>> GetAsync<T>(string key)
156+
private bool CreateGetCommand<T>(string key, out IGetOperationResult<T> result, out IMemcachedNode node, out IGetOperation command)
180157
{
181-
var result = new DefaultGetOperationResultFactory<T>().Create();
182-
158+
result = new DefaultGetOperationResultFactory<T>().Create();
183159
var hashedKey = this.keyTransformer.Transform(key);
184-
var node = this.pool.Locate(hashedKey);
185160

186-
if (node != null)
161+
node = this.pool.Locate(hashedKey);
162+
if (node == null)
187163
{
188-
try
189-
{
190-
var command = this.pool.OperationFactory.Get(hashedKey);
191-
var commandResult = await node.ExecuteAsync(command);
164+
var errorMessage = $"Unable to locate node with \"{key}\" key";
165+
_logger.LogError(errorMessage);
166+
result.Fail(errorMessage);
167+
command = null;
168+
return false;
169+
}
192170

193-
if (commandResult.Success)
194-
{
195-
result.Success = true;
196-
result.Value = transcoder.Deserialize<T>(command.Result);
197-
return result;
198-
}
199-
else
200-
{
201-
commandResult.Combine(result);
171+
command = this.pool.OperationFactory.Get(hashedKey);
172+
return true;
173+
}
202174

203-
return result;
204-
}
205-
}
206-
catch (Exception ex)
207-
{
208-
_logger.LogError(0, ex, $"{nameof(GetAsync)}(\"{key}\")");
209-
throw ex;
210-
}
175+
private IGetOperationResult<T> BuildGetCommandResult<T>(IGetOperationResult<T> result, IGetOperation command, IOperationResult commandResult)
176+
{
177+
if (commandResult.Success)
178+
{
179+
result.Value = transcoder.Deserialize<T>(command.Result);
180+
result.Pass();
211181
}
212182
else
213183
{
214-
_logger.LogError($"Unable to locate memcached node");
184+
commandResult.Combine(result);
215185
}
216186

217-
result.Fail("Unable to locate node");
218187
return result;
219188
}
220189

190+
public async Task<IGetOperationResult<T>> GetAsync<T>(string key)
191+
{
192+
if (!CreateGetCommand<T>(key, out var result, out var node, out var command))
193+
{
194+
return result;
195+
}
196+
197+
try
198+
{
199+
var commandResult = await node.ExecuteAsync(command);
200+
return BuildGetCommandResult<T>(result, command, commandResult);
201+
}
202+
catch (Exception ex)
203+
{
204+
_logger.LogError(0, ex, $"{nameof(GetAsync)}(\"{key}\")");
205+
result.Fail(ex.Message);
206+
return result;
207+
}
208+
}
209+
221210
public async Task<T> GetValueAsync<T>(string key)
222211
{
223212
var result = await GetAsync<T>(key);
@@ -246,7 +235,7 @@ public async Task<T> GetValueOrCreateAsync<T>(string key, int cacheSeconds, Func
246235
{
247236
await AddAsync(key, value, cacheSeconds);
248237
}
249-
catch(Exception ex)
238+
catch (Exception ex)
250239
{
251240
_logger.LogError(ex, $"{nameof(AddAsync)}(\"{key}\", ..., {cacheSeconds})");
252241
}
@@ -260,21 +249,18 @@ public async Task<T> GetValueOrCreateAsync<T>(string key, int cacheSeconds, Func
260249
/// <param name="key">The identifier for the item to retrieve.</param>
261250
/// <param name="value">The retrieved item or null if not found.</param>
262251
/// <returns>The <value>true</value> if the item was successfully retrieved.</returns>
263-
[Obsolete]
264252
public bool TryGet(string key, out object value)
265253
{
266254
ulong cas = 0;
267255

268256
return this.PerformTryGet(key, out cas, out value).Success;
269257
}
270258

271-
[Obsolete]
272259
public CasResult<object> GetWithCas(string key)
273260
{
274261
return this.GetWithCas<object>(key);
275262
}
276263

277-
[Obsolete]
278264
public CasResult<T> GetWithCas<T>(string key)
279265
{
280266
CasResult<object> tmp;
@@ -284,7 +270,6 @@ public CasResult<T> GetWithCas<T>(string key)
284270
: new CasResult<T> { Cas = tmp.Cas, Result = default(T) };
285271
}
286272

287-
[Obsolete]
288273
public bool TryGetWithCas(string key, out CasResult<object> value)
289274
{
290275
object tmp;

0 commit comments

Comments
 (0)