public abstract class CacheLoader<K,V> extends Object implements FunctionalCacheLoader<K,V>
CacheLoader
has
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 get()
, getAll()
or getEntry()
as well as MutableCacheEntry.getValue()
.
The cache loader can be invoked explicitly via Cache.reloadAll(Iterable, CacheOperationCompletionListener)
.
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.
Prefetching: The method Cache.prefetch(Object)
can be used to instruct the cache to load
multiple values in the background.
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 AdvancedCacheLoader
provides the loader
with the current cache value.
The functional loader interface can be used if only a single method should be provided or with Java 8 lambdas or method references.
AdvancedCacheLoader
Constructor and Description |
---|
CacheLoader() |
Modifier and Type | Method and Description |
---|---|
abstract V |
load(K key)
Retrieves or generates data based on the key.
|
Map<K,V> |
loadAll(Iterable<? extends K> keys,
Executor executor)
Loads multiple values to the cache.
|
public abstract V load(K key) throws Exception
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 of the loader implementation to just pass through. Since the cache needs to catch an deal with loader exceptions in any way, this saves otherwise necessary try/catch clauses in the loader.
load
in interface FunctionalCacheLoader<K,V>
key
- the non-null key to provide the value for.null
values a NullPointerException
is thrown, but the expiry policy is called before it.Exception
- Unhandled exception from the loader. Exceptions are suppressed or
wrapped and rethrown via a CacheLoaderException
load(Object)
public Map<K,V> loadAll(Iterable<? extends K> keys, Executor executor) throws Exception
From inside this method it is illegal to call methods on the same cache. This may cause a deadlock.
The method is provided to complete the API. At the moment cache2k is not using it. Please see the road map.
keys
- set of keys for the values to be loadedexecutor
- an executor for concurrent loadingnull
if the cache permits null
values.Exception
- Unhandled exception from the loader. Exceptions are suppressed or
wrapped and rethrown via a CacheLoaderException
.
If an exception happens the cache may retry the load with the
single value load method.cache2k API documentation. Copyright © 2000–2017 headissue GmbH, Munich.