Download - Http Caching for the Android Aficionado
![Page 1: Http Caching for the Android Aficionado](https://reader036.vdocuments.us/reader036/viewer/2022070515/5877a8d71a28ab826e8b6835/html5/thumbnails/1.jpg)
Http caching for the Android aficionado
Paul Blundell
![Page 2: Http Caching for the Android Aficionado](https://reader036.vdocuments.us/reader036/viewer/2022070515/5877a8d71a28ab826e8b6835/html5/thumbnails/2.jpg)
image
![Page 3: Http Caching for the Android Aficionado](https://reader036.vdocuments.us/reader036/viewer/2022070515/5877a8d71a28ab826e8b6835/html5/thumbnails/3.jpg)
image
![Page 4: Http Caching for the Android Aficionado](https://reader036.vdocuments.us/reader036/viewer/2022070515/5877a8d71a28ab826e8b6835/html5/thumbnails/4.jpg)
image
![Page 5: Http Caching for the Android Aficionado](https://reader036.vdocuments.us/reader036/viewer/2022070515/5877a8d71a28ab826e8b6835/html5/thumbnails/5.jpg)
Api ResponseServer emits headers describing caching directives
![Page 6: Http Caching for the Android Aficionado](https://reader036.vdocuments.us/reader036/viewer/2022070515/5877a8d71a28ab826e8b6835/html5/thumbnails/6.jpg)
etags
![Page 7: Http Caching for the Android Aficionado](https://reader036.vdocuments.us/reader036/viewer/2022070515/5877a8d71a28ab826e8b6835/html5/thumbnails/7.jpg)
ETags are Validation Tokens
![Page 8: Http Caching for the Android Aficionado](https://reader036.vdocuments.us/reader036/viewer/2022070515/5877a8d71a28ab826e8b6835/html5/thumbnails/8.jpg)
Local cache has previous responses
![Page 9: Http Caching for the Android Aficionado](https://reader036.vdocuments.us/reader036/viewer/2022070515/5877a8d71a28ab826e8b6835/html5/thumbnails/9.jpg)
ETags
![Page 10: Http Caching for the Android Aficionado](https://reader036.vdocuments.us/reader036/viewer/2022070515/5877a8d71a28ab826e8b6835/html5/thumbnails/10.jpg)
A request when you have an ETag
![Page 11: Http Caching for the Android Aficionado](https://reader036.vdocuments.us/reader036/viewer/2022070515/5877a8d71a28ab826e8b6835/html5/thumbnails/11.jpg)
A request when you have an ETag
![Page 12: Http Caching for the Android Aficionado](https://reader036.vdocuments.us/reader036/viewer/2022070515/5877a8d71a28ab826e8b6835/html5/thumbnails/12.jpg)
image
![Page 13: Http Caching for the Android Aficionado](https://reader036.vdocuments.us/reader036/viewer/2022070515/5877a8d71a28ab826e8b6835/html5/thumbnails/13.jpg)
cache-control
![Page 14: Http Caching for the Android Aficionado](https://reader036.vdocuments.us/reader036/viewer/2022070515/5877a8d71a28ab826e8b6835/html5/thumbnails/14.jpg)
image
![Page 15: Http Caching for the Android Aficionado](https://reader036.vdocuments.us/reader036/viewer/2022070515/5877a8d71a28ab826e8b6835/html5/thumbnails/15.jpg)
Cache Control
![Page 16: Http Caching for the Android Aficionado](https://reader036.vdocuments.us/reader036/viewer/2022070515/5877a8d71a28ab826e8b6835/html5/thumbnails/16.jpg)
Deprecation
Cache-Control header defined in HTTP/1.1 spec
supersedes Expires header
Ref: https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
![Page 17: Http Caching for the Android Aficionado](https://reader036.vdocuments.us/reader036/viewer/2022070515/5877a8d71a28ab826e8b6835/html5/thumbnails/17.jpg)
Cache Control Possibilitiesno-cache
the returned response cannot be used to satisfy a subsequent request to the same URL without first checking with the server if the response has changed.
if ETag is present, no-cache will incur a roundtrip to validate the cached response, but can eliminate the download if the resource has not changed.
no-store disallows the browser and all intermediate caches to store any version of the returned
response - e.g. one containing private personal or banking data
ETags would be ignored
![Page 18: Http Caching for the Android Aficionado](https://reader036.vdocuments.us/reader036/viewer/2022070515/5877a8d71a28ab826e8b6835/html5/thumbnails/18.jpg)
Cache Control Possibilitiespublic
can be cached, even if it has HTTP authentication associated with it, and even when the response status code isn’t normally cacheable
Most of the time, “public” isn’t necessary, because explicit caching information (like “max-age”) indicates that the response is cacheable anyway
private can be cached by the browser but are typically intended for a single user and hence
are not allowed to be cached by any intermediate cachee.g. an HTML page with private user information can be cached by that user’s browser,
but not by a CDN
![Page 19: Http Caching for the Android Aficionado](https://reader036.vdocuments.us/reader036/viewer/2022070515/5877a8d71a28ab826e8b6835/html5/thumbnails/19.jpg)
Cache Control Possibilities
max-agethe maximum time in seconds that the fetched response is allowed to be reused for
from the time of the requeste.g. “max-age=60” indicates that the response can be cached and reused for the next
60 seconds.
![Page 20: Http Caching for the Android Aficionado](https://reader036.vdocuments.us/reader036/viewer/2022070515/5877a8d71a28ab826e8b6835/html5/thumbnails/20.jpg)
Cache-Control examples
cache-control=”max-age=86400” Response can be cached by browser and any intermediary caches (i.e. it is "public")
For up to 1 day (60 seconds x 60 minutes x 24 hours)
cache-control=”private, max-age=600”Response can be cached by the client’s browser only
For up to 10 minutes (60 seconds x 10 minutes)
cache-control=”no-store”Response is not allowed to be cached Must be fetched in full on every request
![Page 21: Http Caching for the Android Aficionado](https://reader036.vdocuments.us/reader036/viewer/2022070515/5877a8d71a28ab826e8b6835/html5/thumbnails/21.jpg)
Decision Tree
![Page 22: Http Caching for the Android Aficionado](https://reader036.vdocuments.us/reader036/viewer/2022070515/5877a8d71a28ab826e8b6835/html5/thumbnails/22.jpg)
Checklist
Use consistent URLsEnsure the server provides a validation tokenIdentify which resources can be cached by intermediaries:Determine the optimal cache lifetime for each resourceDetermine the best cache hierarchy for your siteMinimize churn
![Page 23: Http Caching for the Android Aficionado](https://reader036.vdocuments.us/reader036/viewer/2022070515/5877a8d71a28ab826e8b6835/html5/thumbnails/23.jpg)
Enable OkHttp Http Cacheint cacheSize = 10 * 1024 * 1024; // 10 Mb
File cacheDirectory = new File(context.getCacheDir().getAbsolutePath(), "HttpCache");
Cache cache = new Cache(cacheDirectory, cacheSize);
new OkHttpClient.Builder()
.cache(cache)
.build();
Old but interesting link of Retrofit caching example:
https://gist.github.com/swankjesse/5889518
![Page 24: Http Caching for the Android Aficionado](https://reader036.vdocuments.us/reader036/viewer/2022070515/5877a8d71a28ab826e8b6835/html5/thumbnails/24.jpg)
Enable WebView Http CachewebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
LOAD_DEFAULT
LOAD_NO_CACHE
LOAD_CACHE_ONLY____
webView.getSettings().setAppCacheEnabled(true); webView.getSettings().setAppCachePath(context.getCacheDir()); webView.getSettings().setAppCacheMaxSize(10 * 1024*1024);
This method was deprecated in API level 18.In future quota will be managed automatically.
References:
https://github.com/novoda/notils/blob/master/android/src/main/java/com/novoda/notils/widget/webview/ExternalUrlWebViewActivity.java
http://www.html5rocks.com/en/tutorials/appcache/beginner/
![Page 25: Http Caching for the Android Aficionado](https://reader036.vdocuments.us/reader036/viewer/2022070515/5877a8d71a28ab826e8b6835/html5/thumbnails/25.jpg)
Further ReadingInvalidating cache responses with cache hierarchies
- What if you want to force a change- Not having to update the app if the api
endpoint changes- https://danpalmer.me/blog/your-api-is-n
ot-restful- https://github.com/kevinswiber/siren- https://github.com/mikekelly/hal_specific
ation
![Page 26: Http Caching for the Android Aficionado](https://reader036.vdocuments.us/reader036/viewer/2022070515/5877a8d71a28ab826e8b6835/html5/thumbnails/26.jpg)
Thanksblundell_appsG what?blundell
![Page 27: Http Caching for the Android Aficionado](https://reader036.vdocuments.us/reader036/viewer/2022070515/5877a8d71a28ab826e8b6835/html5/thumbnails/27.jpg)
throw new SlideIndexOutOfBoundsException();
blundell_appsG what?blundell
Questions?
![Page 28: Http Caching for the Android Aficionado](https://reader036.vdocuments.us/reader036/viewer/2022070515/5877a8d71a28ab826e8b6835/html5/thumbnails/28.jpg)
Referenceshttps://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-cachinghttp://www.mobify.com/blog/beginners-guide-to-http-cache-headershttps://www.w3.org/Protocols/rfc2616/rfc2616-sec14.htmlhttps://github.com/square/okhttphttp://developer.android.com/reference/android/webkit/WebView.htmlhttp://loige.co/6-rules-of-thumb-to-build-blazing-fast-web-applications/#rule-4https://danpalmer.me/blog/your-api-is-not-restfulhttps://github.com/kevinswiber/sirenhttps://github.com/mikekelly/hal_specificationhttps://github.com/novoda/notils/blob/master/android/src/main/java/com/novoda/notils/widget/webview/ExternalUrlWebViewActivity.javahttp://www.html5rocks.com/en/tutorials/appcache/beginner/https://gist.github.com/swankjesse/5889518Licensing shout out to Ilya Grigorik & http://creativecommons.org/licenses/by/3.0/