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.collect.ImmutableMap;
019import com.google.common.collect.Maps;
020import java.util.Map;
021import java.util.Map.Entry;
022import java.util.concurrent.Callable;
023import java.util.concurrent.ConcurrentMap;
024import java.util.concurrent.ExecutionException;
025
026/**
027 * This class provides a skeletal implementation of the {@code Cache} interface to minimize the
028 * effort required to implement this interface.
029 *
030 * <p>To implement a cache, the programmer needs only to extend this class and provide an
031 * implementation for the {@link #put} and {@link #getIfPresent} methods. {@link #getAllPresent} is
032 * implemented in terms of {@link #getIfPresent}; {@link #putAll} is implemented in terms of {@link
033 * #put}, {@link #invalidateAll(Iterable)} is implemented in terms of {@link #invalidate}. The
034 * method {@link #cleanUp} is a no-op. All other methods throw an {@link
035 * UnsupportedOperationException}.
036 *
037 * @author Charles Fry
038 * @since 10.0
039 */
040@GwtCompatible
041public abstract class AbstractCache<K, V> implements Cache<K, V> {
042
043  /** Constructor for use by subclasses. */
044  protected AbstractCache() {}
045
046  /** @since 11.0 */
047  @Override
048  public V get(K key, Callable<? extends V> valueLoader) throws ExecutionException {
049    throw new UnsupportedOperationException();
050  }
051
052  /**
053   * {@inheritDoc}
054   *
055   * <p>This implementation of {@code getAllPresent} lacks any insight into the internal cache data
056   * structure, and is thus forced to return the query keys instead of the cached keys. This is only
057   * possible with an unsafe cast which requires {@code keys} to actually be of type {@code K}.
058   *
059   * @since 11.0
060   */
061  @Override
062  public ImmutableMap<K, V> getAllPresent(Iterable<?> keys) {
063    Map<K, V> result = Maps.newLinkedHashMap();
064    for (Object key : keys) {
065      if (!result.containsKey(key)) {
066        @SuppressWarnings("unchecked")
067        K castKey = (K) key;
068        V value = getIfPresent(key);
069        if (value != null) {
070          result.put(castKey, value);
071        }
072      }
073    }
074    return ImmutableMap.copyOf(result);
075  }
076
077  /** @since 11.0 */
078  @Override
079  public void put(K key, V value) {
080    throw new UnsupportedOperationException();
081  }
082
083  /** @since 12.0 */
084  @Override
085  public void putAll(Map<? extends K, ? extends V> m) {
086    for (Entry<? extends K, ? extends V> entry : m.entrySet()) {
087      put(entry.getKey(), entry.getValue());
088    }
089  }
090
091  @Override
092  public void cleanUp() {}
093
094  @Override
095  public long size() {
096    throw new UnsupportedOperationException();
097  }
098
099  @Override
100  public void invalidate(Object key) {
101    throw new UnsupportedOperationException();
102  }
103
104  /** @since 11.0 */
105  @Override
106  public void invalidateAll(Iterable<?> keys) {
107    for (Object key : keys) {
108      invalidate(key);
109    }
110  }
111
112  @Override
113  public void invalidateAll() {
114    throw new UnsupportedOperationException();
115  }
116
117  @Override
118  public CacheStats stats() {
119    throw new UnsupportedOperationException();
120  }
121
122  @Override
123  public ConcurrentMap<K, V> asMap() {
124    throw new UnsupportedOperationException();
125  }
126
127  /**
128   * Accumulates statistics during the operation of a {@link Cache} for presentation by {@link
129   * Cache#stats}. This is solely intended for consumption by {@code Cache} implementors.
130   *
131   * @since 10.0
132   */
133  public interface StatsCounter {
134    /**
135     * Records cache hits. This should be called when a cache request returns a cached value.
136     *
137     * @param count the number of hits to record
138     * @since 11.0
139     */
140    void recordHits(int count);
141
142    /**
143     * Records cache misses. This should be called when a cache request returns a value that was not
144     * found in the cache. This method should be called by the loading thread, as well as by threads
145     * blocking on the load. Multiple concurrent calls to {@link Cache} lookup methods with the same
146     * key on an absent value should result in a single call to either {@code recordLoadSuccess} or
147     * {@code recordLoadException} and multiple calls to this method, despite all being served by
148     * the results of a single load operation.
149     *
150     * @param count the number of misses to record
151     * @since 11.0
152     */
153    void recordMisses(int count);
154
155    /**
156     * Records the successful load of a new entry. This should be called when a cache request causes
157     * an entry to be loaded, and the loading completes successfully. In contrast to {@link
158     * #recordMisses}, this method should only be called by the loading thread.
159     *
160     * @param loadTime the number of nanoseconds the cache spent computing or retrieving the new
161     *     value
162     */
163    void recordLoadSuccess(long loadTime);
164
165    /**
166     * Records the failed load of a new entry. This should be called when a cache request causes an
167     * entry to be loaded, but an exception is thrown while loading the entry. In contrast to {@link
168     * #recordMisses}, this method should only be called by the loading thread.
169     *
170     * @param loadTime the number of nanoseconds the cache spent computing or retrieving the new
171     *     value prior to an exception being thrown
172     */
173    void recordLoadException(long loadTime);
174
175    /**
176     * Records the eviction of an entry from the cache. This should only been called when an entry
177     * is evicted due to the cache's eviction strategy, and not as a result of manual {@linkplain
178     * Cache#invalidate invalidations}.
179     */
180    void recordEviction();
181
182    /**
183     * Returns a snapshot of this counter's values. Note that this may be an inconsistent view, as
184     * it may be interleaved with update operations.
185     */
186    CacheStats snapshot();
187  }
188
189  /**
190   * A thread-safe {@link StatsCounter} implementation for use by {@link Cache} implementors.
191   *
192   * @since 10.0
193   */
194  public static final class SimpleStatsCounter implements StatsCounter {
195    private final LongAddable hitCount = LongAddables.create();
196    private final LongAddable missCount = LongAddables.create();
197    private final LongAddable loadSuccessCount = LongAddables.create();
198    private final LongAddable loadExceptionCount = LongAddables.create();
199    private final LongAddable totalLoadTime = LongAddables.create();
200    private final LongAddable evictionCount = LongAddables.create();
201
202    /** Constructs an instance with all counts initialized to zero. */
203    public SimpleStatsCounter() {}
204
205    /** @since 11.0 */
206    @Override
207    public void recordHits(int count) {
208      hitCount.add(count);
209    }
210
211    /** @since 11.0 */
212    @Override
213    public void recordMisses(int count) {
214      missCount.add(count);
215    }
216
217    @Override
218    public void recordLoadSuccess(long loadTime) {
219      loadSuccessCount.increment();
220      totalLoadTime.add(loadTime);
221    }
222
223    @Override
224    public void recordLoadException(long loadTime) {
225      loadExceptionCount.increment();
226      totalLoadTime.add(loadTime);
227    }
228
229    @Override
230    public void recordEviction() {
231      evictionCount.increment();
232    }
233
234    @Override
235    public CacheStats snapshot() {
236      return new CacheStats(
237          hitCount.sum(),
238          missCount.sum(),
239          loadSuccessCount.sum(),
240          loadExceptionCount.sum(),
241          totalLoadTime.sum(),
242          evictionCount.sum());
243    }
244
245    /** Increments all counters by the values in {@code other}. */
246    public void incrementBy(StatsCounter other) {
247      CacheStats otherStats = other.snapshot();
248      hitCount.add(otherStats.hitCount());
249      missCount.add(otherStats.missCount());
250      loadSuccessCount.add(otherStats.loadSuccessCount());
251      loadExceptionCount.add(otherStats.loadExceptionCount());
252      totalLoadTime.add(otherStats.totalLoadTime());
253      evictionCount.add(otherStats.evictionCount());
254    }
255  }
256}