Skip to content

Commit b2c9566

Browse files
committed
Added test to show off issue with DeleteByPrefix not respecting cache scopes
1 parent 40f70a8 commit b2c9566

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

src/Foundatio.TestHarness/Caching/CacheClientTestsBase.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,62 @@ public virtual async Task CanRemoveByPrefixAsync()
384384
}
385385
}
386386

387+
public virtual async Task CanRemoveByPrefixWithScopedCachesAsync()
388+
{
389+
var cache = GetCacheClient();
390+
if (cache == null)
391+
return;
392+
393+
using (cache)
394+
{
395+
await cache.RemoveAllAsync();
396+
var scopedCache1 = new ScopedCacheClient(cache, "scoped1");
397+
398+
const string cacheKey = "key";
399+
await cache.SetAsync(cacheKey, 1);
400+
await scopedCache1.SetAsync(cacheKey, 1);
401+
Assert.Equal(1, (await cache.GetAsync<int>(cacheKey)).Value);
402+
Assert.Equal(1, (await scopedCache1.GetAsync<int>(cacheKey)).Value);
403+
404+
// Remove by prefix should only remove the unscoped cache.
405+
Assert.Equal(1, await cache.RemoveByPrefixAsync(cacheKey));
406+
Assert.False(await cache.ExistsAsync(cacheKey));
407+
Assert.True(await scopedCache1.ExistsAsync(cacheKey));
408+
Assert.Equal(1, (await scopedCache1.GetAsync<int>(cacheKey)).Value);
409+
410+
// Add the unscoped cache value back.
411+
await cache.SetAsync(cacheKey, 1);
412+
413+
// Remove by empty key.
414+
Assert.Equal(1, await scopedCache1.RemoveByPrefixAsync(String.Empty));
415+
Assert.True(await cache.ExistsAsync(cacheKey));
416+
Assert.False(await scopedCache1.ExistsAsync(cacheKey));
417+
418+
// Add the scoped cache value back.
419+
await scopedCache1.SetAsync(cacheKey, 1);
420+
421+
Assert.Equal(2, await cache.RemoveByPrefixAsync(String.Empty));
422+
Assert.False(await cache.ExistsAsync(cacheKey));
423+
Assert.False(await scopedCache1.ExistsAsync(cacheKey));
424+
425+
// Reset client values
426+
await cache.SetAsync(cacheKey, 1);
427+
await scopedCache1.SetAsync(cacheKey, 1);
428+
429+
// Remove by *.
430+
Assert.Equal(1, await scopedCache1.RemoveByPrefixAsync("*"));
431+
Assert.True(await cache.ExistsAsync(cacheKey));
432+
Assert.False(await scopedCache1.ExistsAsync(cacheKey));
433+
434+
// Reset client values
435+
await scopedCache1.SetAsync(cacheKey, 1);
436+
437+
Assert.Equal(2, await cache.RemoveByPrefixAsync("*"));
438+
Assert.False(await cache.ExistsAsync(cacheKey));
439+
Assert.False(await scopedCache1.ExistsAsync(cacheKey));
440+
}
441+
}
442+
387443
public virtual async Task CanRemoveByPrefixMultipleEntriesAsync(int count)
388444
{
389445
var cache = GetCacheClient();

src/Foundatio.TestHarness/Caching/HybridCacheClientTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ public override Task CanRemoveByPrefixAsync()
8787
return base.CanRemoveByPrefixAsync();
8888
}
8989

90+
[Fact]
91+
public override Task CanRemoveByPrefixWithScopedCachesAsync()
92+
{
93+
return base.CanRemoveByPrefixWithScopedCachesAsync();
94+
}
95+
9096
[Theory]
9197
[InlineData(50)]
9298
[InlineData(500)]

tests/Foundatio.Tests/Caching/InMemoryCacheClientTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ public override Task CanRemoveByPrefixAsync()
8383
return base.CanRemoveByPrefixAsync();
8484
}
8585

86+
[Fact]
87+
public override Task CanRemoveByPrefixWithScopedCachesAsync()
88+
{
89+
return base.CanRemoveByPrefixWithScopedCachesAsync();
90+
}
91+
8692
[Theory]
8793
[InlineData(50)]
8894
[InlineData(500)]

tests/Foundatio.Tests/Caching/InMemoryHybridCacheClientTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ public override Task CanRemoveByPrefixAsync()
4040
return base.CanRemoveByPrefixAsync();
4141
}
4242

43+
[Fact]
44+
public override Task CanRemoveByPrefixWithScopedCachesAsync()
45+
{
46+
return base.CanRemoveByPrefixWithScopedCachesAsync();
47+
}
48+
4349
[Theory]
4450
[InlineData(50)]
4551
[InlineData(500)]

0 commit comments

Comments
 (0)