Skip to content

Commit 550cc6a

Browse files
authored
Add SuppressException option (#176)
1 parent 609d34c commit 550cc6a

File tree

5 files changed

+22
-3
lines changed

5 files changed

+22
-3
lines changed

Enyim.Caching/Configuration/IMemcachedClientConfiguration.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ public interface IMemcachedClientConfiguration
4242

4343
IServerPool CreatePool();
4444

45-
bool UseSslStream { get; }
45+
bool UseSslStream { get; }
4646

47+
bool SuppressException { get; }
4748
}
4849
}
4950

Enyim.Caching/Configuration/MemcachedClientConfiguration.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ public MemcachedClientConfiguration(
117117
}
118118

119119
UseSslStream = options.UseSslStream;
120+
SuppressException = options.SuppressException;
120121

121122
if (!string.IsNullOrEmpty(options.KeyTransformer))
122123
{
@@ -203,7 +204,7 @@ private void ConfigureServers(MemcachedClientOptions options)
203204
}
204205
else
205206
{
206-
_logger.LogInformation($"Memcached server address - {server.Address }:{server.Port}");
207+
_logger.LogInformation($"Memcached server address - {server.Address}:{server.Port}");
207208
}
208209

209210
Servers.Add(new IPEndPoint(address, server.Port));
@@ -338,6 +339,8 @@ IServerPool IMemcachedClientConfiguration.CreatePool()
338339

339340
public bool UseSslStream { get; private set; }
340341

342+
public bool SuppressException { get; private set; }
343+
341344
#endregion
342345
}
343346
}

Enyim.Caching/Configuration/MemcachedClientOptions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public class MemcachedClientOptions : IOptions<MemcachedClientOptions>
2323

2424
public bool UseSslStream { get; set; }
2525

26+
public bool SuppressException { get; set; } = true;
27+
2628
public IProviderFactory<IMemcachedNodeLocator> NodeLocatorFactory { get; set; }
2729

2830
public MemcachedClientOptions Value => this;

Enyim.Caching/MemcachedClient.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public partial class MemcachedClient : IMemcachedClient, IMemcachedResultsClient
2727
public static readonly TimeSpan Infinite = TimeSpan.Zero;
2828
//internal static readonly MemcachedClientSection DefaultSettings = ConfigurationManager.GetSection("enyim.com/memcached") as MemcachedClientSection;
2929
private ILogger<MemcachedClient> _logger;
30+
private bool _suppressException;
3031

3132
private IServerPool pool;
3233
private IMemcachedKeyTransformer keyTransformer;
@@ -51,6 +52,7 @@ public MemcachedClient(ILoggerFactory loggerFactory, IMemcachedClientConfigurati
5152
throw new ArgumentNullException(nameof(configuration));
5253
}
5354

55+
_suppressException = configuration.SuppressException;
5456
this.keyTransformer = configuration.CreateKeyTransformer() ?? new DefaultKeyTransformer();
5557
this.transcoder = configuration.CreateTranscoder() ?? new DefaultTranscoder();
5658

@@ -154,7 +156,10 @@ public IGetOperationResult<T> PerformGet<T>(string key)
154156
}
155157
catch (Exception ex)
156158
{
159+
157160
_logger.LogError(0, ex, $"{nameof(PerformGet)}(\"{key}\")");
161+
if (!_suppressException) throw;
162+
158163
result.Fail(ex.Message);
159164
return result;
160165
}
@@ -246,6 +251,8 @@ public async Task<IGetOperationResult> GetAsync(string key)
246251
catch (Exception ex)
247252
{
248253
_logger.LogError(0, ex, $"{nameof(GetAsync)}(\"{key}\")");
254+
if (!_suppressException) throw;
255+
249256
result.Fail(ex.Message, ex);
250257
return result;
251258
}
@@ -267,6 +274,8 @@ public async Task<IGetOperationResult<T>> GetAsync<T>(string key)
267274
catch (Exception ex)
268275
{
269276
_logger.LogError(0, ex, $"{nameof(GetAsync)}(\"{key}\")");
277+
if (!_suppressException) throw;
278+
270279
result.Fail(ex.Message, ex);
271280
return result;
272281
}
@@ -300,6 +309,7 @@ public async Task<T> GetValueOrCreateAsync<T>(string key, int cacheSeconds, Func
300309
catch (Exception ex)
301310
{
302311
_logger.LogError(ex, $"{nameof(AddAsync)}(\"{key}\", ..., {cacheSeconds})");
312+
if (!_suppressException) throw;
303313
}
304314
}
305315
return value;
@@ -594,6 +604,7 @@ protected virtual IStoreOperationResult PerformStore(StoreMode mode, string key,
594604
catch (Exception e)
595605
{
596606
_logger.LogError("PerformStore", e);
607+
if (!_suppressException) throw;
597608

598609
result.Fail("PerformStore failed", e);
599610
return result;
@@ -1221,6 +1232,7 @@ protected virtual IDictionary<string, T> PerformMultiGet<T>(IEnumerable<string>
12211232
catch (Exception e)
12221233
{
12231234
_logger.LogError(0, e, "PerformMultiGet");
1235+
if (!_suppressException) throw;
12241236
}
12251237
}));
12261238
}

SampleWebApp/appsettings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"receiveTimeout": "00:00:15",
1414
"deadTimeout": "00:00:15",
1515
"queueTimeout": "00:00:00.150"
16-
} //,
16+
},
17+
"suppressException": "false"
1718
//"Transcoder": "BinaryFormatterTranscoder"
1819
//,
1920
//"KeyTransformer": "Enyim.Caching.Memcached.SHA1KeyTransformer"

0 commit comments

Comments
 (0)