aspnet/ResponseCaching

[Question] ResponseCaching and static files.

ctolkien opened this issue · 3 comments

I've configured MVC via options with some cache profiles which (with my understanding) will be picked up by this middlware and appropriately cached in memory:

options.CacheProfiles.Add(Constants.Caching.DefaultCacheProfile, new CacheProfile
{
    Duration = 60 * 60 * 24
});

All good so far. I however also configure certain static file resources for caching:

//Caching static resources
var cachePathExtensions = new[] { ".jpg", ".png", ".woff", ".woff2", ".js", ".css" };
var cacheMaxAge = new System.TimeSpan(7, 0, 0, 0);
app.UseStaticFiles(new StaticFileOptions
{
    OnPrepareResponse = r => {
        var pathExtension = r.File.PhysicalPath.Substring(r.File.PhysicalPath.LastIndexOf('.')).ToLower();
        if (cachePathExtensions.Contains(pathExtension))
        {
            r.Context.Response.Headers.Add("Cache-Control", "max-age=" + cacheMaxAge.TotalSeconds.ToString("0"));
        }
    }
});

My understanding is that these too would be cached by this middleware? The big issue here, is imagery - I've GB's of images which I want to set cache-control headers for so clients will cache them appropriately. If this middleware is caching them in memory, then I'm going to have a reaaaalllly bad time.

The caching middleware will only cash responses marked as Public. You could also put the static files middleware ahead of the caching middleware to avoid caching.

The caching middleware will only cash responses marked as Public

Right, but I would prefer if these could be kept public.

You could also put the static files middleware ahead of the caching middleware to avoid caching.

Will give this a shot.

Looks like this will be addressed with the docs by @guardrex et. al, so going to close this.