Outputcache and 410 status response not working

199 views Asked by At

I would like to throw a 410 status code for a page that does some database lookups. However it seems that outputcache only caches 200 status code pages. Is there anyway around this? I want to cache 410 pages for a while so the database doesn't get it, but it just skips the cache and hits the database again.

[OutputCache(Duration = 3600, VaryByParam = "eventid,teamid,v,r")]
    public virtual ActionResult Team(int? teamId, int? eventId)
    {
1

There are 1 answers

7
Akash Kava On

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.