https://www.codingpedia.org/ama/java-cache-example-with-guava
Save up to a workweek a year by efficiently managing your dev bookmarks, on www.bookmarks.dev. Share your favorites with the community and they will be published on Github -
Last week I showed you how to increase the performance of multiple java calls by making them asynchronous and parallel. This week we take a look at another potential performance booster, you should always keep in mind - caching.
Let’s imagine a scenario where you call a REST service and you know the returned values don’t change very often. In this case you really need to consider caching the response. We will use Guava’s provided caching capabilities to implement such a cache for this example.
Here is the piece of code that might do just that:
import org.codingpedia.example;import com.google.common.cache.CacheBuilder;import com.google.common.cache.CacheLoader;import com.google.common.cache.LoadingCache;import javax.inject.Inject;import javax.ws.rs.client.Client;import javax.ws.rs.client.ClientBuilder;import javax.ws.rs.core.MediaType;import java.util.List;import java.util.concurrent.ExecutionException;import java.util.concurrent.TimeUnit;import java.util.stream.Collectors;public class GuavaCacheDemoService { static LoadingCache<String, ToDo> toDosCache = CacheBuilder.newBuilder() .maximumSize(1000) .expireAfterAccess(60, TimeUnit.SECONDS) .build( new CacheLoader<String, ToDo>() { public ToDo load(String id) { final ToDo toDo = restApiClient.getToDo(id); return toDo; } } ); @Inject RestApiClient restApiClient; ToDo getToDo(String toDoId) throws ExecutionException { final ToDO toDo = toDosCache.get(toDoId); return toDo; } }
COPY
Notes:
Cache
by using the CacheBuilder
builder patternCacheBuilder.maximumSize(1000)
CacheBuilder.expireAfterAccess(60, TimeUnit.SECONDS)
only expire entries after the 60 seconds have passed since the entry was last accessed by a read or a writeLoadingCache
, which is a Cache built with an attached CacheLoader
CacheLoader
you just need to implementing the method V load(K key) throws Exception
V get(K key) throws ExecutionException
method; in our case toDosCache.get(toDoId)
Cache.invalidateAll()
I encourage you to read the Guava Caches Explained Guide to learn about other caching capabilities like writing to cache, using other eviction methods, explicit removals and so on.