Skip to content

Commit 3003237

Browse files
committed
unify key handling
1 parent 8143189 commit 3003237

File tree

1 file changed

+9
-11
lines changed
  • src/libraries/Microsoft.Extensions.Caching.Abstractions/src/Hybrid

1 file changed

+9
-11
lines changed

src/libraries/Microsoft.Extensions.Caching.Abstractions/src/Hybrid/HybridCache.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ public ValueTask<T> GetOrCreateAsync<T>(
104104
IEnumerable<string>? tags = null,
105105
CancellationToken cancellationToken = default)
106106
{
107+
// It is *not* an error that this Clear occurs before the "await"; by definition, the implementation is *required* to copy
108+
// the value locally before an await, precisely because the ref-struct cannot bridge an await. Thus: we are fine to clean
109+
// the buffer even in the non-synchronous completion scenario.
107110
ValueTask<T> result = GetOrCreateAsync(key.Text, factory, WrappedCallbackCache<T>.Instance, options, tags, cancellationToken);
108111
key.Clear();
109112
return result;
@@ -129,17 +132,12 @@ public ValueTask<T> GetOrCreateAsync<TState, T>(
129132
IEnumerable<string>? tags = null,
130133
CancellationToken cancellationToken = default)
131134
{
132-
try
133-
{
134-
return GetOrCreateAsync(key.Text, state, factory, options, tags, cancellationToken);
135-
}
136-
finally
137-
{
138-
// It is *not* an error that this Clear occurs before the "await"; by definition, the implementation is *required* to copy
139-
// the value locally before an await, precisely because the ref-struct cannot bridge an await. Thus: we are fine to clean
140-
// the buffer even in the non-synchronous completion scenario.
141-
key.Clear();
142-
}
135+
// It is *not* an error that this Clear occurs before the "await"; by definition, the implementation is *required* to copy
136+
// the value locally before an await, precisely because the ref-struct cannot bridge an await. Thus: we are fine to clean
137+
// the buffer even in the non-synchronous completion scenario.
138+
ValueTask<T> result = GetOrCreateAsync(key.Text, state, factory, options, tags, cancellationToken);
139+
key.Clear();
140+
return result;
143141
}
144142
#endif
145143

0 commit comments

Comments
 (0)