Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a "LoadingCache"?

Tags:

caching

guava

Google offers a "loading cache", which is described as following:

A semi-persistent mapping from keys to values. Values are automatically loaded by the cache, and are stored in the cache until either evicted or manually invalidated.

Unfortunately, the description above isn't terribly clear. what does it mean to be "automatically loaded"?

I'm hoping it means: "If a requested key does not exist in the cache, it is then added to it automatically".

this is somewhat supported by this statement (from the get() function):

"Returns the value associated with in this cache, first loading that value if necessary."

but again, the "loading" aspect of the cache is explained with the word "loading". way to go, Google :[

like image 823
FuzzyAmi Avatar asked May 16 '17 06:05

FuzzyAmi


People also ask

How does LoadingCache work?

Interface LoadingCache<K,V> A semi-persistent mapping from keys to values. Values are automatically loaded by the cache, and are stored in the cache until either evicted or manually invalidated. Implementations of this interface are expected to be thread-safe, and can be safely accessed by multiple concurrent threads.

What is a cache loader?

A CacheLoader is an interface which specifies load and loadAll methods with a variety of parameters. CacheLoaders come from JCache, but are a frequently requested feature, so they have been incorporated into the core Ehcache classes and can be configured in ehcache. xml.

Is LoadingCache thread-safe?

LoadingCache loads data in cache automatically. Data remains in cache until it is evicted or cache is invalidated. LoadingCache is thread safe. This class provides different methods to access data for the given key.

How does Guava cache work?

Guava provides a very powerful memory based caching mechanism by an interface LoadingCache<K,V>. Values are automatically loaded in the cache and it provides many utility methods useful for caching needs.


1 Answers

Guava's wiki documentation has a full definition of it: (emphasis is mine)

A LoadingCache is a Cache built with an attached CacheLoader. Creating a CacheLoader is typically as easy as implementing the method V load(K key) throws Exception. So, for example, you could create a LoadingCache with the following code:

[...]

The canonical way to query a LoadingCache is with the method get(K). This will either return an already cached value, or else use the cache's CacheLoader to atomically load a new value into the cache. Because CacheLoader might throw an Exception, LoadingCache.get(K) throws ExecutionException. If you have defined a CacheLoader that does not declare any checked exceptions then you can perform cache lookups using getUnchecked(K); however care must be taken not to call getUnchecked on caches whose CacheLoaders declare checked exceptions.

The sentence emphasized explains all there is to explain: a value is either taken from the cache or loaded when get(K) is called.


In the comments you said you want more emphasis on what is loading. When you put things in the cache, you put. When you don't put things in the cache, but the cache will compute and put it itself, the cache loads it for you.

like image 128
Olivier Grégoire Avatar answered Sep 29 '22 04:09

Olivier Grégoire