From d1ca12637b8eb645f50f11a5bfb0f7ae4df31906 Mon Sep 17 00:00:00 2001 From: Blake Niemyjski Date: Wed, 3 Jan 2024 05:01:54 -0600 Subject: [PATCH] Removed unused lua script and extra remove call. --- src/Foundatio.Redis/Cache/RedisCacheClient.cs | 25 +++++++------------ .../Scripts/RemoveByPrefix.lua | 6 ----- .../Caching/RedisCacheClientTests.cs | 8 +++--- 3 files changed, 13 insertions(+), 26 deletions(-) delete mode 100644 src/Foundatio.Redis/Scripts/RemoveByPrefix.lua diff --git a/src/Foundatio.Redis/Cache/RedisCacheClient.cs b/src/Foundatio.Redis/Cache/RedisCacheClient.cs index c7b586d..4a43e6f 100644 --- a/src/Foundatio.Redis/Cache/RedisCacheClient.cs +++ b/src/Foundatio.Redis/Cache/RedisCacheClient.cs @@ -85,28 +85,21 @@ public async Task RemoveAllAsync(IEnumerable keys = null) { } public async Task RemoveByPrefixAsync(string prefix) { - await LoadScriptsAsync().AnyContext(); - const int chunkSize = 2500; - string regex = prefix + "*"; - try { - int total = 0; - int index = 0; + string regex = $"{prefix}*"; - (int cursor, string[] keys) = await ScanKeysAsync(regex, index, chunkSize).AnyContext(); + int total = 0; + int index = 0; - while (keys.Length != 0 || index < chunkSize) { - total += await RemoveAllAsync(keys).AnyContext(); - index += chunkSize; - (cursor, keys) = await ScanKeysAsync(regex, cursor, chunkSize).AnyContext(); - } + (int cursor, string[] keys) = await ScanKeysAsync(regex, index, chunkSize).AnyContext(); + while (keys.Length != 0 || index < chunkSize) { total += await RemoveAllAsync(keys).AnyContext(); - - return total; - } catch (RedisServerException) { - return 0; + index += chunkSize; + (cursor, keys) = await ScanKeysAsync(regex, cursor, chunkSize).AnyContext(); } + + return total; } /// diff --git a/src/Foundatio.Redis/Scripts/RemoveByPrefix.lua b/src/Foundatio.Redis/Scripts/RemoveByPrefix.lua deleted file mode 100644 index cedccca..0000000 --- a/src/Foundatio.Redis/Scripts/RemoveByPrefix.lua +++ /dev/null @@ -1,6 +0,0 @@ -local remove = redis.call('keys', @keys) -if unpack(remove) ~= nil then - return redis.call('del', unpack(remove)) -else - return 0 -end \ No newline at end of file diff --git a/tests/Foundatio.Redis.Tests/Caching/RedisCacheClientTests.cs b/tests/Foundatio.Redis.Tests/Caching/RedisCacheClientTests.cs index 9d15f5a..d2647d3 100644 --- a/tests/Foundatio.Redis.Tests/Caching/RedisCacheClientTests.cs +++ b/tests/Foundatio.Redis.Tests/Caching/RedisCacheClientTests.cs @@ -87,15 +87,15 @@ public virtual async Task CanRemoveByPrefixMultipleEntriesAsync(int count) { using (cache) { await cache.RemoveAllAsync(); - const string prefix = "blah:"; + const string prefix = "prefix:"; await cache.SetAsync("test", 1); - await cache.SetAllAsync(Enumerable.Range(0, count).ToDictionary(i => prefix + "test" + i)); + await cache.SetAllAsync(Enumerable.Range(0, count).ToDictionary(i => $"{prefix}test{i}")); - Assert.Equal(1, (await cache.GetAsync(prefix + "test" + 1)).Value); + Assert.Equal(1, (await cache.GetAsync($"{prefix}test{1}")).Value); Assert.Equal(1, (await cache.GetAsync("test")).Value); - Assert.Equal(0, await cache.RemoveByPrefixAsync(prefix + ":doesntexist")); + Assert.Equal(0, await cache.RemoveByPrefixAsync($"{prefix}:doesntexist")); Assert.Equal(count, await cache.RemoveByPrefixAsync(prefix)); } }