'Caching reponse method results
I have a interface for getting responses from an API server, the result is a future containing the response for a request string, my question is what is better and efficient way without repeating code to cache the results, currently in each implementation of the method I am doing map.get() from in memory cache if is cached, but it is repetitive since in each method I am writing the same. If it's useful, I'm using OkHttp, just in case it's possible to use some kind of interceptor or some other way, thanks..
interface SodyApi {
ListenableFuture<FriendsListResponse> fetchFriendsList(String repo);
ListenableFuture<GuildResponse> fetchGuild(String repo);
ListenableFuture<UsersListResponse> fetchAllUsers(String repo);
}
class SodyApiImpl implements SodyApi {
final Map<String, ListenableFuture<Response>> map = new HashMap();
public ListenableFuture<FriendsList> fetchFriendsList(String repo) {
if (map.containsKey(repo))
return map.get(repo);
// .... get response with okHttp
}
public ListenableFuture<GuildResponse> fetchGuild(String repo) {
if (map.containsKey(repo))
return map.get(repo);
// .... get response with okHttp
}
public ListenableFuture<UsersListResponse> fetchAllUsers(String repo) {
if (map.containsKey(repo))
return map.get(repo);
// .... get response with okHttp
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
