diff --git a/Enyim.Caching.Tests/MemcachedClientTestsBase.cs b/Enyim.Caching.Tests/MemcachedClientTestsBase.cs index 5437bb7c..c2615bcf 100644 --- a/Enyim.Caching.Tests/MemcachedClientTestsBase.cs +++ b/Enyim.Caching.Tests/MemcachedClientTestsBase.cs @@ -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 { @@ -23,10 +23,7 @@ public MemcachedClientTestsBase(Action onAddEnyimMemcach services.AddEnyimMemcached(options => { options.AddServer("memcached", 11211); - if (onAddEnyimMemcached != null) - { - onAddEnyimMemcached(options); - } + onAddEnyimMemcached?.Invoke(options); }); services.AddLogging(builder => builder.SetMinimumLevel(LogLevel.Debug).AddConsole()); diff --git a/Enyim.Caching.Tests/MemcachedClientWithKeyTransformerTests.cs b/Enyim.Caching.Tests/MemcachedClientWithKeyTransformerTests.cs index 2bbb623c..21ab9b51 100644 --- a/Enyim.Caching.Tests/MemcachedClientWithKeyTransformerTests.cs +++ b/Enyim.Caching.Tests/MemcachedClientWithKeyTransformerTests.cs @@ -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 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") { } diff --git a/Enyim.Caching/Configuration/AuthenticationConfiguration.cs b/Enyim.Caching/Configuration/AuthenticationConfiguration.cs index f604fa06..f504048e 100644 --- a/Enyim.Caching/Configuration/AuthenticationConfiguration.cs +++ b/Enyim.Caching/Configuration/AuthenticationConfiguration.cs @@ -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 parameters; + public class AuthenticationConfiguration : IAuthenticationConfiguration + { + private Type authenticator; + private Dictionary 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 IAuthenticationConfiguration.Parameters - { - get { return this.parameters ?? (this.parameters = new Dictionary()); } - } - } + Dictionary IAuthenticationConfiguration.Parameters + { + get { return this.parameters ?? (this.parameters = new Dictionary()); } + } + } } #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. diff --git a/Enyim.Caching/MemcachedClient.cs b/Enyim.Caching/MemcachedClient.cs index 8ccd3077..fc7a6cd5 100755 --- a/Enyim.Caching/MemcachedClient.cs +++ b/Enyim.Caching/MemcachedClient.cs @@ -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 { @@ -113,7 +113,6 @@ public async Task SetAsync(string key, object value, int cacheSeconds) /// /// The identifier for the item to retrieve. /// The retrieved item, or null if the key was not found. - [Obsolete] public object Get(string key) { object tmp; @@ -129,95 +128,85 @@ public object Get(string key) [Obsolete] public T Get(string key) { - var hashedKey = this.keyTransformer.Transform(key); - var node = this.pool.Locate(hashedKey); + var result = PerformGet(key); + return result.Success ? result.Value : default(T); + } - if (node != null) + public IGetOperationResult PerformGet(string key) + { + if (!CreateGetCommand(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(result, command, commandResult); - if (commandResult.Success) - { - if (typeof(T).GetTypeCode() == TypeCode.Object && typeof(T) != typeof(Byte[])) - { - return this.transcoder.Deserialize(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> GetAsync(string key) + private bool CreateGetCommand(string key, out IGetOperationResult result, out IMemcachedNode node, out IGetOperation command) { - var result = new DefaultGetOperationResultFactory().Create(); - + result = new DefaultGetOperationResultFactory().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(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 BuildGetCommandResult(IGetOperationResult result, IGetOperation command, IOperationResult commandResult) + { + if (commandResult.Success) + { + result.Value = transcoder.Deserialize(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> GetAsync(string key) + { + if (!CreateGetCommand(key, out var result, out var node, out var command)) + { + return result; + } + + try + { + var commandResult = await node.ExecuteAsync(command); + return BuildGetCommandResult(result, command, commandResult); + } + catch (Exception ex) + { + _logger.LogError(0, ex, $"{nameof(GetAsync)}(\"{key}\")"); + result.Fail(ex.Message); + return result; + } + } + public async Task GetValueAsync(string key) { var result = await GetAsync(key); @@ -246,7 +235,7 @@ public async Task GetValueOrCreateAsync(string key, int cacheSeconds, Func { await AddAsync(key, value, cacheSeconds); } - catch(Exception ex) + catch (Exception ex) { _logger.LogError(ex, $"{nameof(AddAsync)}(\"{key}\", ..., {cacheSeconds})"); } @@ -260,7 +249,6 @@ public async Task GetValueOrCreateAsync(string key, int cacheSeconds, Func /// The identifier for the item to retrieve. /// The retrieved item or null if not found. /// The true if the item was successfully retrieved. - [Obsolete] public bool TryGet(string key, out object value) { ulong cas = 0; @@ -268,13 +256,11 @@ public bool TryGet(string key, out object value) return this.PerformTryGet(key, out cas, out value).Success; } - [Obsolete] public CasResult GetWithCas(string key) { return this.GetWithCas(key); } - [Obsolete] public CasResult GetWithCas(string key) { CasResult tmp; @@ -284,7 +270,6 @@ public CasResult GetWithCas(string key) : new CasResult { Cas = tmp.Cas, Result = default(T) }; } - [Obsolete] public bool TryGetWithCas(string key, out CasResult value) { object tmp;