I have a problem with setting up caching/compression for my service.
I am trying to cache service response + write corresponding HTTP caching headers, with control over response compression (to be able to enable/disable it up to my needs, not using it for e.g. images).
As I need control over returned content type (will return binary files with various types) I must to be able to set content type manually. Additionally, I want to cache 404 responses (returned for null view models).
And yes - mem caching here is intentional, files are small and needs to be cached.
Approach #1
I tried Request.ToOptimizedResultUsingCache approach but this didn’t work with HttpResponses created by me (as above - because of content type controll, 404s) and in compression being compulsory enabled.
Sample service:
public object Get(SampleRequest request)
{
return Request.ToOptimizedResultUsingCache(Cache, Request.RawUrl, TimeSpan.FromSeconds(60),
() =>
{
return new HttpResult(new byte[] {1, 2, 3}, "image/jpeg");
});
}
and this seems to ignore given content type, always returning text/html, always with compression enabled.
Approach #2
I found out brand new CacheResponse annotation support - updated to 4.0.60 and enabled following plugin:
Plugins.Add(new HttpCacheFeature());
and here is sample service:
[CacheResponse(Duration = 123, MaxAge = 123, CacheControl = CacheControl.Public)]
public HttpResult Get(SampleRequest request)
{
return new HttpResult("sample response");
}
what seems to be working… unless I specify content type in HttpResult. Than - all caching functionality stop to be applied.
Additionally, compression is always enabled. I dotpeeked the HttpCacheFeature and it seems like compression is enabled for each request being sent with accept-encoding header (what means - for each request nowadays).
Is there any way to cache service response without compression, with control over content type?