I need a cached map in my Java code. The map is loaded from DB and needs reloaded periodically from DB (for all of the data in the map). Since we can't import any new package now. Is there any existing function in google's guava package or code example?
It is better if the map is implemented as thread safe. But it is still OK if it is not but simple enough.
The "LoadingCache" is kind of what I like but it doesn't have data initialization method for me to put the data into the map at the beginning. And it needs to reach the DB for everytime "get" comes after the map expired.
Thank you!
Some sample codes may help here:
public interface AToBMapper
{
public static final String DEFAULT_B_NAME = "DEFAULT";
public String getBForA(final String a);
}
public class AToBMapperImpl implements AToBMapper
{
private final SomeDAO dao;
private Map<String, String> cachedMap;
public AToBMapperImpl(final SomeDAO dao)
{
this.dao = dao;
cachedMap = new HashMap<String, String>();
}
public String getBForA(final String a)
{
// if the map is not initialized, initialize it with the data
// if the map is expired, refresh all the data in the map
// return the mapped B for A (if there is no mapping for A, return the "DEFAULT")
}
private Map<String, String> getTheData(final List<String> listOfB)
{
Map<String, String> newData = dao.getAToBMapping(listOfB);
}
}
The "LoadingCache" is kind of what I like but it doesn't have data initialization method for me to put the data into the map at the beginning.
Of course it does have such method - putAll(Map<K, V>)
from Cache
interface which LoadingCache
extends. The method Copies all of the mappings from the specified map to the cache.
There's also similar put(K, V)
method you can use for this purpose.
EDIT:
Based on your comments I can tell that you don't want LoadingCache
at all but rather maintain expiration of all entries by yourself. Here is simple example of what you could use (only JDK's and Guava's classes):
public class AToBMapperImpl implements AToBMapper {
public static final long EXPIRE_TIME_IN_SECONDS =
TimeUnit.SECONDS.convert(1, TimeUnit.HOURS); // or whatever
private final SomeDAO dao;
private final ConcurrentMap<String, String> cache;
private final Stopwatch stopwatch;
public AToBMapperImpl(SomeDAO dao) {
this.dao = dao;
stopwatch = new Stopwatch();
cache = new MapMaker().concurrencyLevel(2).makeMap();
}
@Override
public synchronized String getBForA(final String a) {
// if the map is not initialized, initialize it with the data
if (!stopwatch.isRunning()) {
cache.putAll(getNewCacheContents());
stopwatch.start();
}
// if the map is expired, refresh all the data in the map
if (stopwatch.elapsedTime(TimeUnit.SECONDS) >= EXPIRE_TIME_IN_SECONDS) {
cache.clear();
cache.putAll(getNewCacheContents());
stopwatch.reset();
}
// return the mapped String for A
// (if there is no mapping for A, return the "DEFAULT")
return cache.containsKey(a) ? cache.get(a) : new String(DEFAULT_B_NAME);
}
private Map<String, String> getNewCacheContents() {
return getTheData(Arrays.asList("keys", "you", "want", "to", "load"));
}
private Map<String, String> getTheData(List<String> listOfB) {
return dao.getAToBMapping(listOfB);
}
}
While @Xaerxess has provided a workable solution I feel you could still have achieved this by simply using a LoadingCache
instance with a single key value. I do understand your reasoning as to the scenario as I have worked on projects where a single object load may be just as expensive as the whole set.
public class AToBMapperImpl implements AToBMapper {
private final LoadingCache<Boolean, Map<String, String>> cachedMap;
public AToBMapperImpl(final SomeDAO dao, final List<String> listOfB) {
this.cachedMap = CacheBuilder.newBuilder()
.expireAfterAccess(4, TimeUnit.HOURS)
.build(new CacheLoader<Boolean, Map<String, String>>() {
@Override
public Map<String, String> load(Boolean k) throws Exception {
return dao.getAToBMapping(listOfB);
}
});
}
@Override
public String getBForA(String a) {
try {
Map<String, String> map = cachedMap.get(Boolean.TRUE);
return map.containsKey(a) ? map.get(a) : DEFAULT_B_NAME;
} catch (ExecutionException ex) {
// Do what you need to do on exception.
throw new RuntimeException(ex.getMessage(), ex);
}
}
}
This way the cache is bound to a single entry with the value being the map you're after. You get the benefit of the expiry time. If you find that listOfB
could change over time you may need to write another service method to retrieve this which the cache loader could then call.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With