Implementing cache mechanism with retrofit 2 as like robospice + retrofit 1

103 Views Asked by At

I have been using retrofit1 + robospice to make API requests. You can check sample here. In this framework, I used to make request like this

getSpiceManager().execute(request, CACHE_KEY, DurationInMillis.ONE_MINUTE(CACHE_TIME), new ResponseListener());

Above statement will make sure that it will return the cached response for one minute from the time I requested(with the same cache key).But currently, robospice does not support retrofit v2. My Question is that is there any cache mechanism for retrofit v2 to implement like this. I googled for some time but I could not find what exactly I want.

1

There are 1 best solutions below

0
Eliran Goshen On

the proper way to cache responses in android is using OkHttp (and it combines with Retrofit as well) use the cache method :

OkHttpClient client = new OkHttpClient.Builder()
            .cache(new Cache(cacheDir, MAX_SIZE))
            .build()

    Retrofit retrofit = new Retrofit.Builder()
            .client(okHttpClient)
            .build()