Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 44 additions & 9 deletions src/Enyim.Caching/Memcached/MemcachedNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,11 @@ public IPooledSocketResult Acquire()
var startTime = DateTime.Now;
_internalPoolImpl.InitPool();
_isInitialized = true;
_logger.LogInformation("MemcachedInitPool-cost: {0}ms", (DateTime.Now - startTime).TotalMilliseconds);

if (_logger.IsEnabled(LogLevel.Information))
{
_logger.LogInformation("MemcachedInitPool-cost: {0}ms", (DateTime.Now - startTime).TotalMilliseconds);
}
}
}
finally
Expand Down Expand Up @@ -204,7 +208,11 @@ public async Task<IPooledSocketResult> AcquireAsync()
var startTime = DateTime.Now;
await _internalPoolImpl.InitPoolAsync();
_isInitialized = true;
_logger.LogInformation("MemcachedInitPool-cost: {0}ms", (DateTime.Now - startTime).TotalMilliseconds);

if (_logger.IsEnabled(LogLevel.Information))
{
_logger.LogInformation("MemcachedInitPool-cost: {0}ms", (DateTime.Now - startTime).TotalMilliseconds);
}
}
}
finally
Expand Down Expand Up @@ -502,7 +510,12 @@ public IPooledSocketResult Acquire()
// okay, create the new item
var startTime = DateTime.Now;
socket = CreateSocket();
_logger.LogInformation("MemcachedAcquire-CreateSocket: {0}ms", (DateTime.Now - startTime).TotalMilliseconds);

if (_logger.IsEnabled(LogLevel.Information))
{
_logger.LogInformation("MemcachedAcquire-CreateSocket: {0}ms", (DateTime.Now - startTime).TotalMilliseconds);
}

result.Value = socket;
result.Pass();
}
Expand Down Expand Up @@ -627,7 +640,12 @@ public async Task<IPooledSocketResult> AcquireAsync()
// okay, create the new item
var startTime = DateTime.Now;
socket = await CreateSocketAsync();
_logger.LogInformation("MemcachedAcquire-CreateSocket: {0}ms", (DateTime.Now - startTime).TotalMilliseconds);

if (_logger.IsEnabled(LogLevel.Information))
{
_logger.LogInformation("MemcachedAcquire-CreateSocket: {0}ms", (DateTime.Now - startTime).TotalMilliseconds);
}

result.Value = socket;
result.Pass();
}
Expand Down Expand Up @@ -753,7 +771,11 @@ private bool TryPopPooledSocket(out PooledSocket pooledSocket)
{
try
{
_logger.LogInformation("Connection idle timeout {idleTimeout} reached.", _connectionIdleTimeout);
if (_logger.IsEnabled(LogLevel.Information))
{
_logger.LogInformation("Connection idle timeout {idleTimeout} reached.", _connectionIdleTimeout);
}

socket.Destroy();
}
catch (Exception ex)
Expand Down Expand Up @@ -919,7 +941,10 @@ protected virtual IPooledSocketResult ExecuteOperation(IOperation op)

protected virtual async Task<IPooledSocketResult> ExecuteOperationAsync(IOperation op)
{
_logger.LogDebug($"ExecuteOperationAsync({op})");
if (_logger.IsEnabled(LogLevel.Debug))
{
_logger.LogDebug($"ExecuteOperationAsync({op})");
}

var result = await AcquireAsync();
if (result.Success && result.HasValue)
Expand All @@ -931,7 +956,10 @@ protected virtual async Task<IPooledSocketResult> ExecuteOperationAsync(IOperati
//if Get, call BinaryRequest.CreateBuffer()
var b = op.GetBuffer();

_logger.LogDebug("pooledSocket.WriteAsync...");
if (_logger.IsEnabled(LogLevel.Debug))
{
_logger.LogDebug("pooledSocket.WriteAsync...");
}

var writeSocketTask = pooledSocket.WriteAsync(b);
if (await Task.WhenAny(writeSocketTask, Task.Delay(_config.ConnectionTimeout)) != writeSocketTask)
Expand All @@ -942,7 +970,10 @@ protected virtual async Task<IPooledSocketResult> ExecuteOperationAsync(IOperati
await writeSocketTask;

//if Get, call BinaryResponse
_logger.LogDebug($"{op}.ReadResponseAsync...");
if (_logger.IsEnabled(LogLevel.Debug))
{
_logger.LogDebug($"{op}.ReadResponseAsync...");
}

var readResponseTask = op.ReadResponseAsync(pooledSocket);
if (await Task.WhenAny(readResponseTask, Task.Delay(_config.ConnectionTimeout)) != readResponseTask)
Expand All @@ -958,7 +989,11 @@ protected virtual async Task<IPooledSocketResult> ExecuteOperationAsync(IOperati
}
else
{
_logger.LogInformation($"{op}.{nameof(op.ReadResponseAsync)} result: {readResult.Message}");
if (_logger.IsEnabled(LogLevel.Information))
{
_logger.LogInformation($"{op}.{nameof(op.ReadResponseAsync)} result: {readResult.Message}");
}

readResult.Combine(result);
}
return result;
Expand Down
29 changes: 24 additions & 5 deletions src/Enyim.Caching/MemcachedClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,11 @@ public async Task<IGetOperationResult> GetAsync(string key)
{
if (!CreateGetCommand(key, out var result, out var node, out var command))
{
_logger.LogInformation($"Failed to CreateGetCommand for '{key}' key");
if (_logger.IsEnabled(LogLevel.Information))
{
_logger.LogInformation($"Failed to CreateGetCommand for '{key}' key");
}

return result;
}

Expand All @@ -321,7 +325,11 @@ public async Task<IGetOperationResult<T>> GetAsync<T>(string key)
{
if (!CreateGetCommand<T>(key, out var result, out var node, out var command))
{
_logger.LogInformation($"Failed to CreateGetCommand for '{key}' key");
if (_logger.IsEnabled(LogLevel.Information))
{
_logger.LogInformation($"Failed to CreateGetCommand for '{key}' key");
}

return result;
}

Expand Down Expand Up @@ -351,19 +359,30 @@ public async Task<T> GetValueOrCreateAsync<T>(string key, int cacheSeconds, Func
var result = await GetAsync<T>(key);
if (result.Success)
{
_logger.LogDebug($"Cache is hint. Key is '{key}'.");
if (_logger.IsEnabled(LogLevel.Debug))
{
_logger.LogDebug($"Cache is hint. Key is '{key}'.");
}

return result.Value;
}

_logger.LogDebug($"Cache is missed. Key is '{key}'.");
if (_logger.IsEnabled(LogLevel.Debug))
{
_logger.LogDebug($"Cache is missed. Key is '{key}'.");
}

var value = await generator?.Invoke();
if (value != null)
{
try
{
await AddAsync(key, value, cacheSeconds);
_logger.LogDebug($"Added value into cache. Key is '{key}'. " + value);

if (_logger.IsEnabled(LogLevel.Debug))
{
_logger.LogDebug($"Added value into cache. Key is '{key}'. " + value);
}
}
catch (Exception ex)
{
Expand Down