diff --git a/src/EasyCaching.Redis/DefaultRedisCachingProvider.cs b/src/EasyCaching.Redis/DefaultRedisCachingProvider.cs index 94886aee..0fa3ef78 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. @@ -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) { @@ -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) { @@ -361,35 +361,39 @@ 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); } - + /// /// 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; } } } 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); + } } } 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); + } } }