Skip to content

Commit 7625fc3

Browse files
authored
Merge pull request #67 from cnblogs/refacotry
Refacotry Get opertion
2 parents caaf934 + e2dc2fa commit 7625fc3

File tree

4 files changed

+102
-127
lines changed

4 files changed

+102
-127
lines changed

Enyim.Caching.Tests/MemcachedClientTestsBase.cs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using Enyim.Caching.Configuration;
6-
using Enyim.Caching.Memcached.Results;
1+
using Enyim.Caching.Configuration;
72
using Enyim.Caching.Memcached;
8-
using Xunit;
9-
using Microsoft.Extensions.DependencyInjection;
3+
using Enyim.Caching.Memcached.Results;
104
using Microsoft.Extensions.Configuration;
5+
using Microsoft.Extensions.DependencyInjection;
116
using Microsoft.Extensions.Logging;
7+
using System;
8+
using System.Collections.Generic;
9+
using System.Linq;
10+
using System.Text;
1211
using System.Threading.Tasks;
12+
using Xunit;
1313

1414
namespace Enyim.Caching.Tests
1515
{
@@ -23,10 +23,7 @@ public MemcachedClientTestsBase(Action<MemcachedClientOptions> onAddEnyimMemcach
2323
services.AddEnyimMemcached(options =>
2424
{
2525
options.AddServer("memcached", 11211);
26-
if (onAddEnyimMemcached != null)
27-
{
28-
onAddEnyimMemcached(options);
29-
}
26+
onAddEnyimMemcached?.Invoke(options);
3027
});
3128

3229
services.AddLogging(builder => builder.SetMinimumLevel(LogLevel.Debug).AddConsole());

Enyim.Caching.Tests/MemcachedClientWithKeyTransformerTests.cs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
1-
using System;
1+
using Enyim.Caching.Configuration;
2+
using Enyim.Caching.Memcached;
3+
using System;
24
using System.Collections.Generic;
35
using System.Linq;
46
using System.Text;
57
using System.Threading.Tasks;
6-
using Enyim.Caching.Configuration;
7-
using Enyim.Caching.Memcached;
88
using Xunit;
99

1010
namespace Enyim.Caching.Tests
1111
{
1212
public class MemcachedClientWithKeyTransformerTests : MemcachedClientTestsBase
1313
{
14-
public MemcachedClientWithKeyTransformerTests(Action<MemcachedClientOptions> onAddEnyimMemcached = null)
15-
: base(options =>
16-
{
17-
options.KeyTransformer = "Enyim.Caching.Memcached.TigerHashKeyTransformer";
18-
if (onAddEnyimMemcached != null)
19-
{
20-
onAddEnyimMemcached(options);
21-
}
22-
})
14+
public MemcachedClientWithKeyTransformerTests()
15+
: base(options => options.KeyTransformer = "Enyim.Caching.Memcached.TigerHashKeyTransformer")
2316
{
2417
}
2518

Enyim.Caching/Configuration/AuthenticationConfiguration.cs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1+
using Enyim.Caching.Memcached;
12
using System;
23
using System.Collections.Generic;
3-
using Enyim.Caching.Memcached;
44

55
namespace Enyim.Caching.Configuration
66
{
7-
public class AuthenticationConfiguration : IAuthenticationConfiguration
8-
{
9-
private Type authenticator;
10-
private Dictionary<string, object> parameters;
7+
public class AuthenticationConfiguration : IAuthenticationConfiguration
8+
{
9+
private Type authenticator;
10+
private Dictionary<string, object> parameters;
1111

12-
Type IAuthenticationConfiguration.Type
13-
{
14-
get { return this.authenticator; }
15-
set
16-
{
17-
ConfigurationHelper.CheckForInterface(value, typeof(ISaslAuthenticationProvider));
18-
this.authenticator = value;
19-
}
20-
}
12+
Type IAuthenticationConfiguration.Type
13+
{
14+
get { return this.authenticator; }
15+
set
16+
{
17+
ConfigurationHelper.CheckForInterface(value, typeof(ISaslAuthenticationProvider));
18+
this.authenticator = value;
19+
}
20+
}
2121

22-
Dictionary<string, object> IAuthenticationConfiguration.Parameters
23-
{
24-
get { return this.parameters ?? (this.parameters = new Dictionary<string, object>()); }
25-
}
26-
}
22+
Dictionary<string, object> IAuthenticationConfiguration.Parameters
23+
{
24+
get { return this.parameters ?? (this.parameters = new Dictionary<string, object>()); }
25+
}
26+
}
2727
}
2828

2929
#region [ License information ]
3030
/* ************************************************************
3131
*
32-
* Copyright (c) 2010 Attila Kiskó, enyim.com
32+
* Copyright (c) 2010 Attila Kisk? enyim.com
3333
*
3434
* Licensed under the Apache License, Version 2.0 (the "License");
3535
* you may not use this file except in compliance with the License.

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)