001 /* 002 * Copyright (C) 2011 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017 package com.google.common.cache; 018 019 import com.google.common.annotations.Beta; 020 import com.google.common.annotations.GwtCompatible; 021 import com.google.common.base.Function; 022 import com.google.common.collect.ImmutableMap; 023 import com.google.common.util.concurrent.ExecutionError; 024 import com.google.common.util.concurrent.UncheckedExecutionException; 025 026 import java.util.concurrent.ConcurrentMap; 027 import java.util.concurrent.ExecutionException; 028 029 /** 030 * A semi-persistent mapping from keys to values. Values are automatically loaded by the cache, 031 * and are stored in the cache until either evicted or manually invalidated. 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>All methods other than {@link #get} and {@link #getUnchecked} are optional. 037 * 038 * <p>When evaluated as a {@link Function}, a cache yields the same result as invoking 039 * {@link #getUnchecked}. 040 * 041 * @author Charles Fry 042 * @since 11.0 043 */ 044 @Beta 045 @GwtCompatible 046 public interface LoadingCache<K, V> extends Cache<K, V>, Function<K, V> { 047 048 /** 049 * Returns the value associated with {@code key} in this cache, first loading that value if 050 * necessary. No observable state associated with this cache is modified until loading completes. 051 * 052 * <p>If another call to {@link #get} or {@link #getUnchecked} is currently loading the value for 053 * {@code key}, simply waits for that thread to finish and returns its loaded value. Note that 054 * multiple threads can concurrently load values for distinct keys. 055 * 056 * <p>Caches loaded by a {@link CacheLoader} will call {@link CacheLoader#load} to load new values 057 * into the cache. Newly loaded values are added to the cache using 058 * {@code Cache.asMap().putIfAbsent} after loading has completed; if another value was associated 059 * with {@code key} while the new value was loading then a removal notification will be sent for 060 * the new value. 061 * 062 * <p>If the cache loader associated with this cache is known not to throw checked 063 * exceptions, then prefer {@link #getUnchecked} over this method. 064 * 065 * @throws ExecutionException if a checked exception was thrown while loading the value 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 V get(K key) throws ExecutionException; 071 072 /** 073 * Returns the value associated with {@code key} in this cache, first loading that value if 074 * necessary. No observable state associated with this cache is modified until loading 075 * completes. Unlike {@link #get}, this method does not throw a checked exception, and thus should 076 * only be used in situations where checked exceptions are not thrown by the cache loader. 077 * 078 * <p>If another call to {@link #get} or {@link #getUnchecked} is currently loading the value for 079 * {@code key}, simply waits for that thread to finish and returns its loaded value. Note that 080 * multiple threads can concurrently load values for distinct keys. 081 * 082 * <p>Caches loaded by a {@link CacheLoader} will call {@link CacheLoader#load} to load new values 083 * into the cache. Newly loaded values are added to the cache using 084 * {@code Cache.asMap().putIfAbsent} after loading has completed; if another value was associated 085 * with {@code key} while the new value was loading then a removal notification will be sent for 086 * the new value. 087 * 088 * <p><b>Warning:</b> this method silently converts checked exceptions to unchecked exceptions, 089 * and should not be used with cache loaders which throw checked exceptions. In such cases use 090 * {@link #get} instead. 091 * 092 * @throws UncheckedExecutionException if an exception was thrown while loading the value, 093 * regardless of whether the exception was checked or unchecked 094 * @throws ExecutionError if an error was thrown while loading the value 095 */ 096 V getUnchecked(K key); 097 098 /** 099 * Returns a map of the values associated with {@code keys}, creating or retrieving those values 100 * if necessary. The returned map contains entries that were already cached, combined with newly 101 * loaded entries; it will never contain null keys or values. 102 * 103 * <p>Caches loaded by a {@link CacheLoader} will issue a single request to 104 * {@link CacheLoader#loadAll} for all keys which are not already present in the cache. All 105 * entries returned by {@link CacheLoader#loadAll} will be stored in the cache, over-writing 106 * any previously cached values. This method will throw an exception if 107 * {@link CacheLoader#loadAll} returns {@code null}, returns a map containing null keys or values, 108 * or fails to return an entry for each requested key. 109 * 110 * <p>Note that duplicate elements in {@code keys}, as determined by {@link Object#equals}, will 111 * be ignored. 112 * 113 * @throws ExecutionException if a checked exception was thrown while loading the values 114 * @throws UncheckedExecutionException if an unchecked exception was thrown while loading the 115 * values 116 * @throws ExecutionError if an error was thrown while loading the values 117 * @since 11.0 118 */ 119 ImmutableMap<K, V> getAll(Iterable<? extends K> keys) throws ExecutionException; 120 121 /** 122 * Discouraged. Provided to satisfy the {@code Function} interface; use {@link #get} or 123 * {@link #getUnchecked} instead. 124 * 125 * @throws UncheckedExecutionException if an exception was thrown while loading the value, 126 * regardless of whether the exception was checked or unchecked 127 * @throws ExecutionError if an error was thrown while loading the value 128 */ 129 @Override 130 V apply(K key); 131 132 /** 133 * Loads a new value for key {@code key}, possibly asynchronously. While the new value is loading 134 * the previous value (if any) will continue to be returned by {@code get(key)} unless it is 135 * evicted. If the new value is loaded successfully it will replace the previous value in the 136 * cache; if an exception is thrown while refreshing the previous value will remain, <i>and the 137 * exception will be logged (using {@link java.util.logging.Logger}) and swallowed</i>. 138 * 139 * <p>Caches loaded by a {@link CacheLoader} will call {@link CacheLoader#reload} if the 140 * cache currently contains a value for {@code key}, and {@link CacheLoader#load} otherwise. 141 * 142 * <p>Returns without doing anything if another thread is currently loading the value for 143 * {@code key}. If the cache loader associated with this cache performs refresh asynchronously 144 * then this method may return before refresh completes. 145 * 146 * @since 11.0 147 */ 148 void refresh(K key); 149 150 /** 151 * {@inheritDoc} 152 * 153 * <p><b>Note that although the view <i>is</i> modifiable, no method on the returned map will ever 154 * cause entries to be automatically loaded.</b> 155 */ 156 @Override 157 ConcurrentMap<K, V> asMap(); 158 }