0

Am using loading cache to cache the result of the query. As far as I know, If the key is not found in cache, it will load from DB. While loading will it populate the cache simultaneously?

CacheLoader <String, Obj> Loader = new CacheLoader<String, Obj>() { @Override public Obj load(String key) throws Exception { Obj obj = ObjDao.getDs().createQuery(Obj.class).filter("id ==", key).get(); return obj; } }; 
1
  • 1
    Yes, the CacheLoader as used in a LoadingCache retrieves values that are stored in the cache until either evicted or manually invalidated. It's all in the linked documentation. Commented Mar 23, 2016 at 9:32

1 Answer 1

1

LoadingCache:

Values are automatically loaded by the cache, and are stored in the cache until either evicted or manually invalidated.

LoadingCache.get(K):

Returns the value associated with key in this cache, first loading that value if necessary. No observable state associated with this cache is modified until loading completes.

If another call to get(K) or getUnchecked(K) is currently loading the value for key, simply waits for that thread to finish and returns its loaded value. Note that multiple threads can concurrently load values for distinct keys.

Caches loaded by a CacheLoader will call CacheLoader.load(K) to load new values into the cache. Newly loaded values are added to the cache using Cache.asMap().putIfAbsent after loading has completed; if another value was associated with key while the new value was loading then a removal notification will be sent for the new value.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.