Skip to content

Commit 4f60149

Browse files
committed
PR feedback; be less defensive:
- demand GetAlternateLookup works on the target runtimes - rely on documented "out set to default on failure" lookup behavior
1 parent 28e7ad0 commit 4f60149

File tree

1 file changed

+4
-14
lines changed

1 file changed

+4
-14
lines changed

src/libraries/Microsoft.Extensions.Caching.Memory/src/MemoryCache.cs

+4-14
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,7 @@ public bool TryGetValue(object key, out object? result)
211211
DateTime utcNow = UtcNow;
212212

213213
CoherentState coherentState = _coherentState; // Clear() can update the reference in the meantime
214-
if (!coherentState.TryGetValue(key, out CacheEntry? entry))
215-
{
216-
entry = null;
217-
}
214+
coherentState.TryGetValue(key, out CacheEntry? entry); // note we rely on documented "default when fails" contract re the out
218215
return PostProcessTryGetValue(coherentState, utcNow, entry, out result);
219216
}
220217

@@ -234,10 +231,7 @@ public bool TryGetValue(ReadOnlySpan<char> key, out object? result)
234231
DateTime utcNow = UtcNow;
235232

236233
CoherentState coherentState = _coherentState; // Clear() can update the reference in the meantime
237-
if (!coherentState.TryGetValue(key, out CacheEntry? entry))
238-
{
239-
entry = null;
240-
}
234+
coherentState.TryGetValue(key, out CacheEntry? entry); // note we rely on documented "default when fails" contract re the out
241235
return PostProcessTryGetValue(coherentState, utcNow, entry, out result);
242236
}
243237

@@ -738,14 +732,11 @@ private sealed class CoherentState
738732
private readonly ConcurrentDictionary<object, CacheEntry> _nonStringEntries = new ConcurrentDictionary<object, CacheEntry>();
739733

740734
#if NET9_0_OR_GREATER
741-
private readonly bool _useStringAltLookup;
742735
private readonly ConcurrentDictionary<string, CacheEntry>.AlternateLookup<ReadOnlySpan<char>> _stringAltLookup;
743736

744737
public CoherentState()
745738
{
746-
_useStringAltLookup = _stringEntries.TryGetAlternateLookup<ReadOnlySpan<char>>(out _stringAltLookup);
747-
// we *expect* this to be available in all scenarios where this is used, but add a dev guard, and a fallback
748-
Debug.Assert(_useStringAltLookup, "Expectation failure: alt-lookup feature is not available");
739+
_stringAltLookup = _stringEntries.GetAlternateLookup<ReadOnlySpan<char>>();
749740
}
750741
#endif
751742

@@ -756,8 +747,7 @@ internal bool TryGetValue(object key, [NotNullWhen(true)] out CacheEntry? entry)
756747

757748
#if NET9_0_OR_GREATER
758749
internal bool TryGetValue(ReadOnlySpan<char> key, [NotNullWhen(true)] out CacheEntry? entry)
759-
=> _useStringAltLookup ? _stringAltLookup.TryGetValue(key, out entry)
760-
: _stringEntries.TryGetValue(new string(key), out entry); // <== we do not expect this path to be hit; chaos fallback only
750+
=> _stringAltLookup.TryGetValue(key, out entry);
761751
#endif
762752

763753

0 commit comments

Comments
 (0)