So I am trying to cache HTTP responses (using OkHtttp + retrofit, rxjava for multithreading) using Guava cache. It currently looks something like:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://blablabla.com")
.client(client)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
apiClient = retrofit.create(Api.class);
CacheLoader<Integer, HttpResponse> cacheLoader = new CacheLoader<Integer, HttpResponse>() {
@Override
public Response load( Integer key ) throws InterruptedException, ExecutionException {
return apiClient.getHttpResponse(key)
.subscribeOn(Schedulers.io())
.blockingFirst();
}
};
responseCache = CacheBuilder.newBuilder()
.concurrencyLevel(8)
.maximumSize( 10 )
.build(cacheLoader);
The apiClient returns Observable, and it is subscribed within the CacheLoader's load method. I have also set concurrencyLevel(8) but it doesn't seem to allow 'loading' and 'reading' at the same time.
I think the blockingFirst() call probably block the thread so I can't make concurrent requests from the cache i.e. whenever cache is loading a new http response, cache can't be read.
I don't know how to make it asynchronous, any help is appreciated :)