Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding Cache-control: Private in ServiceStack

Tags:

servicestack

This is similar to question Set Cache-Control: no-cache on GET requests, which wasn't really answered.

In the API responses, the cache-control header is being set to private, and I'm pretty sure I need No-Cache. I tried a ResponseFilter, but the value didn't change. It's not obvious where this is being added or set in the code or how to override, so any guidance would be appreciated.

like image 374
Rich Dudley Avatar asked Jan 12 '23 02:01

Rich Dudley


2 Answers

I use a request filter attribute that I apply to each service or method that should not be cached on the client.

public class NoCacheAttribute : RequestFilterAttribute
{
    public override void Execute(IHttpRequest req, IHttpResponse res, object responseDto)
    {
        res.AddHeader(HttpHeaders.CacheControl, "no-store,must-revalidate,no-cache,max-age=0");
    }
}
like image 96
jeffgabhart Avatar answered Jan 18 '23 23:01

jeffgabhart


In our current implementation we are setting this manually in each service using Response.AddHeader(...). This is because we want to control the cache expiration time per service. I would be interested in learning cleaner ways of doing this though.

Response.AddHeader(ServiceStack.Common.Web.HttpHeaders.CacheControl, String.Format("max-age={0}, public", ConfigurationManager.AppSettings["Cache.Expiration.Resource"]));
like image 32
Christian Hagelid Avatar answered Jan 19 '23 00:01

Christian Hagelid