A semi-persistent mapping from keys to values. Cache entries are manually added using #get(Object, Callable) or #put(Object, Object), and are stored in the cache until either evicted or manually invalidated. Implementations of this interface are expected to be thread-safe, and ca
| 38 | |
| 39 | |
| 40 | @GwtCompatible |
| 41 | public interface Cache<K, V> { |
| 42 | |
| 43 | /** |
| 44 | * Returns the value associated with {@code key} in this cache, or {@code null} if there is no |
| 45 | * cached value for {@code key}. |
| 46 | * |
| 47 | * @since 11.0 |
| 48 | */ |
| 49 | |
| 50 | @Nullable |
| 51 | V getIfPresent(Object key); |
| 52 | |
| 53 | /** |
| 54 | * Returns the value associated with {@code key} in this cache, obtaining that value from {@code |
| 55 | * loader} if necessary. The method improves upon the conventional "if cached, return; otherwise |
| 56 | * create, cache and return" pattern. For further improvements, use {@link LoadingCache} and its |
| 57 | * {@link LoadingCache#get(Object) get(K)} method instead of this one. |
| 58 | * |
| 59 | * <p>Among the improvements that this method and {@code LoadingCache.get(K)} both provide are: |
| 60 | * |
| 61 | * <ul> |
| 62 | * <li>{@linkplain LoadingCache#get(Object) awaiting the result of a pending load} rather than |
| 63 | * starting a redundant one |
| 64 | * <li>eliminating the error-prone caching boilerplate |
| 65 | * <li>tracking load {@linkplain #stats statistics} |
| 66 | * </ul> |
| 67 | * |
| 68 | * <p>Among the further improvements that {@code LoadingCache} can provide but this method cannot: |
| 69 | * |
| 70 | * <ul> |
| 71 | * <li>consolidation of the loader logic to {@linkplain CacheBuilder#build(CacheLoader) a single |
| 72 | * authoritative location} |
| 73 | * <li>{@linkplain LoadingCache#refresh refreshing of entries}, including {@linkplain |
| 74 | * CacheBuilder#refreshAfterWrite automated refreshing} |
| 75 | * <li>{@linkplain LoadingCache#getAll bulk loading requests}, including {@linkplain |
| 76 | * CacheLoader#loadAll bulk loading implementations} |
| 77 | * </ul> |
| 78 | * |
| 79 | * <p><b>Warning:</b> For any given key, every {@code loader} used with it should compute the same |
| 80 | * value. Otherwise, a call that passes one {@code loader} may return the result of another call |
| 81 | * with a differently behaving {@code loader}. For example, a call that requests a short timeout |
| 82 | * for an RPC may wait for a similar call that requests a long timeout, or a call by an |
| 83 | * unprivileged user may return a resource accessible only to a privileged user making a similar |
| 84 | * call. To prevent this problem, create a key object that includes all values that affect the |
| 85 | * result of the query. Or use {@code LoadingCache.get(K)}, which lacks the ability to refer to |
| 86 | * state other than that in the key. |
| 87 | * |
| 88 | * <p><b>Warning:</b> as with {@link CacheLoader#load}, {@code loader} <b>must not</b> return |
| 89 | * {@code null}; it may either return a non-null value or throw an exception. |
| 90 | * |
| 91 | * <p>No observable state associated with this cache is modified until loading completes. |
| 92 | * |
| 93 | * @throws ExecutionException if a checked exception was thrown while loading the value |
| 94 | * @throws UncheckedExecutionException if an unchecked exception was thrown while loading the |
| 95 | * value |
| 96 | * @throws ExecutionError if an error was thrown while loading the value |
| 97 | * |
nothing calls this directly
no outgoing calls
no test coverage detected