OutputCache and almost all HTTP level caches are designed to ignore failed responses, anything other more than 399, all status code above 399 are error codes.
You can manually set Response Headers like
context.Response.Headers.CacheControl = "public, max-age=3600"
This is what OutputCache
does internally, however, OutputCache
might also provide server level cache if setup.
But if you use CDN, I doubt they will respect cache control in case of failed response status codes.
But it is not recommended. Lets say for some reason user deletes the cookies or cookie gets corrupted. And user receives 410 error. And user goes back to login and logs in correctly, and now for same request user will continue to receive 410 for one hour.
Or otherwise let’s say your database is unavailable and database throws 410 error, so even with valid cookie or valid authorization, user will receive cached error response. Caching layer doesn’t know when not to cache.
Instead, you can use MemoryCache
, to cache error results in your memory for couple of seconds without going to database each time. Though the results will still hit the web servers.
CloudFlare and Azure provides web application firewall, which allows intelligently blocking errornous traffic if you are worried about DDOS kind of attacks. This approach seems better than using HTTP Cache, as you will have no control over user's cache. And you can't programmatically clear cache after successful login.