Skip to content

Commit 93432bd

Browse files
KahbaziJohn Luo
authored andcommitted
Drop IResponseCache async methods (#15393)
1 parent 774f8db commit 93432bd

File tree

6 files changed

+56
-116
lines changed

6 files changed

+56
-116
lines changed
Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
@@ -16,29 +16,12 @@ internal interface IResponseCache
1616
/// <returns>The response cache entry if it exists; otherwise <c>null</c>.</returns>
1717
IResponseCacheEntry Get(string key);
1818

19-
/// <summary>
20-
/// Gets the cached response for the given key, if it exists.
21-
/// If no cached response exists for the given key, <c>null</c> is returned.
22-
/// </summary>
23-
/// <param name="key">The cache key to look up.</param>
24-
/// <returns>The response cache entry if it exists; otherwise <c>null</c>.</returns>
25-
Task<IResponseCacheEntry> GetAsync(string key);
26-
2719
/// <summary>
2820
/// Stores the given response in the response cache.
2921
/// </summary>
3022
/// <param name="key">The cache key to store the response under.</param>
3123
/// <param name="entry">The response cache entry to store.</param>
3224
/// <param name="validFor">The amount of time the entry will be kept in the cache before expiring, relative to now.</param>
3325
void Set(string key, IResponseCacheEntry entry, TimeSpan validFor);
34-
35-
/// <summary>
36-
/// Stores the given response in the response cache.
37-
/// </summary>
38-
/// <param name="key">The cache key to store the response under.</param>
39-
/// <param name="entry">The response cache entry to store.</param>
40-
/// <param name="validFor">The amount of time the entry will be kept in the cache before expiring, relative to now.</param>
41-
/// <returns>No result is returned.</returns>
42-
Task SetAsync(string key, IResponseCacheEntry entry, TimeSpan validFor);
4326
}
4427
}

src/Middleware/ResponseCaching/src/MemoryResponseCache.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@ public IResponseCacheEntry Get(string key)
3636
}
3737
}
3838

39-
public Task<IResponseCacheEntry> GetAsync(string key)
40-
{
41-
return Task.FromResult(Get(key));
42-
}
43-
4439
public void Set(string key, IResponseCacheEntry entry, TimeSpan validFor)
4540
{
4641
if (entry is CachedResponse cachedResponse)
@@ -76,11 +71,5 @@ public void Set(string key, IResponseCacheEntry entry, TimeSpan validFor)
7671
});
7772
}
7873
}
79-
80-
public Task SetAsync(string key, IResponseCacheEntry entry, TimeSpan validFor)
81-
{
82-
Set(key, entry, validFor);
83-
return Task.CompletedTask;
84-
}
8574
}
8675
}

src/Middleware/ResponseCaching/src/ResponseCachingMiddleware.cs

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,10 @@ public async Task Invoke(HttpContext httpContext)
113113
await _next(httpContext);
114114

115115
// If there was no response body, check the response headers now. We can cache things like redirects.
116-
await StartResponseAsync(context);
116+
StartResponse(context);
117117

118118
// Finalize the cache entry
119-
await FinalizeCacheBodyAsync(context);
119+
FinalizeCacheBody(context);
120120
}
121121
finally
122122
{
@@ -211,7 +211,7 @@ internal async Task<bool> TryServeCachedResponseAsync(ResponseCachingContext con
211211
internal async Task<bool> TryServeFromCacheAsync(ResponseCachingContext context)
212212
{
213213
context.BaseKey = _keyProvider.CreateBaseKey(context);
214-
var cacheEntry = await _cache.GetAsync(context.BaseKey);
214+
var cacheEntry = _cache.Get(context.BaseKey);
215215

216216
if (cacheEntry is CachedVaryByRules cachedVaryByRules)
217217
{
@@ -220,7 +220,7 @@ internal async Task<bool> TryServeFromCacheAsync(ResponseCachingContext context)
220220

221221
foreach (var varyKey in _keyProvider.CreateLookupVaryByKeys(context))
222222
{
223-
if (await TryServeCachedResponseAsync(context, await _cache.GetAsync(varyKey)))
223+
if (await TryServeCachedResponseAsync(context, _cache.Get(varyKey)))
224224
{
225225
return true;
226226
}
@@ -339,16 +339,7 @@ internal void FinalizeCacheHeaders(ResponseCachingContext context)
339339
}
340340
}
341341

342-
internal Task FinalizeCacheHeadersAsync(ResponseCachingContext context)
343-
{
344-
if (OnFinalizeCacheHeaders(context))
345-
{
346-
return _cache.SetAsync(context.BaseKey, context.CachedVaryByRules, context.CachedResponseValidFor);
347-
}
348-
return Task.CompletedTask;
349-
}
350-
351-
internal async Task FinalizeCacheBodyAsync(ResponseCachingContext context)
342+
internal void FinalizeCacheBody(ResponseCachingContext context)
352343
{
353344
if (context.ShouldCacheResponse && context.ResponseCachingStream.BufferingEnabled)
354345
{
@@ -365,7 +356,7 @@ internal async Task FinalizeCacheBodyAsync(ResponseCachingContext context)
365356

366357
context.CachedResponse.Body = bufferStream;
367358
_logger.ResponseCached();
368-
await _cache.SetAsync(context.StorageVaryKey ?? context.BaseKey, context.CachedResponse, context.CachedResponseValidFor);
359+
_cache.Set(context.StorageVaryKey ?? context.BaseKey, context.CachedResponse, context.CachedResponseValidFor);
369360
}
370361
else
371362
{
@@ -403,15 +394,6 @@ internal void StartResponse(ResponseCachingContext context)
403394
}
404395
}
405396

406-
internal Task StartResponseAsync(ResponseCachingContext context)
407-
{
408-
if (OnStartResponse(context))
409-
{
410-
return FinalizeCacheHeadersAsync(context);
411-
}
412-
return Task.CompletedTask;
413-
}
414-
415397
internal static void AddResponseCachingFeature(HttpContext context)
416398
{
417399
if (context.Features.Get<IResponseCachingFeature>() != null)
@@ -429,8 +411,7 @@ internal void ShimResponseStream(ResponseCachingContext context)
429411
context.OriginalResponseStream,
430412
_options.MaximumBodySize,
431413
StreamUtilities.BodySegmentSize,
432-
() => StartResponse(context),
433-
() => StartResponseAsync(context));
414+
() => StartResponse(context));
434415
context.HttpContext.Response.Body = context.ResponseCachingStream;
435416

436417
// Add IResponseCachingFeature

src/Middleware/ResponseCaching/src/Streams/ResponseCachingStream.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
@@ -15,15 +15,13 @@ internal class ResponseCachingStream : Stream
1515
private readonly int _segmentSize;
1616
private readonly SegmentWriteStream _segmentWriteStream;
1717
private readonly Action _startResponseCallback;
18-
private readonly Func<Task> _startResponseCallbackAsync;
1918

20-
internal ResponseCachingStream(Stream innerStream, long maxBufferSize, int segmentSize, Action startResponseCallback, Func<Task> startResponseCallbackAsync)
19+
internal ResponseCachingStream(Stream innerStream, long maxBufferSize, int segmentSize, Action startResponseCallback)
2120
{
2221
_innerStream = innerStream;
2322
_maxBufferSize = maxBufferSize;
2423
_segmentSize = segmentSize;
2524
_startResponseCallback = startResponseCallback;
26-
_startResponseCallbackAsync = startResponseCallbackAsync;
2725
_segmentWriteStream = new SegmentWriteStream(_segmentSize);
2826
}
2927

@@ -92,7 +90,7 @@ public override async Task FlushAsync(CancellationToken cancellationToken)
9290
{
9391
try
9492
{
95-
await _startResponseCallbackAsync();
93+
_startResponseCallback();
9694
await _innerStream.FlushAsync();
9795
}
9896
catch
@@ -136,7 +134,7 @@ public override async Task WriteAsync(byte[] buffer, int offset, int count, Canc
136134
{
137135
try
138136
{
139-
await _startResponseCallbackAsync();
137+
_startResponseCallback();
140138
await _innerStream.WriteAsync(buffer, offset, count, cancellationToken);
141139
}
142140
catch

0 commit comments

Comments
 (0)