Auto Caching with Retrofit

If you’re an android developer and you cache your objects then there’s a chance you’ve written code like this:

Api.getUsers()
.doOnNext(users-> database.save(users));
Api.getHouses().
doOnNext(posts-> database.save(posts));
...

And that’s the pretty version.

You end up writing each database query manually and as your code grows so does the manual work.

Wouldn’t it be nice to treat caching like a switch? Just say which objects you want cached and be done with it?

That’s what we’re gonna explore today!


Auto Caching

We’re gonna use Retrofit to make our http calls, gson for parsing and ObjectBox to cache the responses.

The idea is simple. Save every response annotated with @Cacheable directly into the database as a POJO, using Retrofit’s converter factory.

Converter

When building your Retrofit instance you have the option to add a converter factory, like so:

new Retrofit.Builder()
.baseUrl(URL)
.client(createClient())
.addConverterFactory(GsonConverterFactory.create())
.build();

We’ll use the gson converter provided by Retrofit and modify it slightly to include a listener in GsonResponseBodyConverter.class which handles the http response parsing.

The key line here is:

if (listener != null && isCacheable) listener.onCacheableResponse(clsType, response);

Whenever we get the response body we check if the @Cacheable annotation is present

and notify the listener with the response and response class type.

Lastly we need to setup GsonConverterFactory.class to include our listener


How to use

There are 2 steps involved:

  1. Simply annotate the calls you want to cache with @Cacheable, like so:

2. Use the listener to cache the response:

Done!

Now you have a single point where all http responses are saved directly into your database.

Checkout the sample project for a full code example


  • We used gson but we could as easily use any other converter as long as we add a listener to the response converter.
  • No need to use ObjectBox. Any storing mechanism flexible enough will work