001/*
002 * Copyright (C) 2011 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.cache;
016
017import com.google.common.annotations.GwtCompatible;
018import com.google.common.base.Function;
019import com.google.common.collect.ImmutableMap;
020import com.google.common.util.concurrent.ExecutionError;
021import com.google.common.util.concurrent.UncheckedExecutionException;
022import java.util.concurrent.ConcurrentMap;
023import java.util.concurrent.ExecutionException;
024
025/**
026 * A semi-persistent mapping from keys to values. Values are automatically loaded by the cache, and
027 * are stored in the cache until either evicted or manually invalidated. The common way to build
028 * instances is using {@link CacheBuilder}.
029 *
030 * <p>Implementations of this interface are expected to be thread-safe, and can be safely accessed
031 * by multiple concurrent threads.
032 *
033 * <p>When evaluated as a {@link Function}, a cache yields the same result as invoking {@link
034 * #getUnchecked}.
035 *
036 * @author Charles Fry
037 * @since 11.0
038 */
039@GwtCompatible
040public interface LoadingCache<K, V> extends Cache<K, V>, Function<K, V> {
041
042  /**
043   * Returns the value associated with {@code key} in this cache, first loading that value if
044   * necessary. No observable state associated with this cache is modified until loading completes.
045   *
046   * <p>If another call to {@link #get} or {@link #getUnchecked} is currently loading the value for
047   * {@code key}, simply waits for that thread to finish and returns its loaded value. Note that
048   * multiple threads can concurrently load values for distinct keys.
049   *
050   * <p>Caches loaded by a {@link CacheLoader} will call {@link CacheLoader#load} to load new values
051   * into the cache. Newly loaded values are added to the cache using {@code
052   * Cache.asMap().putIfAbsent} after loading has completed; if another value was associated with
053   * {@code key} while the new value was loading then a removal notification will be sent for the
054   * new value.
055   *
056   * <p>If the cache loader associated with this cache is known not to throw checked exceptions,
057   * then prefer {@link #getUnchecked} over this method.
058   *
059   * @throws ExecutionException if a checked exception was thrown while loading the value. ({@code
060   *     ExecutionException} is thrown <a
061   *     href="https://github.com/google/guava/wiki/CachesExplained#interruption">even if
062   *     computation was interrupted by an {@code InterruptedException}</a>.)
063   * @throws UncheckedExecutionException if an unchecked exception was thrown while loading the
064   *     value
065   * @throws ExecutionError if an error was thrown while loading the value
066   */
067  V get(K key) throws ExecutionException;
068
069  /**
070   * Returns the value associated with {@code key} in this cache, first loading that value if
071   * necessary. No observable state associated with this cache is modified until loading completes.
072   * Unlike {@link #get}, this method does not throw a checked exception, and thus should only be
073   * used in situations where checked exceptions are not thrown by the cache loader.
074   *
075   * <p>If another call to {@link #get} or {@link #getUnchecked} is currently loading the value for
076   * {@code key}, simply waits for that thread to finish and returns its loaded value. Note that
077   * multiple threads can concurrently load values for distinct keys.
078   *
079   * <p>Caches loaded by a {@link CacheLoader} will call {@link CacheLoader#load} to load new values
080   * into the cache. Newly loaded values are added to the cache using {@code
081   * Cache.asMap().putIfAbsent} after loading has completed; if another value was associated with
082   * {@code key} while the new value was loading then a removal notification will be sent for the
083   * new value.
084   *
085   * <p><b>Warning:</b> this method silently converts checked exceptions to unchecked exceptions,
086   * and should not be used with cache loaders which throw checked exceptions. In such cases use
087   * {@link #get} instead.
088   *
089   * @throws UncheckedExecutionException if an exception was thrown while loading the value. (As
090   *     explained in the last paragraph above, this should be an unchecked exception only.)
091   * @throws ExecutionError if an error was thrown while loading the value
092   */
093  V getUnchecked(K key);
094
095  /**
096   * Returns a map of the values associated with {@code keys}, creating or retrieving those values
097   * if necessary. The returned map contains entries that were already cached, combined with newly
098   * loaded entries; it will never contain null keys or values.
099   *
100   * <p>Caches loaded by a {@link CacheLoader} will issue a single request to {@link
101   * CacheLoader#loadAll} for all keys which are not already present in the cache. All entries
102   * returned by {@link CacheLoader#loadAll} will be stored in the cache, over-writing any
103   * previously cached values. This method will throw an exception if {@link CacheLoader#loadAll}
104   * returns {@code null}, returns a map containing null keys or values, or fails to return an entry
105   * for each requested key.
106   *
107   * <p>Note that duplicate elements in {@code keys}, as determined by {@link Object#equals}, will
108   * be ignored.
109   *
110   * @throws ExecutionException if a checked exception was thrown while loading the value. ({@code
111   *     ExecutionException} is thrown <a
112   *     href="https://github.com/google/guava/wiki/CachesExplained#interruption">even if
113   *     computation was interrupted by an {@code InterruptedException}</a>.)
114   * @throws UncheckedExecutionException if an unchecked exception was thrown while loading the
115   *     values
116   * @throws ExecutionError if an error was thrown while loading the values
117   * @since 11.0
118   */
119  ImmutableMap<K, V> getAll(Iterable<? extends K> keys) throws ExecutionException;
120
121  /**
122   * @deprecated Provided to satisfy the {@code Function} interface; use {@link #get} or {@link
123   *     #getUnchecked} instead.
124   * @throws UncheckedExecutionException if an exception was thrown while loading the value. (As
125   *     described in the documentation for {@link #getUnchecked}, {@code LoadingCache} should be
126   *     used as a {@code Function} only with cache loaders that throw only unchecked exceptions.)
127   */
128  @Deprecated
129  @Override
130  V apply(K key);
131
132  /**
133   * Loads a new value for {@code key}, possibly asynchronously. While the new value is loading the
134   * previous value (if any) will continue to be returned by {@code get(key)} unless it is evicted.
135   * If the new value is loaded successfully it will replace the previous value in the cache; if an
136   * exception is thrown while refreshing the previous value will remain, <i>and the exception will
137   * be logged (using {@link java.util.logging.Logger}) and swallowed</i>.
138   *
139   * <p>Caches loaded by a {@link CacheLoader} will call {@link CacheLoader#reload} if the cache
140   * currently contains a value for {@code key}, and {@link CacheLoader#load} otherwise. Loading is
141   * asynchronous only if {@link CacheLoader#reload} was overridden with an asynchronous
142   * implementation.
143   *
144   * <p>Returns without doing anything if another thread is currently loading the value for {@code
145   * key}. If the cache loader associated with this cache performs refresh asynchronously then this
146   * method may return before refresh completes.
147   *
148   * @since 11.0
149   */
150  void refresh(K key);
151
152  /**
153   * {@inheritDoc}
154   *
155   * <p><b>Note that although the view <i>is</i> modifiable, no method on the returned map will ever
156   * cause entries to be automatically loaded.</b>
157   */
158  @Override
159  ConcurrentMap<K, V> asMap();
160}