infinum android talks #18 - how to cache like a boss by Željko plesac
TRANSCRIPT
![Page 1: Infinum Android Talks #18 - How to cache like a boss by Željko Plesac](https://reader031.vdocument.in/reader031/viewer/2022030203/58a8fc951a28ab837c8b612d/html5/thumbnails/1.jpg)
How to cache like a boss
ŽELJKO PLESAC
![Page 2: Infinum Android Talks #18 - How to cache like a boss by Željko Plesac](https://reader031.vdocument.in/reader031/viewer/2022030203/58a8fc951a28ab837c8b612d/html5/thumbnails/2.jpg)
![Page 3: Infinum Android Talks #18 - How to cache like a boss by Željko Plesac](https://reader031.vdocument.in/reader031/viewer/2022030203/58a8fc951a28ab837c8b612d/html5/thumbnails/3.jpg)
NOT ALL DATA IS EQUAL.
![Page 4: Infinum Android Talks #18 - How to cache like a boss by Željko Plesac](https://reader031.vdocument.in/reader031/viewer/2022030203/58a8fc951a28ab837c8b612d/html5/thumbnails/4.jpg)
NOT ALL CACHING MECHANISMS ARE EQUAL.
![Page 5: Infinum Android Talks #18 - How to cache like a boss by Željko Plesac](https://reader031.vdocument.in/reader031/viewer/2022030203/58a8fc951a28ab837c8b612d/html5/thumbnails/5.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022030203/58a8fc951a28ab837c8b612d/html5/thumbnails/6.jpg)
CACHING IS SLOW.
![Page 7: Infinum Android Talks #18 - How to cache like a boss by Željko Plesac](https://reader031.vdocument.in/reader031/viewer/2022030203/58a8fc951a28ab837c8b612d/html5/thumbnails/7.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022030203/58a8fc951a28ab837c8b612d/html5/thumbnails/8.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022030203/58a8fc951a28ab837c8b612d/html5/thumbnails/9.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022030203/58a8fc951a28ab837c8b612d/html5/thumbnails/10.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022030203/58a8fc951a28ab837c8b612d/html5/thumbnails/11.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022030203/58a8fc951a28ab837c8b612d/html5/thumbnails/12.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022030203/58a8fc951a28ab837c8b612d/html5/thumbnails/13.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022030203/58a8fc951a28ab837c8b612d/html5/thumbnails/14.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022030203/58a8fc951a28ab837c8b612d/html5/thumbnails/15.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022030203/58a8fc951a28ab837c8b612d/html5/thumbnails/16.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022030203/58a8fc951a28ab837c8b612d/html5/thumbnails/17.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022030203/58a8fc951a28ab837c8b612d/html5/thumbnails/18.jpg)
![Page 19: Infinum Android Talks #18 - How to cache like a boss by Željko Plesac](https://reader031.vdocument.in/reader031/viewer/2022030203/58a8fc951a28ab837c8b612d/html5/thumbnails/19.jpg)
![Page 20: Infinum Android Talks #18 - How to cache like a boss by Željko Plesac](https://reader031.vdocument.in/reader031/viewer/2022030203/58a8fc951a28ab837c8b612d/html5/thumbnails/20.jpg)
![Page 21: Infinum Android Talks #18 - How to cache like a boss by Željko Plesac](https://reader031.vdocument.in/reader031/viewer/2022030203/58a8fc951a28ab837c8b612d/html5/thumbnails/21.jpg)
Thank you!
Visit www.infinum.co or find us on social networks:
infinum.co infinumco infinumco infinum
@ZELJKOPLESAC [email protected]