http caching for the android aficionado

28
Http caching for the Android aficionado Paul Blundell

Upload: paul-blundell

Post on 12-Jan-2017

359 views

Category:

Mobile


0 download

TRANSCRIPT

Page 1: Http Caching for the Android Aficionado

Http caching for the Android aficionado

Paul Blundell

Page 2: Http Caching for the Android Aficionado

image

Page 3: Http Caching for the Android Aficionado

image

Page 4: Http Caching for the Android Aficionado

image

Page 5: Http Caching for the Android Aficionado

Api ResponseServer emits headers describing caching directives

Page 6: Http Caching for the Android Aficionado

etags

Page 7: Http Caching for the Android Aficionado

ETags are Validation Tokens

Page 8: Http Caching for the Android Aficionado

Local cache has previous responses

Page 9: Http Caching for the Android Aficionado

ETags

Page 10: Http Caching for the Android Aficionado

A request when you have an ETag

Page 11: Http Caching for the Android Aficionado

A request when you have an ETag

Page 12: Http Caching for the Android Aficionado

image

Page 13: Http Caching for the Android Aficionado

cache-control

Page 14: Http Caching for the Android Aficionado

image

Page 15: Http Caching for the Android Aficionado

Cache Control

Page 16: Http Caching for the Android Aficionado

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

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

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

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

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

Decision Tree

Page 22: Http Caching for the Android Aficionado

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

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

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

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

Thanksblundell_appsG what?blundell

Page 27: Http Caching for the Android Aficionado

throw new SlideIndexOutOfBoundsException();

blundell_appsG what?blundell

Questions?

Page 28: Http Caching for the Android Aficionado

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/