• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.cache;
18 
19 import com.google.common.annotations.Beta;
20 import com.google.common.annotations.GwtCompatible;
21 import com.google.common.base.Function;
22 import com.google.common.collect.ImmutableMap;
23 import com.google.common.util.concurrent.ExecutionError;
24 import com.google.common.util.concurrent.UncheckedExecutionException;
25 
26 import java.util.concurrent.ConcurrentMap;
27 import java.util.concurrent.ExecutionException;
28 
29 /**
30  * A semi-persistent mapping from keys to values. Values are automatically loaded by the cache,
31  * and are stored in the cache until either evicted or manually invalidated.
32  *
33  * <p>Implementations of this interface are expected to be thread-safe, and can be safely accessed
34  * by multiple concurrent threads.
35  *
36  * <p>When evaluated as a {@link Function}, a cache yields the same result as invoking
37  * {@link #getUnchecked}.
38  *
39  * <p>Note that while this class is still annotated as {@link Beta}, the API is frozen from a
40  * consumer's standpoint. In other words existing methods are all considered {@code non-Beta} and
41  * won't be changed without going through an 18 month deprecation cycle; however new methods may be
42  * added at any time.
43  *
44  * @author Charles Fry
45  * @since 11.0
46  */
47 @Beta
48 @GwtCompatible
49 public interface LoadingCache<K, V> extends Cache<K, V>, Function<K, V> {
50 
51   /**
52    * Returns the value associated with {@code key} in this cache, first loading that value if
53    * necessary. No observable state associated with this cache is modified until loading completes.
54    *
55    * <p>If another call to {@link #get} or {@link #getUnchecked} is currently loading the value for
56    * {@code key}, simply waits for that thread to finish and returns its loaded value. Note that
57    * multiple threads can concurrently load values for distinct keys.
58    *
59    * <p>Caches loaded by a {@link CacheLoader} will call {@link CacheLoader#load} to load new values
60    * into the cache. Newly loaded values are added to the cache using
61    * {@code Cache.asMap().putIfAbsent} after loading has completed; if another value was associated
62    * with {@code key} while the new value was loading then a removal notification will be sent for
63    * the new value.
64    *
65    * <p>If the cache loader associated with this cache is known not to throw checked
66    * exceptions, then prefer {@link #getUnchecked} over this method.
67    *
68    * @throws ExecutionException if a checked exception was thrown while loading the value. ({@code
69    *     ExecutionException} is thrown <a
70    *     href="http://code.google.com/p/guava-libraries/wiki/CachesExplained#Interruption">even if
71    *     computation was interrupted by an {@code InterruptedException}</a>.)
72    * @throws UncheckedExecutionException if an unchecked exception was thrown while loading the
73    *     value
74    * @throws ExecutionError if an error was thrown while loading the value
75    */
get(K key)76   V get(K key) throws ExecutionException;
77 
78   /**
79    * Returns the value associated with {@code key} in this cache, first loading that value if
80    * necessary. No observable state associated with this cache is modified until loading
81    * completes. Unlike {@link #get}, this method does not throw a checked exception, and thus should
82    * only be used in situations where checked exceptions are not thrown by the cache loader.
83    *
84    * <p>If another call to {@link #get} or {@link #getUnchecked} is currently loading the value for
85    * {@code key}, simply waits for that thread to finish and returns its loaded value. Note that
86    * multiple threads can concurrently load values for distinct keys.
87    *
88    * <p>Caches loaded by a {@link CacheLoader} will call {@link CacheLoader#load} to load new values
89    * into the cache. Newly loaded values are added to the cache using
90    * {@code Cache.asMap().putIfAbsent} after loading has completed; if another value was associated
91    * with {@code key} while the new value was loading then a removal notification will be sent for
92    * the new value.
93    *
94    * <p><b>Warning:</b> this method silently converts checked exceptions to unchecked exceptions,
95    * and should not be used with cache loaders which throw checked exceptions. In such cases use
96    * {@link #get} instead.
97    *
98    * @throws UncheckedExecutionException if an exception was thrown while loading the value. (As
99    *     explained in the last paragraph above, this should be an unchecked exception only.)
100    * @throws ExecutionError if an error was thrown while loading the value
101    */
getUnchecked(K key)102   V getUnchecked(K key);
103 
104   /**
105    * Returns a map of the values associated with {@code keys}, creating or retrieving those values
106    * if necessary. The returned map contains entries that were already cached, combined with newly
107    * loaded entries; it will never contain null keys or values.
108    *
109    * <p>Caches loaded by a {@link CacheLoader} will issue a single request to
110    * {@link CacheLoader#loadAll} for all keys which are not already present in the cache. All
111    * entries returned by {@link CacheLoader#loadAll} will be stored in the cache, over-writing
112    * any previously cached values. This method will throw an exception if
113    * {@link CacheLoader#loadAll} returns {@code null}, returns a map containing null keys or values,
114    * or fails to return an entry for each requested key.
115    *
116    * <p>Note that duplicate elements in {@code keys}, as determined by {@link Object#equals}, will
117    * be ignored.
118    *
119    * @throws ExecutionException if a checked exception was thrown while loading the value. ({@code
120    *     ExecutionException} is thrown <a
121    *     href="http://code.google.com/p/guava-libraries/wiki/CachesExplained#Interruption">even if
122    *     computation was interrupted by an {@code InterruptedException}</a>.)
123    * @throws UncheckedExecutionException if an unchecked exception was thrown while loading the
124    *     values
125    * @throws ExecutionError if an error was thrown while loading the values
126    * @since 11.0
127    */
getAll(Iterable<? extends K> keys)128   ImmutableMap<K, V> getAll(Iterable<? extends K> keys) throws ExecutionException;
129 
130   /**
131    * @deprecated Provided to satisfy the {@code Function} interface; use {@link #get} or
132    *     {@link #getUnchecked} instead.
133    * @throws UncheckedExecutionException if an exception was thrown while loading the value. (As
134    *     described in the documentation for {@link #getUnchecked}, {@code LoadingCache} should be
135    *     used as a {@code Function} only with cache loaders that throw only unchecked exceptions.)
136    */
137   @Deprecated
138   @Override
apply(K key)139   V apply(K key);
140 
141   /**
142    * Loads a new value for key {@code key}, possibly asynchronously. While the new value is loading
143    * the previous value (if any) will continue to be returned by {@code get(key)} unless it is
144    * evicted. If the new value is loaded successfully it will replace the previous value in the
145    * cache; if an exception is thrown while refreshing the previous value will remain, <i>and the
146    * exception will be logged (using {@link java.util.logging.Logger}) and swallowed</i>.
147    *
148    * <p>Caches loaded by a {@link CacheLoader} will call {@link CacheLoader#reload} if the
149    * cache currently contains a value for {@code key}, and {@link CacheLoader#load} otherwise.
150    * Loading is asynchronous only if {@link CacheLoader#reload} was overridden with an
151    * asynchronous implementation.
152    *
153    * <p>Returns without doing anything if another thread is currently loading the value for
154    * {@code key}. If the cache loader associated with this cache performs refresh asynchronously
155    * then this method may return before refresh completes.
156    *
157    * @since 11.0
158    */
refresh(K key)159   void refresh(K key);
160 
161   /**
162    * {@inheritDoc}
163    *
164    * <p><b>Note that although the view <i>is</i> modifiable, no method on the returned map will ever
165    * cause entries to be automatically loaded.</b>
166    */
167   @Override
asMap()168   ConcurrentMap<K, V> asMap();
169 }
170