Skip to content

Commit

Permalink
Add SuppressException option (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
cnblogs-dudu authored Nov 24, 2022
1 parent 609d34c commit 550cc6a
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Enyim.Caching/Configuration/IMemcachedClientConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ public interface IMemcachedClientConfiguration

IServerPool CreatePool();

bool UseSslStream { get; }
bool UseSslStream { get; }

bool SuppressException { get; }
}
}

Expand Down
5 changes: 4 additions & 1 deletion Enyim.Caching/Configuration/MemcachedClientConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public MemcachedClientConfiguration(
}

UseSslStream = options.UseSslStream;
SuppressException = options.SuppressException;

if (!string.IsNullOrEmpty(options.KeyTransformer))
{
Expand Down Expand Up @@ -203,7 +204,7 @@ private void ConfigureServers(MemcachedClientOptions options)
}
else
{
_logger.LogInformation($"Memcached server address - {server.Address }:{server.Port}");
_logger.LogInformation($"Memcached server address - {server.Address}:{server.Port}");
}

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

public bool UseSslStream { get; private set; }

public bool SuppressException { get; private set; }

#endregion
}
}
Expand Down
2 changes: 2 additions & 0 deletions Enyim.Caching/Configuration/MemcachedClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class MemcachedClientOptions : IOptions<MemcachedClientOptions>

public bool UseSslStream { get; set; }

public bool SuppressException { get; set; } = true;

public IProviderFactory<IMemcachedNodeLocator> NodeLocatorFactory { get; set; }

public MemcachedClientOptions Value => this;
Expand Down
12 changes: 12 additions & 0 deletions Enyim.Caching/MemcachedClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public partial class MemcachedClient : IMemcachedClient, IMemcachedResultsClient
public static readonly TimeSpan Infinite = TimeSpan.Zero;
//internal static readonly MemcachedClientSection DefaultSettings = ConfigurationManager.GetSection("enyim.com/memcached") as MemcachedClientSection;
private ILogger<MemcachedClient> _logger;
private bool _suppressException;

private IServerPool pool;
private IMemcachedKeyTransformer keyTransformer;
Expand All @@ -51,6 +52,7 @@ public MemcachedClient(ILoggerFactory loggerFactory, IMemcachedClientConfigurati
throw new ArgumentNullException(nameof(configuration));
}

_suppressException = configuration.SuppressException;
this.keyTransformer = configuration.CreateKeyTransformer() ?? new DefaultKeyTransformer();
this.transcoder = configuration.CreateTranscoder() ?? new DefaultTranscoder();

Expand Down Expand Up @@ -154,7 +156,10 @@ public IGetOperationResult<T> PerformGet<T>(string key)
}
catch (Exception ex)
{

_logger.LogError(0, ex, $"{nameof(PerformGet)}(\"{key}\")");
if (!_suppressException) throw;

result.Fail(ex.Message);
return result;
}
Expand Down Expand Up @@ -246,6 +251,8 @@ public async Task<IGetOperationResult> GetAsync(string key)
catch (Exception ex)
{
_logger.LogError(0, ex, $"{nameof(GetAsync)}(\"{key}\")");
if (!_suppressException) throw;

result.Fail(ex.Message, ex);
return result;
}
Expand All @@ -267,6 +274,8 @@ public async Task<IGetOperationResult<T>> GetAsync<T>(string key)
catch (Exception ex)
{
_logger.LogError(0, ex, $"{nameof(GetAsync)}(\"{key}\")");
if (!_suppressException) throw;

result.Fail(ex.Message, ex);
return result;
}
Expand Down Expand Up @@ -300,6 +309,7 @@ public async Task<T> GetValueOrCreateAsync<T>(string key, int cacheSeconds, Func
catch (Exception ex)
{
_logger.LogError(ex, $"{nameof(AddAsync)}(\"{key}\", ..., {cacheSeconds})");
if (!_suppressException) throw;
}
}
return value;
Expand Down Expand Up @@ -594,6 +604,7 @@ protected virtual IStoreOperationResult PerformStore(StoreMode mode, string key,
catch (Exception e)
{
_logger.LogError("PerformStore", e);
if (!_suppressException) throw;

result.Fail("PerformStore failed", e);
return result;
Expand Down Expand Up @@ -1221,6 +1232,7 @@ protected virtual IDictionary<string, T> PerformMultiGet<T>(IEnumerable<string>
catch (Exception e)
{
_logger.LogError(0, e, "PerformMultiGet");
if (!_suppressException) throw;
}
}));
}
Expand Down
3 changes: 2 additions & 1 deletion SampleWebApp/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"receiveTimeout": "00:00:15",
"deadTimeout": "00:00:15",
"queueTimeout": "00:00:00.150"
} //,
},
"suppressException": "false"
//"Transcoder": "BinaryFormatterTranscoder"
//,
//"KeyTransformer": "Enyim.Caching.Memcached.SHA1KeyTransformer"
Expand Down

0 comments on commit 550cc6a

Please sign in to comment.