Caching with ToOptimizedResultUsingCache()

I am using ToOptimizedResultUsingCache() as guided by the https://github.com/ServiceStack/ServiceStack/wiki/Caching page.

My goal is (for any of my REST resources) to cache a GET request for some period (say 30secs) and then, if necessary remove the cached response for that GET request whenever there is a PUT or DELETE for that same resource.
Looks like I can do all of that with just ToOptimizedResultUsingCache() and the wiki page guidance, which is great for saving computation for our resources.

However, I also wanted to help support basic HTTP caching of our services, by leveraging both ‘Expiration’ and ‘Validation’ patterns (nicely explained here: http://2ndscale.com/rtomayko/2008/things-caches-do) by returning the correct HTTP headers for any reverse-Proxy servers we deploy later at scale.

In particular, by supporting decorating our responses with these HTTP headers: Cache-Control, Expires, and LastModified, ETag, If-Modified-Since and If-None_Match. All our responses are HTTPS, which are by default not cached by HTTP cache intermediaries.

Is there any help in ServiceStack for adding these HTTP response headers when using ToOptimizedResultUsingCache(). Or is there a recommended way of adding these headers using the information that is already cached by ToOptimizedResultUsingCache()?

Or am I on my own here, in terms of adding these headers manually in my service? (I know how to add headers, and response filters etc, this is more of a question of what are the right hooks when using ToOptimizedResultUsingCache()?

Other than providing a number of different ways to add HTTP Headers and some IRequest extension methods like AddHeaderLastModified/HasNotModifiedSince, there’s no additional built-in functionality you can plug into. The opinionated behavior of ToOptimizedResultUsingCache isn’t customizable, any caching headers/logic would need to be applied before then.

If it helps we have an example how we add Caching Headers in StaticFileHandler.

Thanks, that’s what I wanted to know before proceeding.

BTW, I am not sure I understand the compressed result that comes from the ToOptimizedResult set of methods. What is the intention of returning that? I was expecting just my cached DTO (as it was) to be returned.

CompressedResult encapsulates a deflated/gzip version of the cached DTO so captures the most optimal cached representation.