001package org.cache2k.integration;
002
003/*
004 * #%L
005 * cache2k API
006 * %%
007 * Copyright (C) 2000 - 2016 headissue GmbH, Munich
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 * 
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import org.cache2k.CacheOperationCompletionListener;
024import org.cache2k.processor.MutableCacheEntry;
025import org.cache2k.Cache;
026import org.cache2k.Cache2kBuilder;
027
028import java.util.Map;
029import java.util.concurrent.Executor;
030
031/**
032 * Retrieves or generates a value to load into the cache. Using a loader to automatically
033 * populate the cache is called read through caching. If the cache is primarily used the
034 * cache data that is expensive to generate or retrieve, using a {@code CacheLoader} has
035 * several advantages. The usable features with a loader are explained in the following.
036 *
037 * <p><b>Transparent operation</b>: If configured, the loader is invoked implicitly, in case there is no value in
038 * the cache or it is expired, by the cache methods {@code get()}, {@code getAll()}
039 * or {@code getEntry()} as well as {@link MutableCacheEntry#getValue()}.
040 *
041 * <p>The cache loader can be invoked explicitly via {@link Cache#reloadAll(CacheOperationCompletionListener, Iterable)}.
042 *
043 * <p><b>Blocking</b>: If the loader is invoked by {@link Cache#get} or other methods that allow transparent access
044 * (see above) concurrent requests on the same key will block until the loading is completed.
045 * For expired values blocking can be avoided by enabling {@link Cache2kBuilder#refreshAhead}.
046 * There is no guarantee that the loader is invoked only for one key at a time. For example,
047 * after {@link Cache#clear()} is called load operations for one key may overlap.
048 *
049 * <p><b>Prefetching</b>: The method {@link Cache#prefetch(Object)} can be used to instruct the cache to load
050 * multiple values in the background.
051 *
052 * <p><b>Refresh ahead</b>: By enabling {@link Cache2kBuilder#refreshAhead} the cache will
053 * call the loader when an entry is expired, eagerly trying to keep the cache contents fresh.
054 *
055 * <p>The alternative loader interface {@link AdvancedCacheLoader} provides the loader
056 * with the current cache value.
057 *
058 * @author Jens Wilke
059 * @see AdvancedCacheLoader
060 * @since 0.24
061 */
062public abstract class CacheLoader<K, V> {
063
064  /**
065   * Retrieves or generates data based on the key.
066   *
067   * <p>From inside this method it is illegal to call methods on the same cache. This
068   * may cause a deadlock.
069   *
070   * <p>API rationale: This method declares an exception to allow any unhandled
071   * exceptions of the loader implementation to just pass through. Since the cache
072   * needs to catch an deal with loader exceptions in any way, this saves otherwise
073   * necessary try/catch clauses in the loader.
074   *
075   * @param key the non-null key to provide the value for.
076   * @return value to be associated with the key. If the cache does not permit {@code null}
077   *         values a {@link NullPointerException} is thrown, but the expiry policy is called before it.
078   * @throws Exception Unhandled exception from the loader. Exceptions are suppressed or
079   *                   wrapped and rethrown via a {@link CacheLoaderException}
080   */
081  public abstract V load(K key) throws Exception;
082
083  /**
084   * Loads multiple values to the cache.
085   *
086   * <p>From inside this method it is illegal to call methods on the same cache. This
087   * may cause a deadlock.
088   *
089   * <p>The method is provided to complete the API. At the moment cache2k is not
090   * using it. Please see the road map.
091   *
092   * @param keys set of keys for the values to be loaded
093   * @param executor an executor for concurrent loading
094   * @return The loaded values. A key may map to {@code null} if the cache permits {@code null} values.
095   * @throws Exception Unhandled exception from the loader. Exceptions are suppressed or
096   *                   wrapped and rethrown via a {@link CacheLoaderException}.
097   *                   If an exception happens the cache may retry the load with the
098   *                   single value load method.
099   */
100  public Map<K, V> loadAll(Iterable<? extends K> keys, Executor executor) throws Exception {
101    throw new UnsupportedOperationException();
102  }
103
104}