Skip to content

Commit ff30c32

Browse files
authored
Merge pull request #364 from dotnetcore/improve-iss357
Improve memcached operation when can not create connection
2 parents bac207b + 989f2ac commit ff30c32

File tree

4 files changed

+75
-10
lines changed

4 files changed

+75
-10
lines changed

src/EasyCaching.Core/EasyCachingException.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ public EasyCachingException(string message)
88
: base(message)
99
{
1010
}
11+
12+
public EasyCachingException(string message, Exception innerException)
13+
: base(message, innerException)
14+
{
15+
}
1116
}
1217

1318
public class EasyCachingNotFoundException : Exception

src/EasyCaching.Memcached/DefaultMemcachedCachingProvider.Async.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public override async Task<CacheValue<T>> BaseGetAsync<T>(string cacheKey)
7878
else
7979
{
8080
OnCacheMiss(cacheKey);
81+
CheckResult(result);
8182
return CacheValue<T>.NoValue;
8283
}
8384
}
@@ -132,7 +133,8 @@ public override async Task BaseRemoveAsync(string cacheKey)
132133
{
133134
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
134135

135-
await _memcachedClient.RemoveAsync(this.HandleCacheKey(cacheKey));
136+
var data = await _memcachedClient.ExecuteRemoveAsync(this.HandleCacheKey(cacheKey));
137+
CheckResult(data);
136138
}
137139

138140
/// <summary>
@@ -199,6 +201,7 @@ public override async Task BaseRemoveByPrefixAsync(string prefix)
199201
{
200202
newValue = string.Concat(newValue, new Random().Next(9).ToString());
201203
}
204+
202205
await _memcachedClient.StoreAsync(
203206
Enyim.Caching.Memcached.StoreMode.Set,
204207
this.HandleCacheKey(prefix),

src/EasyCaching.Memcached/DefaultMemcachedCachingProvider.cs

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
using EasyCaching.Core;
44
using EasyCaching.Core.DistributedLock;
5-
using EasyCaching.Memcached.DistributedLock;
5+
using EasyCaching.Memcached.DistributedLock;
66
using Microsoft.Extensions.Logging;
77
using System;
88
using System.Collections.Generic;
@@ -157,7 +157,11 @@ public override CacheValue<T> BaseGet<T>(string cacheKey)
157157
{
158158
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
159159

160-
var result = ConvertFromStoredValue<T>(_memcachedClient.Get(this.HandleCacheKey(cacheKey)));
160+
var data = _memcachedClient.PerformGet<object>(this.HandleCacheKey(cacheKey));
161+
162+
CheckResult(data);
163+
164+
var result = ConvertFromStoredValue<T>(data.Value);
161165

162166
if (result.HasValue)
163167
{
@@ -180,7 +184,9 @@ public override void BaseRemove(string cacheKey)
180184
{
181185
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
182186

183-
_memcachedClient.Remove(this.HandleCacheKey(cacheKey));
187+
var data = _memcachedClient.ExecuteRemove(this.HandleCacheKey(cacheKey));
188+
189+
CheckResult(data);
184190
}
185191

186192
/// <summary>
@@ -203,11 +209,13 @@ public override void BaseSet<T>(string cacheKey, T cacheValue, TimeSpan expirati
203209
expiration = expiration.Add(TimeSpan.FromSeconds(addSec));
204210
}
205211

206-
_memcachedClient.Store(
212+
var data = _memcachedClient.ExecuteStore(
207213
Enyim.Caching.Memcached.StoreMode.Set,
208214
this.HandleCacheKey(cacheKey),
209215
this.ConvertToStoredValue(cacheValue),
210216
expiration);
217+
218+
CheckResult(data);
211219
}
212220

213221
/// <summary>
@@ -246,11 +254,14 @@ public override void BaseRemoveByPrefix(string prefix)
246254
{
247255
newValue = string.Concat(newValue, new Random().Next(9).ToString());
248256
}
249-
_memcachedClient.Store(
250-
Enyim.Caching.Memcached.StoreMode.Set,
251-
this.HandleCacheKey(prefix),
252-
newValue,
253-
new TimeSpan(0, 0, 0));
257+
258+
var data = _memcachedClient.ExecuteStore(
259+
Enyim.Caching.Memcached.StoreMode.Set,
260+
this.HandleCacheKey(prefix),
261+
newValue,
262+
new TimeSpan(0, 0, 0));
263+
264+
CheckResult(data);
254265
}
255266

256267
/// <summary>
@@ -431,5 +442,13 @@ private void OnCacheMiss(string cacheKey)
431442
if (_options.EnableLogging)
432443
_logger?.LogInformation($"Cache Missed : cachekey = {cacheKey}");
433444
}
445+
446+
private void CheckResult(Enyim.Caching.Memcached.Results.IOperationResult data)
447+
{
448+
if (!data.Success
449+
&& (!data.InnerResult?.Success ?? false)
450+
&& (data.InnerResult?.Message?.Contains("Failed to create socket") ?? false))
451+
throw new EasyCachingException($"opereation fail, {data.InnerResult?.Message ?? ""}", data.Exception);
452+
}
434453
}
435454
}

test/EasyCaching.UnitTests/CachingTests/MemcachedProviderTest.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,4 +386,42 @@ public void Provider_Information_Should_Be_Correct()
386386
Assert.Equal("mName", _provider.Name);
387387
}
388388
}
389+
390+
public class MemcachedProviderNoConnectionTest
391+
{
392+
[Fact]
393+
public async void NoConnectionTest()
394+
{
395+
IServiceCollection services = new ServiceCollection();
396+
services.AddLogging();
397+
services.AddEasyCaching(option =>
398+
{
399+
option.UseMemcached(config =>
400+
{
401+
config.DBConfig = new EasyCachingMemcachedClientOptions
402+
{
403+
Servers = new System.Collections.Generic.List<Enyim.Caching.Configuration.Server>
404+
{
405+
new Enyim.Caching.Configuration.Server() {Address = "123.123.123.123", Port = 45678 }
406+
},
407+
SocketPool = new Enyim.Caching.Configuration.SocketPoolOptions
408+
{
409+
ConnectionTimeout = TimeSpan.FromSeconds(2),
410+
DeadTimeout = TimeSpan.FromSeconds(2),
411+
ReceiveTimeout = TimeSpan.FromSeconds(2),
412+
}
413+
};
414+
}, EasyCachingConstValue.DefaultMemcachedName);
415+
});
416+
417+
IServiceProvider serviceProvider = services.BuildServiceProvider();
418+
var factory = serviceProvider.GetService<IEasyCachingProviderFactory>();
419+
var provider = factory.GetCachingProvider(EasyCachingConstValue.DefaultMemcachedName);
420+
421+
Assert.Throws<EasyCachingException>(() => provider.Get<string>("123123"));
422+
await Assert.ThrowsAnyAsync<EasyCachingException>(() => provider.GetAsync<string>("123123"));
423+
Assert.Throws<EasyCachingException>(() => provider.Remove("123123"));
424+
await Assert.ThrowsAnyAsync<EasyCachingException>(() => provider.RemoveAsync("123123"));
425+
}
426+
}
389427
}

0 commit comments

Comments
 (0)