Interface CacheLoader<K,​V>

  • All Superinterfaces:
    Customization, DataAware<K,​V>, DataAwareCustomization<K,​V>
    All Known Subinterfaces:
    BulkCacheLoader<K,​V>
    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 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).

    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 AdvancedCacheLoader provides the loader with the current cache value.

    The AsyncCacheLoader interface can be used to provide a non-blocking asynchronous loader implementation.

    If no loader is enabled, the methods Cache.get(K) and Cache.peek(K) have identical behavior.

    Since:
    2
    Author:
    Jens Wilke
    See Also:
    Loading / Read-Through - cache2k User Guide, AdvancedCacheLoader, AsyncCacheLoader
    • Method Detail

      • load

        V load​(K key)
        throws Exception
        Retrieves 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.

        Parameters:
        key - the non-null key to provide the value for.
        Returns:
        value to be associated with the key. If the cache does not permit null values a NullPointerException is thrown, but the expiry policy is called before it.
        Throws:
        Exception - Unhandled exception from the loader. Exceptions are suppressed or wrapped and rethrown via a CacheLoaderException. Rethrow