Skip to content

Commit

Permalink
Fixed failing delete by prefix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
niemyjski committed Sep 27, 2024
1 parent b2c9566 commit af8c978
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/Foundatio.TestHarness/Caching/CacheClientTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,22 @@ public virtual async Task CanRemoveByPrefixWithScopedCachesAsync()
// Add the unscoped cache value back.
await cache.SetAsync(cacheKey, 1);

// Remove by null key.
Assert.Equal(1, await scopedCache1.RemoveByPrefixAsync(null));
Assert.True(await cache.ExistsAsync(cacheKey));
Assert.False(await scopedCache1.ExistsAsync(cacheKey));

// Add the scoped cache value back.
await scopedCache1.SetAsync(cacheKey, 1);

Assert.Equal(2, await cache.RemoveByPrefixAsync(null));
Assert.False(await cache.ExistsAsync(cacheKey));
Assert.False(await scopedCache1.ExistsAsync(cacheKey));

// Reset client values
await cache.SetAsync(cacheKey, 1);
await scopedCache1.SetAsync(cacheKey, 1);

// Remove by empty key.
Assert.Equal(1, await scopedCache1.RemoveByPrefixAsync(String.Empty));
Assert.True(await cache.ExistsAsync(cacheKey));
Expand Down
3 changes: 2 additions & 1 deletion src/Foundatio/Caching/InMemoryCacheClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ public Task<int> RemoveAllAsync(IEnumerable<string> keys = null)
public Task<int> RemoveByPrefixAsync(string prefix)
{
var keysToRemove = new List<string>();
var regex = new Regex(String.Concat(prefix, "*").Replace("*", ".*").Replace("?", ".+"));
string normalizedPrefix = String.IsNullOrWhiteSpace(prefix) ? "*" : prefix.Trim();
var regex = new Regex(String.Concat("^", Regex.Escape(normalizedPrefix.Contains("*") ? normalizedPrefix : $"{normalizedPrefix}*"), "$").Replace("\\*", ".*?"));
try
{
foreach (string key in _memory.Keys.ToList())
Expand Down

0 comments on commit af8c978

Please sign in to comment.