From 08fd57d40e56a146fcf88d7579d88761c6a40f9a Mon Sep 17 00:00:00 2001 From: catcherwong Date: Thu, 1 Feb 2018 19:26:48 +0800 Subject: [PATCH 1/4] :white_check_mark: Add RemoveByPrefix(Async) tests for SQLite Caching provider --- .../CachingTests/SQLiteCachingTest.cs | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/test/EasyCaching.UnitTests/CachingTests/SQLiteCachingTest.cs b/test/EasyCaching.UnitTests/CachingTests/SQLiteCachingTest.cs index de66b72f..11ab9b01 100644 --- a/test/EasyCaching.UnitTests/CachingTests/SQLiteCachingTest.cs +++ b/test/EasyCaching.UnitTests/CachingTests/SQLiteCachingTest.cs @@ -166,5 +166,59 @@ public async Task Refresh_Async_Should_Succeed() Assert.Equal("NewValue", act.Value); } + + [Fact] + public void RemoveByPrefix_Should_Succeed() + { + var cacheKey1 = "PREFIX:1"; + var cacheValue1 = "value1"; + _provider.Set(cacheKey1, cacheValue1, _defaultTs); + + var cacheKey2 = "PREFIX:2"; + var cacheValue2 = "value2"; + _provider.Set(cacheKey2, cacheValue2, _defaultTs); + + + var tmpRes1 = _provider.Get(cacheKey1); + var tmpRes2 = _provider.Get(cacheKey2); + + Assert.Equal(cacheValue1, tmpRes1.Value); + Assert.Equal(cacheValue2, tmpRes2.Value); + + _provider.RemoveByPrefix("PREFIX"); + + var res1 = _provider.Get(cacheKey1); + var res2 = _provider.Get(cacheKey2); + + Assert.False(res1.HasValue); + Assert.False(res2.HasValue); + } + + [Fact] + public async Task RemoveByPrefixAsync_Should_Succeed() + { + var cacheKey1 = "PREFIX:1"; + var cacheValue1 = "value1"; + await _provider.SetAsync(cacheKey1, cacheValue1, _defaultTs); + + var cacheKey2 = "PREFIX:2"; + var cacheValue2 = "value2"; + await _provider.SetAsync(cacheKey2, cacheValue2, _defaultTs); + + + var tmpRes1 = await _provider.GetAsync(cacheKey1); + var tmpRes2 = await _provider.GetAsync(cacheKey2); + + Assert.Equal(cacheValue1, tmpRes1.Value); + Assert.Equal(cacheValue2, tmpRes2.Value); + + await _provider.RemoveByPrefixAsync("PREFIX"); + + var res1 = await _provider.GetAsync(cacheKey1); + var res2 = await _provider.GetAsync(cacheKey2); + + Assert.False(res1.HasValue); + Assert.False(res2.HasValue); + } } } From 9c722819f85004389927aab42876860c5ca96885 Mon Sep 17 00:00:00 2001 From: catcherwong Date: Thu, 1 Feb 2018 19:44:57 +0800 Subject: [PATCH 2/4] :white_check_mark: Add RemoveByPrefix(Async) tests for Redis Caching provider --- .../CachingTests/RedisCachingProviderTest.cs | 57 ++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/test/EasyCaching.UnitTests/CachingTests/RedisCachingProviderTest.cs b/test/EasyCaching.UnitTests/CachingTests/RedisCachingProviderTest.cs index c4ac67db..46428e39 100644 --- a/test/EasyCaching.UnitTests/CachingTests/RedisCachingProviderTest.cs +++ b/test/EasyCaching.UnitTests/CachingTests/RedisCachingProviderTest.cs @@ -299,6 +299,61 @@ public async Task Refresh_Async_Should_Succeed() Assert.Equal("NewValue", act.Value); } - + + + [Fact] + public void RemoveByPrefix_Should_Succeed() + { + SetCacheItem("demo:1", "1"); + SetCacheItem("demo:2", "2"); + SetCacheItem("demo:3", "3"); + SetCacheItem("demo:4", "4"); + + _provider.RemoveByPrefix("demo"); + + var demo1 = _provider.Get("demo:1"); + var demo2 = _provider.Get("demo:2"); + var demo3 = _provider.Get("demo:3"); + var demo4 = _provider.Get("demo:4"); + + Assert.False(demo1.HasValue); + Assert.False(demo2.HasValue); + Assert.False(demo3.HasValue); + Assert.False(demo4.HasValue); + } + + [Fact] + public async Task RemoveByPrefixAsync_Should_Succeed() + { + SetCacheItem("demo:1", "1"); + SetCacheItem("demo:2", "2"); + SetCacheItem("demo:3", "3"); + SetCacheItem("demo:4", "4"); + + await _provider.RemoveByPrefixAsync("demo"); + + var demo1 = _provider.Get("demo:1"); + var demo2 = _provider.Get("demo:2"); + var demo3 = _provider.Get("demo:3"); + var demo4 = _provider.Get("demo:4"); + + Assert.False(demo1.HasValue); + Assert.False(demo2.HasValue); + Assert.False(demo3.HasValue); + Assert.False(demo4.HasValue); + } + + private void SetCacheItem(string cacheKey, string cacheValue) + { + var cacheBytes = new byte[] { 0x01 }; + + A.CallTo(() => _serializer.Serialize(cacheValue)).Returns(cacheBytes); + A.CallTo(() => _serializer.Deserialize(A.Ignored)).Returns(cacheValue); + + _provider.Set(cacheKey, cacheValue, _defaultTs); + + var val = _provider.Get(cacheKey); + Assert.Equal(cacheValue, val.Value); + } } } From fdc615c5a78870ce744d3cf6caa401cd3e92471e Mon Sep 17 00:00:00 2001 From: catcherwong Date: Thu, 1 Feb 2018 19:47:57 +0800 Subject: [PATCH 3/4] :bug: Fix bug for prefix handle in redis caching provider. --- .../DefaultRedisCachingProvider.cs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/EasyCaching.Redis/DefaultRedisCachingProvider.cs b/src/EasyCaching.Redis/DefaultRedisCachingProvider.cs index 94886aee..9e8e55a4 100644 --- a/src/EasyCaching.Redis/DefaultRedisCachingProvider.cs +++ b/src/EasyCaching.Redis/DefaultRedisCachingProvider.cs @@ -140,7 +140,7 @@ public CacheValue Get(string cacheKey) where T : class else { return CacheValue.NoValue; - } + } } /// @@ -162,7 +162,7 @@ public async Task> GetAsync(string cacheKey) where T : class else { return CacheValue.NoValue; - } + } } /// @@ -251,7 +251,7 @@ public async Task ExistsAsync(string cacheKey) ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); return await _cache.KeyExistsAsync(cacheKey); - } + } /// /// Refresh the specified cacheKey, cacheValue and expiration. @@ -312,7 +312,7 @@ public async Task RemoveByPrefixAsync(string prefix) { ArgumentCheck.NotNullOrWhiteSpace(prefix, nameof(prefix)); - this.HandlePrefix(prefix); + prefix = this.HandlePrefix(prefix); foreach (var server in _servers) { @@ -368,28 +368,26 @@ private async Task HandleKeyDelWithTranAsync(IServer server, string prefix) } while (count <= 0); } - + /// /// Handles the prefix of CacheKey. /// /// Prefix of CacheKey. /// - private void HandlePrefix(string prefix) + private string HandlePrefix(string prefix) { // Forbid if (prefix.Equals("*")) - { throw new ArgumentException("the prefix should not to *"); - } // Don't start with * prefix = new System.Text.RegularExpressions.Regex("^\\*+").Replace(prefix, ""); // End with * if (!prefix.EndsWith("*", StringComparison.OrdinalIgnoreCase)) - { prefix = string.Concat(prefix, "*"); - } + + return prefix; } } } From 4da5dfbb0e3b7ff641b6eca8b8ea50e29b6404ee Mon Sep 17 00:00:00 2001 From: catcherwong Date: Fri, 2 Feb 2018 22:38:22 +0800 Subject: [PATCH 4/4] :bug: Fix RemoveByPrefixAsync for Redis Caching Provider. --- src/EasyCaching.Redis/DefaultRedisCachingProvider.cs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/EasyCaching.Redis/DefaultRedisCachingProvider.cs b/src/EasyCaching.Redis/DefaultRedisCachingProvider.cs index 9e8e55a4..0fa3ef78 100644 --- a/src/EasyCaching.Redis/DefaultRedisCachingProvider.cs +++ b/src/EasyCaching.Redis/DefaultRedisCachingProvider.cs @@ -296,7 +296,7 @@ public void RemoveByPrefix(string prefix) { ArgumentCheck.NotNullOrWhiteSpace(prefix, nameof(prefix)); - this.HandlePrefix(prefix); + prefix = this.HandlePrefix(prefix); foreach (var server in _servers) { @@ -361,9 +361,15 @@ private async Task HandleKeyDelWithTranAsync(IServer server, string prefix) { var tran = _cache.CreateTransaction(); - await tran.KeyDeleteAsync(keys.ToArray()); + Task delTask = tran.KeyDeleteAsync(keys.ToArray()); + + Task execTask = tran.ExecuteAsync(CommandFlags.FireAndForget); + + await Task.WhenAll(delTask, execTask); + + //await tran.KeyDeleteAsync(keys.ToArray()); - await tran.ExecuteAsync(CommandFlags.FireAndForget); + //await tran.ExecuteAsync(CommandFlags.FireAndForget); } } while (count <= 0);