Infinum Android Talks #18 - How to cache like a boss by Željko Plesac
Transcript of Infinum Android Talks #18 - How to cache like a boss by Željko Plesac
How to cache like a boss
ŽELJKO PLESAC
NOT ALL DATA IS EQUAL.
NOT ALL CACHING MECHANISMS ARE EQUAL.
EACH CACHING MECHANISM IS SUITABLE FOR ONLY ONE PART OF YOUR APP DATA.
CACHING IS SLOW.
Response caching with OkHttp
Session object
LruCache
DiskLruCache
Preferences
Database
CACHING MECHANISMS
Cache network responses on OkHttp client.
Custom cache size and cache time validity.
No support for data caching, which is created in your app.
OKHTTP RESPONSE CACHING
private static final Interceptor cacheControlInterceptor = new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); Response response = chain.proceed(request); if (shouldCacheResponse(request)) { // Re-write response CC header to force use of cache return response.newBuilder() .header("Cache-Control", "public, max-age=3600") // 1 hour .build(); } else { return response; } }};
Simple POJO object which is used for data storage.
Singleton, provided by AppComponent.
Can have shorter lifecycle than app lifecycle.
Used for storing sensitive user data.
SESSION OBJECT
If you need to store it, the data should be encrypted.
SESSION OBJECT
Memory cache which holds strong references to a limited
number of values.
It has a limited space requirement.
Deleted on app restart.
LRU CACHE
Each time a valued is accessed, it is moved to the head of the
queue.
When a value is put into the cache, the value at the end of the
list may be evicted.
LRU CACHE - HOW IT WORKS
Similar to LruCache, but the data is stored in a directory on a
filesystem.
Use Jake Wharton’s implementation.
DISK LRU CACHE
For storing primitive data in key-value pairs.
Can be private or shared.
Stored on filesystem.
PREFERENCES
Native support for relational databases (SQLite).
Support for NoSQL databases by 3rd party libraries (Realm,
Firebase, CouchBaseLite)
DATABASES
Full database support • transactions • database schemas • CRUD actions • migrations • can be copied, deleted, exported
DATABASES
Thank you!
Visit www.infinum.co or find us on social networks:
infinum.co infinumco infinumco infinum
@ZELJKOPLESAC [email protected]