ASP.Net Core 2.0 - ResponseCaching Middleware - Not Caching on Server
Asked Answered
D

3

14

I want to use server-side response caching (output cache) with asp.net core 2.0 and found out about Response Caching Middleware and wanted to give it a try with a brand new asp.core mvc project.

Here is the description from the link above which makes me think this could be used like output cache.

The middleware determines when responses are cacheable, stores responses, and serves responses from cache.

Here is how my startup.cs looks like.

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddResponseCaching();
        services.AddMvc();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseResponseCaching();

        if (env.IsDevelopment())
        {
            app.UseBrowserLink();
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseStaticFiles();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}

and here is the HomeController.cs

[ResponseCache(Duration = 60)]
public class HomeController : Controller
{
    public IActionResult Index()
    {
        return View();
    }

    public IActionResult About()
    {
        ViewData["Message"] = "Your application description page.";

        return View();
    }

    public IActionResult Contact()
    {
        ViewData["Message"] = "Your contact page.";

        return View();
    }

    public IActionResult Error()
    {
        return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
    }
}

there is also a timestamp at the bottom of _Layout.cshtml file so i can tell when the page is rendered, like below.

<p>&copy; 2018 - ResponseCachingMiddleware - @DateTime.UtcNow</p>

Cache-Control headers seem to be fine, this is what I get in headers when I load the page but time stamp keeps getting updated on every refresh every second.

Cache-Control:public,max-age=60

What I'm understanding from MS documentations is Response Caching Middleware is the server-side caching mechanism that takes care of caching the response while Response Caching seems to be just a filter to manipulate response headers for caching.

Can't tell if there is something wrong with my understanding or code and I wanna complain that I'm feeling this way too often since I started prototyping with ASP.Net Core. Maybe you could also suggest better resources as a side topic.

I've checked out this post before ASP.NET Core 2.0 - Http Response Caching Middleware - Nothing cached

Also checked this out but it seems like the only difference is I'm using mvc. https://github.com/aspnet/ResponseCaching/blob/dev/samples/ResponseCachingSample/Startup.cs

Thanks

Edit: I'm seeing the message below in the output window, cannot find anything about it on google except the few places I already checked for response caching middleware.

Microsoft.AspNetCore.ResponseCaching.ResponseCachingMiddleware:Information: The response could not be cached for this request.

Note: I wish I could create #response-caching-middleware tag. Not sure #responsecache is relevant.

Doyen answered 12/1, 2018 at 19:31 Comment(0)
W
14

I had this same confusion recently.

ASP.Net Core's ResponseCaching does provide both client-side caching (through HTTP response headers) & server-side (through a memory cache'd middleware that short-circuits other middlewares if the response is in the cache). The server-side portion reads the HTTP response cache headers to determine if it should do server-side caching (similar to what an ISP or CDN might do).

Unfortunately, debugging the server-side ResponseCaching is tricky because it has weird rules & there's not adequate logging. In my case I pulled down Microsoft's source code to step through it & find the issue with my code.

The note you found in the output window "The response could not be cached for this request" is a clue.

There's 2 parts to the server-side caching of a request. The server has to prime the cache the first time the url is requested. It will serve the cached version the 2nd time it's requested. Pay attention to when the error message shows up, if it's on the 1st or 2nd request. That'll tell you if it couldn't be stored in the cache or if it couldn't be retrieved from the cache.

The rules for both storage & retrieval are in this source code file: https://github.com/aspnet/ResponseCaching/blob/3bf5f6a1ce69b65c998d6f5c739822a9bed4a67e/src/Microsoft.AspNetCore.ResponseCaching/Internal/ResponseCachingPolicyProvider.cs

Your "Cache-Control:public,max-age=60" header should match these rules just fine.

My guess is you actually had it working, but didn't know how to test it correctly. There is a counter-intuitive portion of ResponseCaching noted in this issue: https://github.com/aspnet/Home/issues/2607 Essentially, if the browser sends a no-cache or no-store header (when you hit CTRL+F5 or have your debugger tools open), ASP.Net Core's ResponseCaching will honor the browser's request & re-generate the response.

So, to test if your code was working you probably loaded the page, which primed the cache, then you hit CTRL+F5 to force-refresh your browser & you expected the server-side to respond with a cached entry rather than running your WebAPI code. However, it honored the no-cache request header & bypassed the cache (& wrote that message in your output log).

The way to test this would be to clear your browser cache in-between requests (or switch to incognito), rather than using CTRL+F5.

On a side note, honoring the no-cache/no-store request headers was probably a poor design choice since ASP.Net Core's ResponseCache will most likely be used by a server who owns the response, rather than an intermediary cache like a CDN/ISP. I've extended the base ResponseCache with an option to disable honoring these headers (as well as serialize the cache to disk, rather than in-memory only). It's an easy drop-in replacement for the default cache.

You can find my extension here: https://github.com/speige/AspNetCore.ResponseCaching.Extensions https://www.nuget.org/packages/AspNetCore.ResponseCaching.Extensions

There are also a few other other gotchas with ResponseCaching to watch out for which you may have already read about in the blog urls you posted. Authenticated requests & responses with set-cookie won't be cached. Only requests using GET or HEAD method will be cached. If the QueryString is different, it'll make a new cache entry. Also, usually you'll want a "Vary" header to prevent caching if certain conditions of a request differ from the previously-cached request (example: user-agent, accept-encoding, etc). Finally, if a Middleware handles a request it'll short-circuit later Middlewares. Make sure your app.UseResponseCaching() is registered before app.UseMVC()

Widen answered 16/2, 2018 at 7:41 Comment(5)
Thank you for the detailed response. I've abandoned this problem for a while after finding a different solution but I'll check this out as soon as I got back to it. It makes more sense that I was testing it wrong than the functionality didn't exist.Doyen
@DevinGarner looked into your code and you should log exception(because there are). By the way, you should do a performance benchmark on your code compared to default response caching. Your implementation is quite slow :) and should support any IResponseCacheEntry. Regards.Electromechanical
Thanks for the code review @Electromechanical if you have time, please submit a pull request with your improvements. Thanks!Widen
Thank god I read your answer until the end. Calling app.UseResponseCaching(); before app.UseMvc(); did the trick!Smart
Microsoft desperately needs to tell us which rule was violated that preventing caching.Ziagos
S
23

I had the same issue, I was about to pull my hairs over it, I'd set app.UseResponseCaching(); as well as services.AddResponseCaching(); and add ResponseCache on top of my action exactly like what was told in Microsoft official Docs, despite the the cache-controll header was set correctly on response returning from server but still nothing cached at server-side.

After couple of hours of sweating on this issue I figured out where the problem arises and why nothing cached at server.

Browsers by default set cache-controll value to max-age=0 for the request (if the request is not caused by back or forward) even though you set cache-controller correctly in your response by adding ResponseCache attribute on top of you action (or controller) since the cache-controller sent by request is set to max-age=0, the server is unable to cache response, I think this must be added to list of Response Caching limitation as well

Anyway you can override browser default behavior by adding few line of code right before calling app.UseResponseCaching(); on the other hand you need to add a custom middle-ware to modify request cache-control header value before calling app.UseResponseCaching();.

See code below, worked for me hope work for you too

 app.Use(async (ctx, next) =>
        {
            ctx.Request.GetTypedHeaders().CacheControl = new Microsoft.Net.Http.Headers.CacheControlHeaderValue()
            {
                Public = true,
                MaxAge = TimeSpan.FromSeconds(60)
            };
            await next();
        }
            );
        app.UseResponseCaching();

for ensuring that ResponseCaching works as expected you can also use postman but you must set 'Send no-cache Header' to off in the setting, see image below

enter image description here

Somnolent answered 10/8, 2018 at 11:16 Comment(5)
Thank you, I've abandoned this problem for a while now but I'll get back to it shortly and give this a try.Doyen
@Doyen happy coding :)Somnolent
Yes it has worked, if some one have a compiling error add he namespace Microsoft.AspNetCore.Http GetTypedHeaders() is an extension methodStouthearted
Works on web api calls, note that you can check the output window (.net core 3.1 for me): "ResponseCachingMiddleware: Information: The response has been cached.", "ResponseCachingMiddleware: Information: Serving response from cache."Rawdin
Damn, Postman setting was the problem in my case. OMG I needed 2 hours to solve the mystery.Touraine
W
14

I had this same confusion recently.

ASP.Net Core's ResponseCaching does provide both client-side caching (through HTTP response headers) & server-side (through a memory cache'd middleware that short-circuits other middlewares if the response is in the cache). The server-side portion reads the HTTP response cache headers to determine if it should do server-side caching (similar to what an ISP or CDN might do).

Unfortunately, debugging the server-side ResponseCaching is tricky because it has weird rules & there's not adequate logging. In my case I pulled down Microsoft's source code to step through it & find the issue with my code.

The note you found in the output window "The response could not be cached for this request" is a clue.

There's 2 parts to the server-side caching of a request. The server has to prime the cache the first time the url is requested. It will serve the cached version the 2nd time it's requested. Pay attention to when the error message shows up, if it's on the 1st or 2nd request. That'll tell you if it couldn't be stored in the cache or if it couldn't be retrieved from the cache.

The rules for both storage & retrieval are in this source code file: https://github.com/aspnet/ResponseCaching/blob/3bf5f6a1ce69b65c998d6f5c739822a9bed4a67e/src/Microsoft.AspNetCore.ResponseCaching/Internal/ResponseCachingPolicyProvider.cs

Your "Cache-Control:public,max-age=60" header should match these rules just fine.

My guess is you actually had it working, but didn't know how to test it correctly. There is a counter-intuitive portion of ResponseCaching noted in this issue: https://github.com/aspnet/Home/issues/2607 Essentially, if the browser sends a no-cache or no-store header (when you hit CTRL+F5 or have your debugger tools open), ASP.Net Core's ResponseCaching will honor the browser's request & re-generate the response.

So, to test if your code was working you probably loaded the page, which primed the cache, then you hit CTRL+F5 to force-refresh your browser & you expected the server-side to respond with a cached entry rather than running your WebAPI code. However, it honored the no-cache request header & bypassed the cache (& wrote that message in your output log).

The way to test this would be to clear your browser cache in-between requests (or switch to incognito), rather than using CTRL+F5.

On a side note, honoring the no-cache/no-store request headers was probably a poor design choice since ASP.Net Core's ResponseCache will most likely be used by a server who owns the response, rather than an intermediary cache like a CDN/ISP. I've extended the base ResponseCache with an option to disable honoring these headers (as well as serialize the cache to disk, rather than in-memory only). It's an easy drop-in replacement for the default cache.

You can find my extension here: https://github.com/speige/AspNetCore.ResponseCaching.Extensions https://www.nuget.org/packages/AspNetCore.ResponseCaching.Extensions

There are also a few other other gotchas with ResponseCaching to watch out for which you may have already read about in the blog urls you posted. Authenticated requests & responses with set-cookie won't be cached. Only requests using GET or HEAD method will be cached. If the QueryString is different, it'll make a new cache entry. Also, usually you'll want a "Vary" header to prevent caching if certain conditions of a request differ from the previously-cached request (example: user-agent, accept-encoding, etc). Finally, if a Middleware handles a request it'll short-circuit later Middlewares. Make sure your app.UseResponseCaching() is registered before app.UseMVC()

Widen answered 16/2, 2018 at 7:41 Comment(5)
Thank you for the detailed response. I've abandoned this problem for a while after finding a different solution but I'll check this out as soon as I got back to it. It makes more sense that I was testing it wrong than the functionality didn't exist.Doyen
@DevinGarner looked into your code and you should log exception(because there are). By the way, you should do a performance benchmark on your code compared to default response caching. Your implementation is quite slow :) and should support any IResponseCacheEntry. Regards.Electromechanical
Thanks for the code review @Electromechanical if you have time, please submit a pull request with your improvements. Thanks!Widen
Thank god I read your answer until the end. Calling app.UseResponseCaching(); before app.UseMvc(); did the trick!Smart
Microsoft desperately needs to tell us which rule was violated that preventing caching.Ziagos
C
-1

If the Cache-Control header is coming through, then it's working. That's all the server can do from that perspective. The client ultimately makes the decision whether or not to actually cache the resource. Sending the header doesn't force the client to do anything; in fact, the server, in general, cannot force the client to do anything.

Crackup answered 12/1, 2018 at 19:36 Comment(17)
Can you explain me what's the difference between Response Caching Middleware and Response Caching? As far I can tell, you're describing Response Caching filter's responsibility.Doyen
There is none. They are two sides of the same coin. The middleware is what actually does the work, while the attribute informs the middleware as to what it should actually do (i.e. set max-age=60 because you specified Duration = 60). Response caching, in general, only sets the Cache-Control header. It does nothing to cache the response server-side. And, like I said, it's 100% up to the client as to whether the header is actually honored. If you're looking for server-side caching, you need to employ in-memory cache or distributed caching.Crackup
Please see the description for Response Caching Middleware from the documentation page I referred to. It clearly states that it stores and serves responses. You might be confusing it with Response Caching.Doyen
I believe the Response Caching filters are used for the first objective of Response Caching Middleware which is determining when the response is cacheable.Doyen
The description is misleading, bordering on incorrect. I think it's referring to the act of the client storing the response, but if so, it's worded extremely poorly. Regardless, all it does it set cache headers. It even says as much in the next line as it refers to what it does as "HTTP Caching".Crackup
If you look through the whole documentation, you will see that there is no mention of anything but various headers and values that are set. Never any mention of actual server-side storage. If you look at the actual code, you can also see clearly that it does nothing but manipulate headers.Crackup
Are you saying that code is to demonstrate response caching middleware's functionality while it's already doing the header manipulation manually? That mean if I remove the middleware I would get exactly the same behavior so what's the code example here?Doyen
are you using asp.net core?Doyen
I believe the code there manually changing the response headers is to let middleware know that it's supposed to cache since there is no controller in the code to add the filters.Doyen
there is also an error message in the output windows which you can see in the question. it says Microsoft.AspNetCore.ResponseCaching.ResponseCachingMiddleware:Information: The response could not be cached for this request. all these things make me think that middleware is actually responsible for caching.Doyen
It is. However, I think you're missing the point. This is HTTP Caching. That's a cooperative effort between the server and client, where the server determines what can be cached, how, and when, and indicates this to the client via HTTP headers. The client the can opt to cache the resource according to the server's suggestions. The server here is not caching anything. The client does the caching.Crackup
The middleware reads the attribute, and then adds HTTP headers accordingly. That's it.Crackup
I am really missing the point here because it makes the middleware redundant, especially in the code example given for the middleware itself since the headers already added before the middleware. I'm sorry if I'm sounding like trying to argue with you, I'm convinced with your answer that it won't cache, but I'm still not sure if this is not something else, like a wrapper to implement your own in-memory cache which I haven't seen any code examples for. I'd like to make sure I properly write to the response. Thanks of bunch.Doyen
I'm not sure what you're referring to, but this middleware is what adds those headers. It has a simplistic job, but it does have a job, and if you don't add the middleware you won't get the cache headers. That said, the middleware adds default headers, so you don't actually need the attribute on your controller/action. That's just if you want to customize the defaults.Crackup
This answer is incorrect. ResponseCaching in ASP.Net Core does both client-side caching (through headers) & Server-Side Caching (similar to OutputCache in previous versions of ASP.Net). OP was specifically asking why the server-side caching wasn't working.Widen
@DevinGarner: No. You can go look at the source if you like. It doesn't. It's just HTTP cache.Crackup
@ChrisPratt I have read the source. github.com/aspnet/ResponseCaching I have also successfully used it. You may have been correct about an earlier version of .net core's ResponseCaching, but it has also been a server-side cache for over a year now.Widen

© 2022 - 2024 — McMap. All rights reserved.