I want to use the
[CacheResponse(Duration = 60)] and Http Caching on certain GET methods in a web service project. There is mention of Cache Aware Client, however client solutions are not interacting with the service with ServiceStack libs. Everyone will be using browsers (chrome, etc.) to call the services. Will the browser accept the caching headers returned by the
CacheResponse attribute? Can browsers be considered cache aware clients?
The aim is that the browsers will cache the responses so that they do not have to even call the GET methods.
[CacheResponse] attribute uses the same HTTP Caching headers that browsers understand. You'll want to read up on the purpose of HTTP Caching headers like Cache-Control so you can understand the browsing caching behavior the different caching properties have.
Ok so to clarify:
Instead of writing that
GetCustomer method, I can write something like:
public Customer Any(GetCustomer request)
My assumptions would be:
- The Server will cache the response for 60 seconds
- Browsers will cache the response on their side for 30 seconds to potentially prevent them from calling the server at all during this time on subsequent requests.
For normal requests, yeah.
A use case I have is the following service which returns a user's profile picture bytes to display in an
In the browser I see the following response headers:
The browser is still calling the GET method on subsequent requests. And there is no
max-age header returned?
Use fiddler to see the exact traffic sent, the browser still shows network requests it resolves from its Cache. Make sure you're also not doing a hard refresh (i.e. just press enter on the url or click a link to the page).
On link click, or page refresh, the browser is still making direct requests to the server. And there is no
max-age header being sent according to Fiddler.
Please provide the Service impl that returns the image, and verify that your Service is getting called, instead of the default static file handling.
I copied it above:
I can confirm the service is being called due to the logging output.
I have registered an
ICacheClient as well:
If you're returning a
HttpResult specify the cache Attributes on the result, i.e:
return new HttpResult(img, imgMimeType)
LastModified = ...,
MaxAge = TimeSpan.FromSeconds(60),
CacheControl = CacheControl.Public,
Alright let me give that a try. What about the server-side caching of the result? Would I need to manually implement
ToOptimizedResultUsingCache in this specific case?
I will test a normal JSON response next.
Specifying the Attributes on the
HttpResult worked nicely. I adjusted the profile pic request to look as follows:
Working very nicely now Great job Demis!