From 8843fbe5f1b872277f0e38eef1605b8ca7105202 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B2=88=E6=A3=B5?= <542611769@qq.com> Date: Wed, 10 Jul 2019 19:25:05 +0800 Subject: [PATCH] improve TryGet TryGetWithCas ExecuteTryGet support --- .../MemcachedClientGetTests.cs | 13 ++++ Enyim.Caching/IMemcachedClient.cs | 2 + Enyim.Caching/IMemcachedResultsClient.cs | 1 + Enyim.Caching/MemcachedClient.Results.cs | 29 ++++++--- Enyim.Caching/MemcachedClient.cs | 62 ++++++++++++++++++- Enyim.Caching/MemcachedClientT.cs | 10 +++ Enyim.Caching/NullMemcachedClient.cs | 12 ++++ 7 files changed, 118 insertions(+), 11 deletions(-) diff --git a/Enyim.Caching.Tests/MemcachedClientGetTests.cs b/Enyim.Caching.Tests/MemcachedClientGetTests.cs index 4bb09803..e267bf75 100644 --- a/Enyim.Caching.Tests/MemcachedClientGetTests.cs +++ b/Enyim.Caching.Tests/MemcachedClientGetTests.cs @@ -45,6 +45,19 @@ public void When_TryGetting_Existing_Item_Value_Is_Not_Null_And_Result_Is_Succes GetAssertPass(getResult, temp); } + [Fact] + public void When_Generic_TryGetting_Existing_Item_Value_Is_Not_Null_And_Result_Is_Successful() + { + var key = GetUniqueKey("get"); + var value = GetRandomString(); + var storeResult = Store(key: key, value: value); + StoreAssertPass(storeResult); + + string temp; + var getResult = _client.ExecuteTryGet(key, out temp); + GetAssertPass(getResult, temp); + } + [Fact] public void When_Generic_Getting_Existing_Item_Value_Is_Not_Null_And_Result_Is_Successful() { diff --git a/Enyim.Caching/IMemcachedClient.cs b/Enyim.Caching/IMemcachedClient.cs index 1a873916..9560fd1d 100644 --- a/Enyim.Caching/IMemcachedClient.cs +++ b/Enyim.Caching/IMemcachedClient.cs @@ -26,7 +26,9 @@ public interface IMemcachedClient : IDisposable Task> GetAsync(IEnumerable keys); bool TryGet(string key, out object value); + bool TryGet(string key, out T value); bool TryGetWithCas(string key, out CasResult value); + bool TryGetWithCas(string key, out CasResult value); CasResult GetWithCas(string key); CasResult GetWithCas(string key); diff --git a/Enyim.Caching/IMemcachedResultsClient.cs b/Enyim.Caching/IMemcachedResultsClient.cs index cc85b327..c8dde01f 100644 --- a/Enyim.Caching/IMemcachedResultsClient.cs +++ b/Enyim.Caching/IMemcachedResultsClient.cs @@ -18,6 +18,7 @@ public interface IMemcachedResultsClient IDictionary ExecuteGet(IEnumerable keys); IGetOperationResult ExecuteTryGet(string key, out object value); + IGetOperationResult ExecuteTryGet(string key, out T value); IStoreOperationResult ExecuteStore(StoreMode mode, string key, object value); IStoreOperationResult ExecuteStore(StoreMode mode, string key, object value, DateTime expiresAt); diff --git a/Enyim.Caching/MemcachedClient.Results.cs b/Enyim.Caching/MemcachedClient.Results.cs index 9e58764b..eebf1afd 100644 --- a/Enyim.Caching/MemcachedClient.Results.cs +++ b/Enyim.Caching/MemcachedClient.Results.cs @@ -145,16 +145,29 @@ public IGetOperationResult ExecuteTryGet(string key, out object value) ulong cas = 0; return this.PerformTryGet(key, out cas, out value); - } + } - /// - /// Retrieves the specified item from the cache. - /// - /// The identifier for the item to retrieve. - /// The retrieved item, or default(T) if the key was not found. - public IGetOperationResult ExecuteGet(string key) + /// + /// Tries to get an item from the cache. + /// + /// The identifier for the item to retrieve. + /// The retrieved item or null if not found. + /// The true if the item was successfully retrieved. + public IGetOperationResult ExecuteTryGet(string key, out T value) + { + ulong cas = 0; + + return this.PerformTryGet(key, out cas, out value); + } + + /// + /// Retrieves the specified item from the cache. + /// + /// The identifier for the item to retrieve. + /// The retrieved item, or default(T) if the key was not found. + public IGetOperationResult ExecuteGet(string key) { - object tmp; + T tmp; var result = new DefaultGetOperationResultFactory().Create(); var tryGetResult = ExecuteTryGet(key, out tmp); diff --git a/Enyim.Caching/MemcachedClient.cs b/Enyim.Caching/MemcachedClient.cs index 4eead3d5..37df55a4 100755 --- a/Enyim.Caching/MemcachedClient.cs +++ b/Enyim.Caching/MemcachedClient.cs @@ -261,6 +261,19 @@ public bool TryGet(string key, out object value) return this.PerformTryGet(key, out cas, out value).Success; } + /// + /// Tries to get an item from the cache. + /// + /// The identifier for the item to retrieve. + /// The retrieved item or null if not found. + /// The true if the item was successfully retrieved. + public bool TryGet(string key, out T value) + { + ulong cas = 0; + + return this.PerformTryGet(key, out cas, out value).Success; + } + public CasResult GetWithCas(string key) { return this.GetWithCas(key); @@ -268,11 +281,11 @@ public CasResult GetWithCas(string key) public CasResult GetWithCas(string key) { - CasResult tmp; + CasResult tmp; return this.TryGetWithCas(key, out tmp) - ? new CasResult { Cas = tmp.Cas, Result = (T)tmp.Result } - : new CasResult { Cas = tmp.Cas, Result = default(T) }; + ? new CasResult { Cas = tmp.Cas, Result = tmp.Result } + : new CasResult { Cas = tmp.Cas, Result = default }; } public bool TryGetWithCas(string key, out CasResult value) @@ -287,6 +300,15 @@ public bool TryGetWithCas(string key, out CasResult value) return retval.Success; } + public bool TryGetWithCas(string key, out CasResult value) + { + var retVal = PerformTryGet(key, out var cas, out T tmp); + + value = new CasResult { Cas = cas, Result = tmp }; + + return retVal.Success; + } + protected virtual IGetOperationResult PerformTryGet(string key, out ulong cas, out object value) { var hashedKey = this.keyTransformer.Transform(key); @@ -323,6 +345,40 @@ protected virtual IGetOperationResult PerformTryGet(string key, out ulong cas, o return result; } + protected virtual IGetOperationResult PerformTryGet(string key, out ulong cas, out T value) + { + var hashedKey = keyTransformer.Transform(key); + var node = pool.Locate(hashedKey); + var result = GetOperationResultFactory.Create(); + + cas = 0; + value = default; + + if (node != null) + { + var command = pool.OperationFactory.Get(hashedKey); + var commandResult = node.Execute(command); + + if (commandResult.Success) + { + result.Value = value = transcoder.Deserialize(command.Result); + result.Cas = cas = command.CasValue; + + result.Pass(); + return result; + } + + commandResult.Combine(result); + return result; + } + + result.Value = value; + result.Cas = cas; + + result.Fail("Unable to locate node"); + return result; + } + #region [ Store ] diff --git a/Enyim.Caching/MemcachedClientT.cs b/Enyim.Caching/MemcachedClientT.cs index 2b870789..3853a640 100644 --- a/Enyim.Caching/MemcachedClientT.cs +++ b/Enyim.Caching/MemcachedClientT.cs @@ -280,9 +280,19 @@ public bool TryGet(string key, out object value) return _memcachedClient.TryGet(key, out value); } + public bool TryGet(string key, out T1 value) + { + return _memcachedClient.TryGet(key, out value); + } + public bool TryGetWithCas(string key, out CasResult value) { return _memcachedClient.TryGetWithCas(key, out value); } + + public bool TryGetWithCas(string key, out CasResult value) + { + return _memcachedClient.TryGetWithCas(key, out value); + } } } diff --git a/Enyim.Caching/NullMemcachedClient.cs b/Enyim.Caching/NullMemcachedClient.cs index f690e656..22d6721d 100644 --- a/Enyim.Caching/NullMemcachedClient.cs +++ b/Enyim.Caching/NullMemcachedClient.cs @@ -228,12 +228,24 @@ public bool TryGet(string key, out object value) return false; } + public bool TryGet(string key, out T value) + { + value = default; + return false; + } + public bool TryGetWithCas(string key, out CasResult value) { value = new CasResult(); return false; } + public bool TryGetWithCas(string key, out CasResult value) + { + value = new CasResult(); + return false; + } + public bool Add(string key, object value, int cacheSeconds) { return true;