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