Http Caching for the Android Aficionado

  • View
    361

  • Download
    0

  • Category

    Mobile

Preview:

Citation preview

Http caching for the Android aficionado

Paul Blundell

image

image

image

Api ResponseServer emits headers describing caching directives

etags

ETags are Validation Tokens

Local cache has previous responses

ETags

A request when you have an ETag

A request when you have an ETag

image

cache-control

image

Cache Control

Deprecation

Cache-Control header defined in HTTP/1.1 spec

supersedes Expires header

Ref: https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

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

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

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.

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

Decision Tree

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

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

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/

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

Thanksblundell_appsG what?blundell

throw new SlideIndexOutOfBoundsException();

blundell_appsG what?blundell

Questions?

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/

Recommended