Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>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.</p> </blockquote> <p>Of course it does have such method - <a href="http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/cache/Cache.html#putAll%28java.util.Map%29" rel="nofollow"><code>putAll(Map&lt;K, V&gt;)</code></a> from <code>Cache</code> interface which <code>LoadingCache</code> extends. The method <em>Copies all of the mappings from the specified map to the cache</em>. </p> <p>There's also similar <code>put(K, V)</code> method you can use for this purpose.</p> <p><strong>EDIT:</strong></p> <p>Based on your comments I can tell that you don't want <code>LoadingCache</code> 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):</p> <pre><code>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&lt;String, String&gt; 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) &gt;= 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&lt;String, String&gt; getNewCacheContents() { return getTheData(Arrays.asList("keys", "you", "want", "to", "load")); } private Map&lt;String, String&gt; getTheData(List&lt;String&gt; listOfB) { return dao.getAToBMapping(listOfB); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload