26

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 :[

4
  • 1
    It does what you think it does. But I'm wondering why this question exists at all. Why didn't you try it first? It's not like it's complicated to setup given the huge amount of examples you can find either on Guava's GitHub project or on the Internet. You would have seen first hand that it's doing exactly what you think it's doing. Note that the word "load" is explictly used on Wikipedia, that the Guava documentation (not Javadoc, real doc) explains this is further details. May 16, 2017 at 7:45
  • Second google result for LoadingCache links to Guava wiki page "CachesExplained" - please read it and follow up with any further concrete questions if you still don't understand it's purpose. May 16, 2017 at 7:49
  • 1
    @OlivierGrégoire: I'm not a java programmer. trying this out isn't trivial for me. Also, the wiki article is nice, but the term load is mentioned there exactly twice and I can just as easily find the term 'put' in similar contexts (see: developer.mozilla.org/en-US/docs/Web/API/Cache/put ).
    – FuzzyAmi
    May 16, 2017 at 8:04
  • 1
    @Xaerxess: thanks for the link. I did see it before i posted. It, too, does not explain what loading is besides saying: "LoadingCache can be useful [..], due to its automatic cache loading". which (again) explains nothing.
    – FuzzyAmi
    May 16, 2017 at 8:08

1 Answer 1

20

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.

10
  • 1
    thanks for confirming my question! I still think that explaining an 'X cache' with 'it will atomically X' isn't terribly descriptive and leaves room for doubt (and hence the question).
    – FuzzyAmi
    May 16, 2017 at 8:01
  • Regarding "atomically", refer to Java's "atomic" definition: it's code that will be executed "as a single action", which is important in multithreaded applications. May 16, 2017 at 8:05
  • 8
    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. May 16, 2017 at 8:13
  • 1
    Exactly the definition that is missing from the docs. thanks!
    – FuzzyAmi
    May 16, 2017 at 8:15
  • 1
    @Flaom it's a LoadingCache, and LoadingCaches can only be created after providing a CacheLoader, which represents a function returning (loading) a value based on the key. So basically, you don't put the value in the cache, but tell the cache how to load a value when a key is requested and the corresponding value must be loaded. You should really read the whole page linked in the answer. Jul 23, 2019 at 16:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.