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