Skip to content

Commit 989f2ac

Browse files
committed
fix: memcached operation result check
1 parent 1c2961b commit 989f2ac

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

src/EasyCaching.Memcached/DefaultMemcachedCachingProvider.Async.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ public override async Task<CacheValue<T>> BaseGetAsync<T>(string cacheKey)
7878
else
7979
{
8080
OnCacheMiss(cacheKey);
81-
throw new EasyCachingException($"opereation fail {result.Message}", result.Exception);
81+
CheckResult(result);
82+
return CacheValue<T>.NoValue;
8283
}
8384
}
8485

@@ -133,7 +134,7 @@ public override async Task BaseRemoveAsync(string cacheKey)
133134
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
134135

135136
var data = await _memcachedClient.ExecuteRemoveAsync(this.HandleCacheKey(cacheKey));
136-
if(!data.Success) throw new EasyCachingException($"opereation fail {data.Message}", data.Exception);
137+
CheckResult(data);
137138
}
138139

139140
/// <summary>

src/EasyCaching.Memcached/DefaultMemcachedCachingProvider.cs

Lines changed: 14 additions & 6 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,9 +157,9 @@ public override CacheValue<T> BaseGet<T>(string cacheKey)
157157
{
158158
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
159159

160-
var data = _memcachedClient.PerformGet<T>(this.HandleCacheKey(cacheKey));
160+
var data = _memcachedClient.PerformGet<object>(this.HandleCacheKey(cacheKey));
161161

162-
if (!data.Success) throw new EasyCachingException($"opereation fail {data.Message}", data.Exception);
162+
CheckResult(data);
163163

164164
var result = ConvertFromStoredValue<T>(data.Value);
165165

@@ -186,7 +186,7 @@ public override void BaseRemove(string cacheKey)
186186

187187
var data = _memcachedClient.ExecuteRemove(this.HandleCacheKey(cacheKey));
188188

189-
if (!data.Success) throw new EasyCachingException($"opereation fail {data.Message}", data.Exception);
189+
CheckResult(data);
190190
}
191191

192192
/// <summary>
@@ -215,7 +215,7 @@ public override void BaseSet<T>(string cacheKey, T cacheValue, TimeSpan expirati
215215
this.ConvertToStoredValue(cacheValue),
216216
expiration);
217217

218-
if (!data.Success) throw new EasyCachingException($"opereation fail {data.Message}", data.Exception);
218+
CheckResult(data);
219219
}
220220

221221
/// <summary>
@@ -261,7 +261,7 @@ public override void BaseRemoveByPrefix(string prefix)
261261
newValue,
262262
new TimeSpan(0, 0, 0));
263263

264-
if (!data.Success) throw new EasyCachingException($"opereation fail {data.Message}", data.Exception);
264+
CheckResult(data);
265265
}
266266

267267
/// <summary>
@@ -442,5 +442,13 @@ private void OnCacheMiss(string cacheKey)
442442
if (_options.EnableLogging)
443443
_logger?.LogInformation($"Cache Missed : cachekey = {cacheKey}");
444444
}
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+
}
445453
}
446454
}

test/EasyCaching.UnitTests/CachingTests/MemcachedProviderTest.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ public void Provider_Information_Should_Be_Correct()
390390
public class MemcachedProviderNoConnectionTest
391391
{
392392
[Fact]
393-
public void NoConnectionTest()
393+
public async void NoConnectionTest()
394394
{
395395
IServiceCollection services = new ServiceCollection();
396396
services.AddLogging();
@@ -419,6 +419,9 @@ public void NoConnectionTest()
419419
var provider = factory.GetCachingProvider(EasyCachingConstValue.DefaultMemcachedName);
420420

421421
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"));
422425
}
423426
}
424427
}

0 commit comments

Comments
 (0)