no-cache + ETag
ClientServer
GET
200 OK
Cache-Control →no-cache, private
ETag →“123"
...
GET
If-None-Match→“123"
Checking
cache
304 Not Modified
16.
no-cache + Last-Modified
ClientServer
GET
200 OK
Cache-Control →no-cache, publice
Last-Modified→ Sat, 01 Jan 2000 00:00:00 GMT
...
GET
If-Modified-Since → Sat, 01 Jan 2000 00:00:00 GMT
Checking
cache
304 Not Modified
17.
CODE EXAMPLE -BACKEND
Demo code : https://bitbucket.org/ct7ct7ct7/demo-mvc_testcache
Demo page: http://demo-test-cache.azurewebsites.net
18.
public class UserMaxAgeController: ApiController {
public HttpResponseMessage Get() {
User user = TestCacheHelper.getUser();
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, user);
response.Headers.CacheControl = new CacheControlHeaderValue() {
Private = true,
MaxAge = TimeSpan.FromSeconds(10)
};
return response;
}
}
ONLY MAX-AGE
19.
public class UserNoCacheController: ApiController {
public HttpResponseMessage Get() {
User user = TestCacheHelper.getUser();
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, user);
response.Headers.ETag = new EntityTagHeaderValue(TestCacheHelper.convertToEtag(user.GetHashCode()));
response.Content.Headers.LastModified = user.LastUpdatedAt.ToUniversalTime(); //GMT
response.Headers.CacheControl = new CacheControlHeaderValue() {
Private = true,
MaxAge = TimeSpan.FromSeconds(10)
};
var modifiedSince = Request.Headers.IfModifiedSince;
var eTag = Request.Headers.IfNoneMatch.FirstOrDefault();
if (modifiedSince != null && modifiedSince.Value.ToUniversalTime().Equals(user.LastUpdatedAt)) {
response.StatusCode = HttpStatusCode.NotModified; //304
}
if (eTag != null && eTag.ToString().Equals(TestCacheHelper.convertToEtag(user.GetHashCode()))) {
response.StatusCode = HttpStatusCode.NotModified; //304
}
return response;
}
}
ETAG + LAST-MODIFIED + MAX-AGE
20.
CODE EXAMPLE -ANDROID
Demo code : https://bitbucket.org/ct7ct7ct7/demo-android_test_cache