Skip to content

[release/7.0] Support populating cache without serving from it. #47481

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

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 2 additions & 0 deletions src/Middleware/OutputCaching/src/OutputCacheMiddleware.cs
Original file line number Diff line number Diff line change
@@ -119,6 +119,8 @@ private async Task InvokeAwaited(HttpContext httpContext, IReadOnlyList<IOutputC
// Should we store the response to this request?
if (context.AllowCacheStorage)
{
CreateCacheKey(context);

// It is also a pre-condition to response locking

var executed = false;
68 changes: 68 additions & 0 deletions src/Middleware/OutputCaching/test/OutputCacheMiddlewareTests.cs
Original file line number Diff line number Diff line change
@@ -962,4 +962,72 @@ public async Task EmptyCacheKey_IsNotCached()
sink.Writes,
LoggedMessage.ResponseCached);
}

public class RefreshableCachePolicy : IOutputCachePolicy
{
public ValueTask CacheRequestAsync(OutputCacheContext context, CancellationToken cancellation)
{
context.AllowCacheLookup = !context.HttpContext.Request.Headers.ContainsKey("X-Refresh");
context.AllowCacheStorage = true;
return ValueTask.CompletedTask;
}

public ValueTask ServeFromCacheAsync(OutputCacheContext context, CancellationToken cancellation)
{
return ValueTask.CompletedTask;
}

public ValueTask ServeResponseAsync(OutputCacheContext context, CancellationToken cancellation)
{
return ValueTask.CompletedTask;
}
}

[Fact]
public async Task Can_Implement_Policy_That_Enables_Storage_Without_Serving()
{
var options = new OutputCacheOptions();
options.AddBasePolicy(builder =>
{
builder.AddPolicy(new RefreshableCachePolicy());
builder.Cache();
}, true);

var cache = new TestOutputCache();
var sink = new TestSink();
var middleware = TestUtils.CreateTestMiddleware(options: options, testSink: sink, cache: cache, next: async c =>
{
await c.Response.WriteAsync(Guid.NewGuid().ToString());
});

// Act - Four requests are executed. The third request
// should trigger a cache refresh so that the first two requests
// have matching output, and the last two have matching output.
var initialResponse = await SendRequestAsync(includeRefreshHeader: false);
var cachedResponse = await SendRequestAsync(includeRefreshHeader: false);
var refreshedResponse = await SendRequestAsync(includeRefreshHeader: true);
var cachedResponseAfterRefresh = await SendRequestAsync(includeRefreshHeader: false);

Assert.Equal(initialResponse, cachedResponse);
Assert.NotEqual(cachedResponse, refreshedResponse);
Assert.Equal(refreshedResponse, cachedResponseAfterRefresh);

async Task<string> SendRequestAsync(bool includeRefreshHeader)
{
var requestContext = TestUtils.CreateTestContext(cache: cache);
requestContext.HttpContext.Request.Method = "GET";
requestContext.HttpContext.Request.Path = "/";
var responseStream = new MemoryStream();
requestContext.HttpContext.Response.Body = responseStream;

if (includeRefreshHeader)
{
requestContext.HttpContext.Request.Headers.Add("X-Refresh", "randomvalue");
}

await middleware.Invoke(requestContext.HttpContext);
var response = Encoding.UTF8.GetString(responseStream.GetBuffer());
return response;
}
}
}