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