I'm making an android app which uses google cloud endpoints as the backend. So I'm making request from the app. I want to cache the response of these requests in memory as well as storage.
I want to cache the response on the phone, so that I don't have to make unnecessary repeated network requests.
I searched the internet for some inbuilt solution but couldn't find anything like that in the api provided by google.
There's a total of about 2MB data that I want to cache. This data is spread over 20 end point requests.
What are my best options to implement such a cache?
You can implement an amazing library called android-easy-cache
the sample implementation is shown in my answer here
I'm going to answer my own question so that it can help someone until there's a more clean solution available.
I'm using this library for caching responses: https://github.com/vincentbrison/android-easy-cache
GenericJson
. Serialize the response using this SerializationUtilUse this code to create DualCache library boilerplate.
dualCacheByteArray
for caching the response anddualCacheDate
for keeping track oftime_to_live_for_response
Now use the above
DualCache
s to cache your response.Before making a new request using google cloud endpoints, you should check in dual cache if the old response is already present in cache
if the cached byte array is not null, then deserialize it using SerializationUtil and use it as a cached response, else make a new request from google cloud endpoints
EDIT : Using serialization util may not be necessary in every case as pointed out by Sanket Berde in other answer