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 :[
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.
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.
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.
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.
Guava's wiki documentation has a full definition of it: (emphasis is mine)
A
LoadingCache
is aCache
built with an attachedCacheLoader
. Creating aCacheLoader
is typically as easy as implementing the methodV load(K key) throws Exception
. So, for example, you could create aLoadingCache
with the following code:[...]
The canonical way to query a
LoadingCache
is with the methodget(K)
. This will either return an already cached value, or else use the cache'sCacheLoader
to atomically load a new value into the cache. BecauseCacheLoader
might throw anException
,LoadingCache.get(K)
throwsExecutionException
. If you have defined aCacheLoader
that does not declare any checked exceptions then you can perform cache lookups usinggetUnchecked(K)
; however care must be taken not to callgetUnchecked
on caches whoseCacheLoader
s 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.
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