"LoadingCache" is what I like, but it has no data initialization method for entering data onto the card to the beginning.
Of course, he has such a method - putAll(Map<K, V>) from the Cache interface, which LoadingCache expands. Method Copies all mappings from the specified map to the cache.
There is also a similar put(K, V) method that you can use for this purpose.
EDIT:
Based on your comments, I can say that you don’t want LoadingCache at all, but rather save the end of all entries yourself. Here is a simple example of what you could use (JDK and Guava classes only):
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); } }
source share