From db20f0265fb573b64d6685f34246b5e44c8cc155 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A3=8A=E7=A3=8A?= Date: Tue, 9 Aug 2022 09:32:37 +0800 Subject: [PATCH] fix/redis: fix query error (#185) * feat: Add AppConfig * refactor: refactor AppConfig * feat: Initialize Framework configuration * feat(Configuration): Add Global Configuration * rename: Masa.EventBus.IntegrationTests rename to Masa.Framework.IntegrationTests.EventBus * refactor(Configuration): Adjust default variables and default values * chroe: delete extra space * fix(redis): fix query error --- .../RedisCacheClient.cs | 26 +++++++++++++++++-- .../DistributedCacheClientTest.cs | 18 +++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/Utils/Caching/Distributed/Masa.Utils.Caching.Redis/RedisCacheClient.cs b/src/Utils/Caching/Distributed/Masa.Utils.Caching.Redis/RedisCacheClient.cs index 535529b54..ad400bd01 100644 --- a/src/Utils/Caching/Distributed/Masa.Utils.Caching.Redis/RedisCacheClient.cs +++ b/src/Utils/Caching/Distributed/Masa.Utils.Caching.Redis/RedisCacheClient.cs @@ -367,7 +367,7 @@ public async Task> GetKeysAsync(string keyPattern) foreach (var redisResult in arrayRedisResult) { var byteArray = (RedisValue[])redisResult.Value; - MapMetadata(byteArray, out DateTimeOffset? absExpr, out TimeSpan? sldExpr, out RedisValue data); + MapMetadataByAutomatic(byteArray, out DateTimeOffset? absExpr, out TimeSpan? sldExpr, out RedisValue data); func.Invoke(redisResult.Key, absExpr, sldExpr); dictionary.Add(redisResult.Key, ConvertToValue(data)); } @@ -535,7 +535,29 @@ private void RefreshCore( } } - private static void MapMetadata(RedisValue[] results, out DateTimeOffset? absoluteExpiration, out TimeSpan? slidingExpiration, + private static void MapMetadata(RedisValue[] results, + out DateTimeOffset? absoluteExpiration, + out TimeSpan? slidingExpiration, + out RedisValue data) + { + absoluteExpiration = null; + slidingExpiration = null; + data = results.Length > 2 ? results[2] : RedisValue.Null; + var absoluteExpirationTicks = (long?)results[0]; + if (absoluteExpirationTicks.HasValue && absoluteExpirationTicks.Value != NOT_PRESENT) + { + absoluteExpiration = new DateTimeOffset(absoluteExpirationTicks.Value, TimeSpan.Zero); + } + + var slidingExpirationTicks = (long?)results[1]; + if (slidingExpirationTicks.HasValue && slidingExpirationTicks.Value != NOT_PRESENT) + { + slidingExpiration = new TimeSpan(slidingExpirationTicks.Value); + } + } + + private static void MapMetadataByAutomatic(RedisValue[] results, out DateTimeOffset? absoluteExpiration, + out TimeSpan? slidingExpiration, out RedisValue data) { absoluteExpiration = null; diff --git a/src/Utils/Caching/Distributed/Tests/Masa.Utils.Caching.Redis.Tests/DistributedCacheClientTest.cs b/src/Utils/Caching/Distributed/Tests/Masa.Utils.Caching.Redis.Tests/DistributedCacheClientTest.cs index 291f07dfc..cbfd7db5a 100644 --- a/src/Utils/Caching/Distributed/Tests/Masa.Utils.Caching.Redis.Tests/DistributedCacheClientTest.cs +++ b/src/Utils/Caching/Distributed/Tests/Masa.Utils.Caching.Redis.Tests/DistributedCacheClientTest.cs @@ -58,4 +58,22 @@ public async Task TestGetListByKeyPatternAsyncReturnCountIs1() Assert.AreEqual(1, dictionary.Count); Assert.IsTrue(dictionary["test1"] == "test1:Result"); } + + [DataTestMethod] + [DataRow("test1", "Result1")] + public async Task TestGetAsyncReturnResult1(string key, string value) + { + _cacheClient.Set(key, value); + var res = await _cacheClient.GetAsync(key); + Assert.AreEqual(value, res); + } + + [DataTestMethod] + [DataRow("test1", "Result1")] + public void TestGetReturnResult1(string key, string value) + { + _cacheClient.Set(key, value); + var res = _cacheClient.Get(key); + Assert.AreEqual(value, res); + } }