-
Notifications
You must be signed in to change notification settings - Fork 41
Add GetAsyncAlternateLookup and TryGetAsyncAlternateLookup methods to IAsyncCache #751
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a2642ac
Add GetAsyncAlternateLookup and TryGetAsyncAlternateLookup to IAsyncC…
Copilot 7944598
Implement GetAsyncAlternateLookup/TryGetAsyncAlternateLookup in Atomi…
Copilot 2ef7671
Merge main into copilot/add-getasyncalternatelookup-methods
Copilot 92725a6
Fix GetOrAddAsync to preserve exactly-once initialization via AsyncAt…
Copilot a449d7a
Merge branch 'main' into copilot/add-getasyncalternatelookup-methods
bitfaster 7af9ef6
Merge branch 'main' of https://github.com/bitfaster/BitFaster.Caching…
Copilot 88f9d4e
Replace KeyBox key capture with comparer.Create() for alternate key t…
Copilot afac179
improve getoradd
9e6f9e0
Merge branch 'main' into copilot/add-getasyncalternatelookup-methods
bitfaster 6769e42
Merge branch 'main' into copilot/add-getasyncalternatelookup-methods
bitfaster c459f99
Merge branch 'main' into copilot/add-getasyncalternatelookup-methods
bitfaster File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
162 changes: 162 additions & 0 deletions
162
BitFaster.Caching.UnitTests/Atomic/AtomicFactoryAsyncCacheAsyncAlternateLookupTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| #if NET9_0_OR_GREATER | ||
| using System; | ||
| using System.Threading.Tasks; | ||
| using BitFaster.Caching.Atomic; | ||
| using BitFaster.Caching.Lru; | ||
| using FluentAssertions; | ||
| using Xunit; | ||
|
|
||
| namespace BitFaster.Caching.UnitTests.Atomic | ||
| { | ||
| public class AtomicFactoryAsyncCacheAsyncAlternateLookupTests | ||
| { | ||
| private readonly AtomicFactoryAsyncCache<string, string> cache; | ||
|
|
||
| public AtomicFactoryAsyncCacheAsyncAlternateLookupTests() | ||
| { | ||
| var innerCache = new ConcurrentLru<string, AsyncAtomicFactory<string, string>>(1, 9, StringComparer.Ordinal); | ||
| cache = new AtomicFactoryAsyncCache<string, string>(innerCache); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TryGetAsyncAlternateLookupReturnsLookupForCompatibleComparer() | ||
| { | ||
| cache.AddOrUpdate("42", "value"); | ||
| ReadOnlySpan<char> key = "42"; | ||
|
|
||
| cache.TryGetAsyncAlternateLookup<ReadOnlySpan<char>>(out var alternate).Should().BeTrue(); | ||
| alternate.TryGet(key, out var value).Should().BeTrue(); | ||
| value.Should().Be("value"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GetAsyncAlternateLookupThrowsForIncompatibleComparer() | ||
| { | ||
| Action act = () => cache.GetAsyncAlternateLookup<int>(); | ||
|
|
||
| act.Should().Throw<InvalidOperationException>().WithMessage("Incompatible comparer"); | ||
| cache.TryGetAsyncAlternateLookup<int>(out var alternate).Should().BeFalse(); | ||
| alternate.Should().BeNull(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void AsyncAlternateLookupTryGetReturnsFalseForMissingKey() | ||
| { | ||
| var alternate = cache.GetAsyncAlternateLookup<ReadOnlySpan<char>>(); | ||
| ReadOnlySpan<char> key = "42"; | ||
|
|
||
| alternate.TryGet(key, out _).Should().BeFalse(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void AsyncAlternateLookupTryRemoveReturnsActualKeyAndValue() | ||
| { | ||
| cache.AddOrUpdate("42", "value"); | ||
| var alternate = cache.GetAsyncAlternateLookup<ReadOnlySpan<char>>(); | ||
| ReadOnlySpan<char> key = "42"; | ||
|
|
||
| alternate.TryRemove(key, out var actualKey, out var value).Should().BeTrue(); | ||
|
|
||
| actualKey.Should().Be("42"); | ||
| value.Should().Be("value"); | ||
| cache.TryGet("42", out _).Should().BeFalse(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void AsyncAlternateLookupTryRemoveReturnsFalseForMissingKey() | ||
| { | ||
| var alternate = cache.GetAsyncAlternateLookup<ReadOnlySpan<char>>(); | ||
| ReadOnlySpan<char> key = "42"; | ||
|
|
||
| alternate.TryRemove(key, out _, out _).Should().BeFalse(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void AsyncAlternateLookupTryUpdateReturnsFalseForMissingKeyAndUpdatesExistingValue() | ||
| { | ||
| var alternate = cache.GetAsyncAlternateLookup<ReadOnlySpan<char>>(); | ||
| ReadOnlySpan<char> key = "42"; | ||
|
|
||
| alternate.TryUpdate(key, "value-42").Should().BeFalse(); | ||
| cache.TryGet("42", out _).Should().BeFalse(); | ||
|
|
||
| cache.AddOrUpdate("42", "value-42"); | ||
| alternate.TryUpdate(key, "updated").Should().BeTrue(); | ||
|
|
||
| cache.TryGet("42", out var value).Should().BeTrue(); | ||
| value.Should().Be("updated"); | ||
| alternate.TryGet(key, out value).Should().BeTrue(); | ||
| value.Should().Be("updated"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void AsyncAlternateLookupAddOrUpdateAddsMissingValueAndUpdatesExistingValue() | ||
| { | ||
| var alternate = cache.GetAsyncAlternateLookup<ReadOnlySpan<char>>(); | ||
| ReadOnlySpan<char> key = "42"; | ||
|
|
||
| alternate.AddOrUpdate(key, "value-42"); | ||
|
|
||
| cache.TryGet("42", out var value).Should().BeTrue(); | ||
| value.Should().Be("value-42"); | ||
|
|
||
| alternate.AddOrUpdate(key, "updated"); | ||
|
|
||
| cache.TryGet("42", out value).Should().BeTrue(); | ||
| value.Should().Be("updated"); | ||
| alternate.TryGet(key, out value).Should().BeTrue(); | ||
| value.Should().Be("updated"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task AsyncAlternateLookupGetOrAddAsyncUsesActualKeyOnMissAndHit() | ||
| { | ||
| var alternate = cache.GetAsyncAlternateLookup<ReadOnlySpan<char>>(); | ||
| var factoryCalls = 0; | ||
|
|
||
| var result = await alternate.GetOrAddAsync("42".AsSpan(), key => | ||
| { | ||
| factoryCalls++; | ||
| return Task.FromResult($"value-{key}"); | ||
| }); | ||
| result.Should().Be("value-42"); | ||
|
|
||
| result = await alternate.GetOrAddAsync("42".AsSpan(), key => | ||
| { | ||
| factoryCalls++; | ||
| return Task.FromResult("unused"); | ||
| }); | ||
| result.Should().Be("value-42"); | ||
|
|
||
| factoryCalls.Should().Be(1); | ||
| cache.TryGet("42", out var value).Should().BeTrue(); | ||
| value.Should().Be("value-42"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task AsyncAlternateLookupGetOrAddAsyncWithArgUsesActualKeyOnMissAndHit() | ||
| { | ||
| var alternate = cache.GetAsyncAlternateLookup<ReadOnlySpan<char>>(); | ||
| var factoryCalls = 0; | ||
|
|
||
| var result = await alternate.GetOrAddAsync("42".AsSpan(), (key, prefix) => | ||
| { | ||
| factoryCalls++; | ||
| return Task.FromResult($"{prefix}{key}"); | ||
| }, "value-"); | ||
| result.Should().Be("value-42"); | ||
|
|
||
| result = await alternate.GetOrAddAsync("42".AsSpan(), (key, prefix) => | ||
| { | ||
| factoryCalls++; | ||
| return Task.FromResult("unused"); | ||
| }, "unused-"); | ||
| result.Should().Be("value-42"); | ||
|
|
||
| factoryCalls.Should().Be(1); | ||
| cache.TryGet("42", out var value).Should().BeTrue(); | ||
| value.Should().Be("value-42"); | ||
| } | ||
| } | ||
| } | ||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.