@@ -86,11 +86,14 @@ private CacheValue GetCacheValue(TRequest request)
8686 /// <param name="request">
8787 /// The request payload to be cached.
8888 /// </param>
89+ /// <param name="cancellationToken">
90+ /// The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.
91+ /// </param>
8992 /// <returns>
9093 /// The response payload from executing the handler.
9194 /// </returns>
92- public ValueTask < TResponse > GetValue ( TRequest request ) =>
93- GetCacheValue ( request ) . GetValue ( ) ;
95+ public ValueTask < TResponse > GetValue ( TRequest request , CancellationToken cancellationToken = default ) =>
96+ GetCacheValue ( request ) . GetValue ( cancellationToken ) ;
9497
9598 /// <summary>
9699 /// Sets the value for a particular cache entry, bypassing the execution of the handler.
@@ -124,9 +127,9 @@ Owned<IHandler<TRequest, TResponse>> handler
124127 private TaskCompletionSource < TResponse > ? _responseSource ;
125128 private readonly Lock _lock = new ( ) ;
126129
127- public async ValueTask < TResponse > GetValue ( )
130+ public async ValueTask < TResponse > GetValue ( CancellationToken cancellationToken )
128131 {
129- if ( ! TryAcquireResponseSource ( ) )
132+ if ( ! TryAcquireResponseSource ( cancellationToken ) )
130133 return await _responseSource . Task . ConfigureAwait ( false ) ;
131134
132135 var token = _tokenSource . Token ;
@@ -151,7 +154,7 @@ public async ValueTask<TResponse> GetValue()
151154 }
152155 }
153156 }
154- catch ( OperationCanceledException ) when ( _responseSource is not null )
157+ catch ( OperationCanceledException ) when ( ! cancellationToken . IsCancellationRequested && _responseSource is not null )
155158 {
156159 return await _responseSource . Task . ConfigureAwait ( false ) ;
157160 }
@@ -160,7 +163,7 @@ public async ValueTask<TResponse> GetValue()
160163 [ MemberNotNull ( nameof ( _responseSource ) ) ]
161164 [ MemberNotNullWhen ( true , nameof ( _tokenSource ) ) ]
162165 [ SuppressMessage ( "Maintainability" , "CA1508:Avoid dead conditional code" , Justification = "Double-checked lock pattern" ) ]
163- private bool TryAcquireResponseSource ( )
166+ private bool TryAcquireResponseSource ( CancellationToken cancellationToken )
164167 {
165168 if ( _responseSource is not null )
166169 return false ;
@@ -170,7 +173,7 @@ private bool TryAcquireResponseSource()
170173 if ( _responseSource is not null )
171174 return false ;
172175
173- _tokenSource = new CancellationTokenSource ( ) ;
176+ _tokenSource = CancellationTokenSource . CreateLinkedTokenSource ( cancellationToken ) ;
174177 _responseSource = new TaskCompletionSource < TResponse > ( ) ;
175178 return true ;
176179 }
0 commit comments