Search code examples
javaspringcachingguava

What is the load() method in Guava LoadingCache?


I am trying to implement the LoadingCache and in it I must override the load() method.

However, the documentation is a bit lacking and I can't seem to find any decent examples surrounding this. My questions for this are:

  • What does it do?
  • When is it called?
  • How often is it called?

Solution

  • LoadingCache doesn't have a load() method, CacheLoader does. And if you read the CachesExplained page of the wiki in addition to the javadoc, I think there's plenty of documentation:

    • A LoadingCache will automatically compute values it doesn't already have (because they never were requested, or were evicted) when they are requested by key.
    • To do so, it delegates the computation to the CacheLoader which given a key, returns the value: that's the job of the V load(K key) method, the only abstract method of CacheLoader, the one you need to implement.