Skip to content

Commit 7f6da0c

Browse files
committed
Catch exception in GetValueOrCreateAsync of MemcachedClient
1 parent 2f6722b commit 7f6da0c

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

Enyim.Caching/Enyim.Caching.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<Description>EnyimMemcachedCore is a Memcached client library for .NET Core. Usage: Add services.AddEnyimMemcached(...) and app.UseEnyimMemcached() in Startup. Add IMemcachedClient into constructor.</Description>
5-
<VersionPrefix>2.1.5</VersionPrefix>
5+
<VersionPrefix>2.1.6</VersionPrefix>
66
<Authors>cnblogs.com</Authors>
77
<TargetFramework>netstandard2.0</TargetFramework>
88
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>

Enyim.Caching/MemcachedClient.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,16 +221,30 @@ public async Task<T> GetValueAsync<T>(string key)
221221

222222
public async Task<T> GetValueOrCreateAsync<T>(string key, int cacheSeconds, Func<Task<T>> generator)
223223
{
224-
var result = await GetAsync<T>(key);
225-
if (result.Success)
224+
try
225+
{
226+
var result = await GetAsync<T>(key);
227+
if (result.Success)
228+
{
229+
return result.Value;
230+
}
231+
}
232+
catch (Exception ex)
226233
{
227-
return result.Value;
234+
_logger.LogError(ex, $"{nameof(GetAsync)}<{typeof(T)}>(\"{key}\")");
228235
}
229236

230237
var value = await generator?.Invoke();
231238
if (value != null)
232239
{
233-
await AddAsync(key, value, cacheSeconds);
240+
try
241+
{
242+
await AddAsync(key, value, cacheSeconds);
243+
}
244+
catch(Exception ex)
245+
{
246+
_logger.LogError(ex, $"{nameof(AddAsync)}(\"{key}\", ..., {cacheSeconds})");
247+
}
234248
}
235249
return value;
236250
}

0 commit comments

Comments
 (0)