- All Known Subinterfaces:
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
@FunctionalInterface public interface CacheLoader<K,V> extends DataAwareCustomization<K,V>Retrieves or generates a value to load into the cache. Using a loader to automatically populate the cache is called read through caching. If the cache is primarily used the cache data that is expensive to generate or retrieve, using a
CacheLoaderhas several advantages. The usable features with a loader are explained in the following.
Transparent operation: If configured, the loader is invoked implicitly, in case there is no value in the cache, or it is expired, by the cache methods
getEntry()as well as
The cache loader can be invoked explicitly via
Blocking: If the loader is invoked by
Cache.get(K)or other methods that allow transparent access (see above) concurrent requests on the same key will block until the loading is completed. For expired values blocking can be avoided by enabling
Cache2kBuilder.refreshAhead(boolean). There is no guarantee that the loader is invoked only for one key at a time. For example, after
Cache.clear()is called load operations for one key may overlap.
Refresh ahead: By enabling
Cache2kBuilder.refreshAhead(boolean)the cache will call the loader when an entry is expired, eagerly trying to keep the cache contents fresh.
The alternative loader interface
AdvancedCacheLoaderprovides the loader with the current cache value.
AsyncCacheLoaderinterface can be used to provide a non-blocking asynchronous loader implementation.
loadRetrieves or generates data based on the key.
From inside this method it is illegal to call methods on the same cache. This may cause a deadlock.
API rationale: This method declares an exception to allow any unhandled exceptions from the loader implementation to just pass through. Since the cache needs to deal with loader exceptions any way, this saves otherwise necessary try/catch clauses in the loader and also reduces boilerplate code.
key- the non-null key to provide the value for.
- value to be associated with the key. If the cache does not permit
NullPointerExceptionis thrown, but the expiry policy is called before it.
Exception- Unhandled exception from the loader. Exceptions are suppressed or wrapped and rethrown via a