Infinum Android Talks #18 - How to cache like a boss by Željko Plesac

21
How to cache like a boss ŽELJKO PLESAC

Transcript of Infinum Android Talks #18 - How to cache like a boss by Željko Plesac

Page 1: Infinum Android Talks #18 - How to cache like a boss by Željko Plesac

How to cache like a boss

ŽELJKO PLESAC

Page 2: Infinum Android Talks #18 - How to cache like a boss by Željko Plesac
Page 3: Infinum Android Talks #18 - How to cache like a boss by Željko Plesac

NOT ALL DATA IS EQUAL.

Page 4: Infinum Android Talks #18 - How to cache like a boss by Željko Plesac

NOT ALL CACHING MECHANISMS ARE EQUAL.

Page 5: Infinum Android Talks #18 - How to cache like a boss by Željko Plesac

EACH CACHING MECHANISM IS SUITABLE FOR ONLY ONE PART OF YOUR APP DATA.

Page 6: Infinum Android Talks #18 - How to cache like a boss by Željko Plesac

CACHING IS SLOW.

Page 7: Infinum Android Talks #18 - How to cache like a boss by Željko Plesac

Response caching with OkHttp

Session object

LruCache

DiskLruCache

Preferences

Database

CACHING MECHANISMS

Page 8: Infinum Android Talks #18 - How to cache like a boss by Željko Plesac

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

Page 9: Infinum Android Talks #18 - How to cache like a boss by Željko Plesac

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; } }};

Page 10: Infinum Android Talks #18 - How to cache like a boss by Željko Plesac

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

Page 11: Infinum Android Talks #18 - How to cache like a boss by Željko Plesac

If you need to store it, the data should be encrypted.

SESSION OBJECT

Page 12: Infinum Android Talks #18 - How to cache like a boss by Željko Plesac

Memory cache which holds strong references to a limited

number of values.

It has a limited space requirement.

Deleted on app restart.

LRU CACHE

Page 13: Infinum Android Talks #18 - How to cache like a boss by Željko Plesac

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

Page 14: Infinum Android Talks #18 - How to cache like a boss by Željko Plesac

Similar to LruCache, but the data is stored in a directory on a

filesystem.

Use Jake Wharton’s implementation.

DISK LRU CACHE

Page 15: Infinum Android Talks #18 - How to cache like a boss by Željko Plesac

For storing primitive data in key-value pairs.

Can be private or shared.

Stored on filesystem.

PREFERENCES

Page 16: Infinum Android Talks #18 - How to cache like a boss by Željko Plesac

Native support for relational databases (SQLite).

Support for NoSQL databases by 3rd party libraries (Realm,

Firebase, CouchBaseLite)

DATABASES

Page 17: Infinum Android Talks #18 - How to cache like a boss by Željko Plesac

Full database support • transactions • database schemas • CRUD actions • migrations • can be copied, deleted, exported

DATABASES

Page 18: Infinum Android Talks #18 - How to cache like a boss by Željko Plesac
Page 19: Infinum Android Talks #18 - How to cache like a boss by Željko Plesac
Page 20: Infinum Android Talks #18 - How to cache like a boss by Željko Plesac
Page 21: Infinum Android Talks #18 - How to cache like a boss by Željko Plesac

Thank you!

Visit www.infinum.co or find us on social networks:

infinum.co infinumco infinumco infinum

@ZELJKOPLESAC [email protected]