2. Caching tasks:
1. Activating content upload;
2. Decreasing traffic;
3. Simplifying code (reduction of complexity and size);
4. Making possible to look through content offline (that was browsed before).
2
1. *Create database with tables for every rendering entity, and tables synchronization.
2. *Create database with one table and two fields: URL-request and response.
3. Enable HTTP cache (native or third-party).
* should write validation logic request/response
How to create cache using HTTP Restful:
3. HTTP & RESTFul
The more understandable - the better!
3
1. GET - Receiving data, an element or a list
Potential entries > 50 ? pagination : that’ll do;
Size of JSON list > 100 Kb ? pagination : that’ll do.
2. PUT - updating entry(ies).
3. POST - creating entry(ies).
4. DELETE - deleting entry(ies).
5. HEAD - getting headers in response, without body.
------------------------------------------------------------
Examples of caching URL(GET) requests:
http://blalala.com/api/category/1/articles
http://blalala.com/api/category/1/articles?offset=0&count=20
4. HTTP 1.1 Content validators
4
Request Headers Response Headers
If-Modified-Since (HTTP Date) Last-Modified (HTTP Date)
If-None-Match (Object Hash) ETag (Object Hash)
HTTP Date
RFC 822, updated by RFC 1123 Sun, 06 Nov 1994 08:49:37 GMT
RFC 850, obsoleted by RFC 1036 Sunday, 06-Nov-94 08:49:37 GMT
ANSI C's asctime() format Sun Nov 6 08:49:37 1994
Server
First request
200 code, headers and
response body
200 code, headers and response
body, 304 code and headers
Second request
6. HTTP 1.1 Cache-Control
6
● max-age=[seconds] — describes maximum time of continuing content to be fresh.
● max-stale=[seconds] — describes period of time, when it is better to put scale
content back, without request for server.
● s-maxage=[seconds] — like max-age, but is used to intermediate cache.
● public — marks requests as publicly available.
● private — is caching only on an end device.
● no-cache — makes cache to send request on a source server every time for
validation.
● no-store — doesn’t let cache save content copy, at no point.
● must-revalidate — updates content even if there is available information in cache.
● proxy-revalidate — like must-revalidate, but is used to proxy.
7. Android Setup
7
After the launching Android 4.0 version the HttpResponseCache class has become available, it
runs together with HttpURLConnection. Also OkHttp and Volley caches can be used (cache
operating speed is approximately the same).
You can enable cache in such way:
private void enableHttpResponseCache() {
try {
long httpCacheSize = 10 * 1024 * 1024; // 10 MiB
File httpCacheDir = new File(getExternalCacheDir(), "http");
HttpResponseCache.install(httpCacheDir, httpCacheSize);
} catch (Exception httpResponseCacheNotAvailable) {
Log.d(TAG, "HTTP response cache is unavailable.");
}
}
8. iOS Setup
8
You can enable cache in app delegate, that runs together with NSURLRequest:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions {
...
NSURLCache *myURLCache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 *
1024/*in bytes*/diskCapacity:20 * 1024 * 1024/*in bytes*/ diskPath:nil/*@"my folder in
default cache folder"*/];
[NSURLCache setSharedURLCache:myURLCache];
...
return YES;
}
For solving situations, which can’t be taken into a protocol, you can use another NSURLRequestCachePolicy:
1. NSURLRequestUseProtocolCachePolicy = 0, - default, is used for caching following the data from headers requests.
2. NSURLRequestReloadIgnoringLocalCacheData = 1, - is used for the force data loading.
3. NSURLRequestReturnCacheDataElseLoad = 2, - data on demand are got from cache (without sending a request on
server for checking its truth).
4. NSURLRequestReturnCacheDataDontLoad = 3, - data are got only from cache, and can be used in case of no Internet
connection.
9. Conclusion
9
1. To gain insight on the whole structure can’t be easy, but it is
worth it.
2. Error detection is challenging (the appearance of any unknown
header can change the process of caching).
3. It is incomprehensible how the third-party libraries work with
cache (especially iOS: AFNetworking, RestKit...), that is a matter
of argument on this seminar.
Alexander Sergienko
alex.khrv@gmail.com
skype: lex_sergienko_s