Skip to content

Drop IResponseCache async methods #15393

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 1 commit into from
Nov 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 1 addition & 18 deletions src/Middleware/ResponseCaching/src/Interfaces/IResponseCache.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

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

/// <summary>
/// Gets the cached response for the given key, if it exists.
/// If no cached response exists for the given key, <c>null</c> is returned.
/// </summary>
/// <param name="key">The cache key to look up.</param>
/// <returns>The response cache entry if it exists; otherwise <c>null</c>.</returns>
Task<IResponseCacheEntry> GetAsync(string key);

/// <summary>
/// Stores the given response in the response cache.
/// </summary>
/// <param name="key">The cache key to store the response under.</param>
/// <param name="entry">The response cache entry to store.</param>
/// <param name="validFor">The amount of time the entry will be kept in the cache before expiring, relative to now.</param>
void Set(string key, IResponseCacheEntry entry, TimeSpan validFor);

/// <summary>
/// Stores the given response in the response cache.
/// </summary>
/// <param name="key">The cache key to store the response under.</param>
/// <param name="entry">The response cache entry to store.</param>
/// <param name="validFor">The amount of time the entry will be kept in the cache before expiring, relative to now.</param>
/// <returns>No result is returned.</returns>
Task SetAsync(string key, IResponseCacheEntry entry, TimeSpan validFor);
}
}
11 changes: 0 additions & 11 deletions src/Middleware/ResponseCaching/src/MemoryResponseCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ public IResponseCacheEntry Get(string key)
}
}

public Task<IResponseCacheEntry> GetAsync(string key)
{
return Task.FromResult(Get(key));
}

public void Set(string key, IResponseCacheEntry entry, TimeSpan validFor)
{
if (entry is CachedResponse cachedResponse)
Expand Down Expand Up @@ -76,11 +71,5 @@ public void Set(string key, IResponseCacheEntry entry, TimeSpan validFor)
});
}
}

public Task SetAsync(string key, IResponseCacheEntry entry, TimeSpan validFor)
{
Set(key, entry, validFor);
return Task.CompletedTask;
}
}
}
33 changes: 7 additions & 26 deletions src/Middleware/ResponseCaching/src/ResponseCachingMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ public async Task Invoke(HttpContext httpContext)
await _next(httpContext);

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

// Finalize the cache entry
await FinalizeCacheBodyAsync(context);
FinalizeCacheBody(context);
}
finally
{
Expand Down Expand Up @@ -211,7 +211,7 @@ internal async Task<bool> TryServeCachedResponseAsync(ResponseCachingContext con
internal async Task<bool> TryServeFromCacheAsync(ResponseCachingContext context)
{
context.BaseKey = _keyProvider.CreateBaseKey(context);
var cacheEntry = await _cache.GetAsync(context.BaseKey);
var cacheEntry = _cache.Get(context.BaseKey);

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

foreach (var varyKey in _keyProvider.CreateLookupVaryByKeys(context))
{
if (await TryServeCachedResponseAsync(context, await _cache.GetAsync(varyKey)))
if (await TryServeCachedResponseAsync(context, _cache.Get(varyKey)))
{
return true;
}
Expand Down Expand Up @@ -339,16 +339,7 @@ internal void FinalizeCacheHeaders(ResponseCachingContext context)
}
}

internal Task FinalizeCacheHeadersAsync(ResponseCachingContext context)
{
if (OnFinalizeCacheHeaders(context))
{
return _cache.SetAsync(context.BaseKey, context.CachedVaryByRules, context.CachedResponseValidFor);
}
return Task.CompletedTask;
}

internal async Task FinalizeCacheBodyAsync(ResponseCachingContext context)
internal void FinalizeCacheBody(ResponseCachingContext context)
{
if (context.ShouldCacheResponse && context.ResponseCachingStream.BufferingEnabled)
{
Expand All @@ -365,7 +356,7 @@ internal async Task FinalizeCacheBodyAsync(ResponseCachingContext context)

context.CachedResponse.Body = bufferStream;
_logger.ResponseCached();
await _cache.SetAsync(context.StorageVaryKey ?? context.BaseKey, context.CachedResponse, context.CachedResponseValidFor);
_cache.Set(context.StorageVaryKey ?? context.BaseKey, context.CachedResponse, context.CachedResponseValidFor);
}
else
{
Expand Down Expand Up @@ -403,15 +394,6 @@ internal void StartResponse(ResponseCachingContext context)
}
}

internal Task StartResponseAsync(ResponseCachingContext context)
{
if (OnStartResponse(context))
{
return FinalizeCacheHeadersAsync(context);
}
return Task.CompletedTask;
}

internal static void AddResponseCachingFeature(HttpContext context)
{
if (context.Features.Get<IResponseCachingFeature>() != null)
Expand All @@ -429,8 +411,7 @@ internal void ShimResponseStream(ResponseCachingContext context)
context.OriginalResponseStream,
_options.MaximumBodySize,
StreamUtilities.BodySegmentSize,
() => StartResponse(context),
() => StartResponseAsync(context));
() => StartResponse(context));
context.HttpContext.Response.Body = context.ResponseCachingStream;

// Add IResponseCachingFeature
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
Expand All @@ -15,15 +15,13 @@ internal class ResponseCachingStream : Stream
private readonly int _segmentSize;
private readonly SegmentWriteStream _segmentWriteStream;
private readonly Action _startResponseCallback;
private readonly Func<Task> _startResponseCallbackAsync;

internal ResponseCachingStream(Stream innerStream, long maxBufferSize, int segmentSize, Action startResponseCallback, Func<Task> startResponseCallbackAsync)
internal ResponseCachingStream(Stream innerStream, long maxBufferSize, int segmentSize, Action startResponseCallback)
{
_innerStream = innerStream;
_maxBufferSize = maxBufferSize;
_segmentSize = segmentSize;
_startResponseCallback = startResponseCallback;
_startResponseCallbackAsync = startResponseCallbackAsync;
_segmentWriteStream = new SegmentWriteStream(_segmentSize);
}

Expand Down Expand Up @@ -92,7 +90,7 @@ public override async Task FlushAsync(CancellationToken cancellationToken)
{
try
{
await _startResponseCallbackAsync();
_startResponseCallback();
await _innerStream.FlushAsync();
}
catch
Expand Down Expand Up @@ -136,7 +134,7 @@ public override async Task WriteAsync(byte[] buffer, int offset, int count, Canc
{
try
{
await _startResponseCallbackAsync();
_startResponseCallback();
await _innerStream.WriteAsync(buffer, offset, count, cancellationToken);
}
catch
Expand Down
Loading