dotnet / dotnet/aspnetcore

OutputCaching + YARP interoperability

Open
#43,791 2 comments 0 reactions 0 assignees View on GitHub
area-middleware area-networking feature-output-caching
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

Hello,

As per discussion over in the YARP repo https://github.com/microsoft/reverse-proxy/discussions/1851.

In the scenario where OutputCaching is being used with YARP I would like a way to be able to validate the cached items on request.

I believe there would be value in having a way to make YARP and OutputCaching work together.
Have YARP leverage OutputCaching instead of having OutputCaching on top of YARP.

At a high level I'd like to achieve the following pipeline:

Get request > YARP processes its routes/clusters > YARP/[custom middleware](https://microsoft.github.io/reverse-proxy/articles/middleware.html#custom-proxy-middleware) gets the output cache headers and performs a head request against the origin server with the "If-Modified-Since" and "If-None-Match" headers > return the Output Cache data if valid else back to YARP to process the request per usual and the output is cached

Perhaps something like the HttpContext.Features `HttpContext.GetReverseProxyFeature()` extension method for YARP middleware but for output caching?

EG:
```c#
endpoints.MapReverseProxy(proxyPipeline =>
{
proxyPipeline.Use((context, next) =>
{
var proxyFeature = context.GetReverseProxyFeature();
var outputCacheFeature = context.GetOutputCacheFeature();
var cluster = proxyFeature.Cluster;
var destinations = proxyFeature.AvailableDestinations;
var cachedHeaders = outputCacheFeature.GetHeaders(context.Request);

if (cachedHeaders is not null)
{
// logic to validate and optionally return the cached item
}

return next();
});
proxyPipeline.UseSessionAffinity();
proxyPipeline.UseLoadBalancing();
proxyPipeline.UsePassiveHealthChecks();
});
```

This could also be used by other non YARP things that have access to the HttpContext.

My end game goal is to be able to replace IIS+ARR/Nginx's disk based content caching with YARP and hopefully OutputCaching.
Ideally I wont have to go down the path of writing my own caching mechanisms for YARP as the guts of OutputCaching appears to be solid as.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.