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
21 changes: 9 additions & 12 deletions Enyim.Caching.Tests/MemcachedClientTestsBase.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Enyim.Caching.Configuration;
using Enyim.Caching.Memcached.Results;
using Enyim.Caching.Configuration;
using Enyim.Caching.Memcached;
using Xunit;
using Microsoft.Extensions.DependencyInjection;
using Enyim.Caching.Memcached.Results;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace Enyim.Caching.Tests
{
Expand All @@ -23,10 +23,7 @@ public MemcachedClientTestsBase(Action<MemcachedClientOptions> onAddEnyimMemcach
services.AddEnyimMemcached(options =>
{
options.AddServer("memcached", 11211);
if (onAddEnyimMemcached != null)
{
onAddEnyimMemcached(options);
}
onAddEnyimMemcached?.Invoke(options);
});

services.AddLogging(builder => builder.SetMinimumLevel(LogLevel.Debug).AddConsole());
Expand Down
17 changes: 5 additions & 12 deletions Enyim.Caching.Tests/MemcachedClientWithKeyTransformerTests.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
using System;
using Enyim.Caching.Configuration;
using Enyim.Caching.Memcached;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Enyim.Caching.Configuration;
using Enyim.Caching.Memcached;
using Xunit;

namespace Enyim.Caching.Tests
{
public class MemcachedClientWithKeyTransformerTests : MemcachedClientTestsBase
{
public MemcachedClientWithKeyTransformerTests(Action<MemcachedClientOptions> onAddEnyimMemcached = null)
: base(options =>
{
options.KeyTransformer = "Enyim.Caching.Memcached.TigerHashKeyTransformer";
if (onAddEnyimMemcached != null)
{
onAddEnyimMemcached(options);
}
})
public MemcachedClientWithKeyTransformerTests()
: base(options => options.KeyTransformer = "Enyim.Caching.Memcached.TigerHashKeyTransformer")
{
}

Expand Down
40 changes: 20 additions & 20 deletions Enyim.Caching/Configuration/AuthenticationConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
using Enyim.Caching.Memcached;
using System;
using System.Collections.Generic;
using Enyim.Caching.Memcached;

namespace Enyim.Caching.Configuration
{
public class AuthenticationConfiguration : IAuthenticationConfiguration
{
private Type authenticator;
private Dictionary<string, object> parameters;
public class AuthenticationConfiguration : IAuthenticationConfiguration
{
private Type authenticator;
private Dictionary<string, object> parameters;

Type IAuthenticationConfiguration.Type
{
get { return this.authenticator; }
set
{
ConfigurationHelper.CheckForInterface(value, typeof(ISaslAuthenticationProvider));
this.authenticator = value;
}
}
Type IAuthenticationConfiguration.Type
{
get { return this.authenticator; }
set
{
ConfigurationHelper.CheckForInterface(value, typeof(ISaslAuthenticationProvider));
this.authenticator = value;
}
}

Dictionary<string, object> IAuthenticationConfiguration.Parameters
{
get { return this.parameters ?? (this.parameters = new Dictionary<string, object>()); }
}
}
Dictionary<string, object> IAuthenticationConfiguration.Parameters
{
get { return this.parameters ?? (this.parameters = new Dictionary<string, object>()); }
}
}
}

#region [ License information ]
/* ************************************************************
*
* Copyright (c) 2010 Attila Kisk�, enyim.com
* Copyright (c) 2010 Attila Kisk? enyim.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
151 changes: 68 additions & 83 deletions Enyim.Caching/MemcachedClient.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
using System;
using System.Linq;
using Enyim.Caching.Configuration;
using Enyim.Caching.Configuration;
using Enyim.Caching.Memcached;
using System.Collections.Generic;
using System.Threading;
using System.Net;
using System.Diagnostics;
using Enyim.Caching.Memcached.Results;
using Enyim.Caching.Memcached.Results.Factories;
using Enyim.Caching.Memcached.Results.Extensions;
using System.Threading.Tasks;
using Enyim.Caching.Memcached.Results.Factories;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Caching.Distributed;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;

namespace Enyim.Caching
{
Expand Down Expand Up @@ -113,7 +113,6 @@ public async Task SetAsync(string key, object value, int cacheSeconds)
/// </summary>
/// <param name="key">The identifier for the item to retrieve.</param>
/// <returns>The retrieved item, or <value>null</value> if the key was not found.</returns>
[Obsolete]
public object Get(string key)
{
object tmp;
Expand All @@ -129,95 +128,85 @@ public object Get(string key)
[Obsolete]
public T Get<T>(string key)
{
var hashedKey = this.keyTransformer.Transform(key);
var node = this.pool.Locate(hashedKey);
var result = PerformGet<T>(key);
return result.Success ? result.Value : default(T);
}

if (node != null)
public IGetOperationResult<T> PerformGet<T>(string key)
{
if (!CreateGetCommand<T>(key, out var result, out var node, out var command))
{
try
{
var command = this.pool.OperationFactory.Get(hashedKey);
var commandResult = node.Execute(command);
return result;
}

try
{
var commandResult = node.Execute(command);
return BuildGetCommandResult<T>(result, command, commandResult);

if (commandResult.Success)
{
if (typeof(T).GetTypeCode() == TypeCode.Object && typeof(T) != typeof(Byte[]))
{
return this.transcoder.Deserialize<T>(command.Result);
}
else
{
var tempResult = this.transcoder.Deserialize(command.Result);
if (tempResult != null)
{
if (typeof(T) == typeof(Guid))
{
return (T)(object)new Guid((string)tempResult);
}
else
{
return (T)tempResult;
}
}
}
}
}
catch (Exception ex)
{
_logger.LogError(0, ex, $"{nameof(GetAsync)}(\"{key}\")");
throw ex;
}
}
else
catch (Exception ex)
{
_logger.LogError($"Unable to locate memcached node");
_logger.LogError(0, ex, $"{nameof(PerformGet)}(\"{key}\")");
result.Fail(ex.Message);
return result;
}

return default(T);
}

public async Task<IGetOperationResult<T>> GetAsync<T>(string key)
private bool CreateGetCommand<T>(string key, out IGetOperationResult<T> result, out IMemcachedNode node, out IGetOperation command)
{
var result = new DefaultGetOperationResultFactory<T>().Create();

result = new DefaultGetOperationResultFactory<T>().Create();
var hashedKey = this.keyTransformer.Transform(key);
var node = this.pool.Locate(hashedKey);

if (node != null)
node = this.pool.Locate(hashedKey);
if (node == null)
{
try
{
var command = this.pool.OperationFactory.Get(hashedKey);
var commandResult = await node.ExecuteAsync(command);
var errorMessage = $"Unable to locate node with \"{key}\" key";
_logger.LogError(errorMessage);
result.Fail(errorMessage);
command = null;
return false;
}

if (commandResult.Success)
{
result.Success = true;
result.Value = transcoder.Deserialize<T>(command.Result);
return result;
}
else
{
commandResult.Combine(result);
command = this.pool.OperationFactory.Get(hashedKey);
return true;
}

return result;
}
}
catch (Exception ex)
{
_logger.LogError(0, ex, $"{nameof(GetAsync)}(\"{key}\")");
throw ex;
}
private IGetOperationResult<T> BuildGetCommandResult<T>(IGetOperationResult<T> result, IGetOperation command, IOperationResult commandResult)
{
if (commandResult.Success)
{
result.Value = transcoder.Deserialize<T>(command.Result);
result.Pass();
}
else
{
_logger.LogError($"Unable to locate memcached node");
commandResult.Combine(result);
}

result.Fail("Unable to locate node");
return result;
}

public async Task<IGetOperationResult<T>> GetAsync<T>(string key)
{
if (!CreateGetCommand<T>(key, out var result, out var node, out var command))
{
return result;
}

try
{
var commandResult = await node.ExecuteAsync(command);
return BuildGetCommandResult<T>(result, command, commandResult);
}
catch (Exception ex)
{
_logger.LogError(0, ex, $"{nameof(GetAsync)}(\"{key}\")");
result.Fail(ex.Message);
return result;
}
}

public async Task<T> GetValueAsync<T>(string key)
{
var result = await GetAsync<T>(key);
Expand Down Expand Up @@ -246,7 +235,7 @@ public async Task<T> GetValueOrCreateAsync<T>(string key, int cacheSeconds, Func
{
await AddAsync(key, value, cacheSeconds);
}
catch(Exception ex)
catch (Exception ex)
{
_logger.LogError(ex, $"{nameof(AddAsync)}(\"{key}\", ..., {cacheSeconds})");
}
Expand All @@ -260,21 +249,18 @@ public async Task<T> GetValueOrCreateAsync<T>(string key, int cacheSeconds, Func
/// <param name="key">The identifier for the item to retrieve.</param>
/// <param name="value">The retrieved item or null if not found.</param>
/// <returns>The <value>true</value> if the item was successfully retrieved.</returns>
[Obsolete]
public bool TryGet(string key, out object value)
{
ulong cas = 0;

return this.PerformTryGet(key, out cas, out value).Success;
}

[Obsolete]
public CasResult<object> GetWithCas(string key)
{
return this.GetWithCas<object>(key);
}

[Obsolete]
public CasResult<T> GetWithCas<T>(string key)
{
CasResult<object> tmp;
Expand All @@ -284,7 +270,6 @@ public CasResult<T> GetWithCas<T>(string key)
: new CasResult<T> { Cas = tmp.Cas, Result = default(T) };
}

[Obsolete]
public bool TryGetWithCas(string key, out CasResult<object> value)
{
object tmp;
Expand Down