Skip to content

Commit

Permalink
Removed unused lua script and extra remove call.
Browse files Browse the repository at this point in the history
  • Loading branch information
niemyjski committed Jan 3, 2024
1 parent ec1d2ed commit d1ca126
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 26 deletions.
25 changes: 9 additions & 16 deletions src/Foundatio.Redis/Cache/RedisCacheClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,28 +85,21 @@ public async Task<int> RemoveAllAsync(IEnumerable<string> keys = null) {
}

public async Task<int> 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;
}

/// <summary>
Expand Down
6 changes: 0 additions & 6 deletions src/Foundatio.Redis/Scripts/RemoveByPrefix.lua

This file was deleted.

8 changes: 4 additions & 4 deletions tests/Foundatio.Redis.Tests/Caching/RedisCacheClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(prefix + "test" + 1)).Value);
Assert.Equal(1, (await cache.GetAsync<int>($"{prefix}test{1}")).Value);
Assert.Equal(1, (await cache.GetAsync<int>("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));
}
}
Expand Down

0 comments on commit d1ca126

Please sign in to comment.