Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2011 The Guava Authors
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package com.google.common.cache;
 
 
A semi-persistent mapping from keys to values. Values are automatically loaded by the cache, and are stored in the cache until either evicted or manually invalidated.

Implementations of this interface are expected to be thread-safe, and can be safely accessed by multiple concurrent threads.

When evaluated as a com.google.common.base.Function, a cache yields the same result as invoking getUnchecked(java.lang.Object).

Note that while this class is still annotated as com.google.common.annotations.Beta, the API is frozen from a consumer's standpoint. In other words existing methods are all considered non-Beta and won't be changed without going through an 18 month deprecation cycle; however new methods may be added at any time.

Author(s):
Charles Fry
Since:
11.0
 
 public interface LoadingCache<K, V> extends Cache<K, V>, Function<K, V> {

  
Returns the value associated with key in this cache, first loading that value if necessary. No observable state associated with this cache is modified until loading completes.

If another call to get(java.lang.Object) or getUnchecked(java.lang.Object) is currently loading the value for key, simply waits for that thread to finish and returns its loaded value. Note that multiple threads can concurrently load values for distinct keys.

Caches loaded by a CacheLoader will call CacheLoader.load(java.lang.Object) to load new values into the cache. Newly loaded values are added to the cache using Cache.asMap().putIfAbsent after loading has completed; if another value was associated with key while the new value was loading then a removal notification will be sent for the new value.

If the cache loader associated with this cache is known not to throw checked exceptions, then prefer getUnchecked(java.lang.Object) over this method.

Throws:
java.util.concurrent.ExecutionException if a checked exception was thrown while loading the value. (ExecutionException is thrown even if computation was interrupted by an InterruptedException.)
com.google.common.util.concurrent.UncheckedExecutionException if an unchecked exception was thrown while loading the value
com.google.common.util.concurrent.ExecutionError if an error was thrown while loading the value
 
   V get(K keythrows ExecutionException;

  
Returns the value associated with key in this cache, first loading that value if necessary. No observable state associated with this cache is modified until loading completes. Unlike get(java.lang.Object), this method does not throw a checked exception, and thus should only be used in situations where checked exceptions are not thrown by the cache loader.

If another call to get(java.lang.Object) or getUnchecked(java.lang.Object) is currently loading the value for key, simply waits for that thread to finish and returns its loaded value. Note that multiple threads can concurrently load values for distinct keys.

Caches loaded by a CacheLoader will call CacheLoader.load(java.lang.Object) to load new values into the cache. Newly loaded values are added to the cache using Cache.asMap().putIfAbsent after loading has completed; if another value was associated with key while the new value was loading then a removal notification will be sent for the new value.

Warning: this method silently converts checked exceptions to unchecked exceptions, and should not be used with cache loaders which throw checked exceptions. In such cases use get(java.lang.Object) instead.

Throws:
com.google.common.util.concurrent.UncheckedExecutionException if an exception was thrown while loading the value. (As explained in the last paragraph above, this should be an unchecked exception only.)
com.google.common.util.concurrent.ExecutionError if an error was thrown while loading the value
  V getUnchecked(K key);

  
Returns a map of the values associated with keys, creating or retrieving those values if necessary. The returned map contains entries that were already cached, combined with newly loaded entries; it will never contain null keys or values.

Caches loaded by a CacheLoader will issue a single request to CacheLoader.loadAll(java.lang.Iterable) for all keys which are not already present in the cache. All entries returned by CacheLoader.loadAll(java.lang.Iterable) will be stored in the cache, over-writing any previously cached values. This method will throw an exception if CacheLoader.loadAll(java.lang.Iterable) returns null, returns a map containing null keys or values, or fails to return an entry for each requested key.

Note that duplicate elements in keys, as determined by java.lang.Object.equals(java.lang.Object), will be ignored.

Throws:
java.util.concurrent.ExecutionException if a checked exception was thrown while loading the value. (ExecutionException is thrown even if computation was interrupted by an InterruptedException.)
com.google.common.util.concurrent.UncheckedExecutionException if an unchecked exception was thrown while loading the values
com.google.common.util.concurrent.ExecutionError if an error was thrown while loading the values
Since:
11.0
  ImmutableMap<K, V> getAll(Iterable<? extends K> keysthrows ExecutionException;

  

Deprecated:
Provided to satisfy the Function interface; use get(java.lang.Object) or getUnchecked(java.lang.Object) instead.
Throws:
com.google.common.util.concurrent.UncheckedExecutionException if an exception was thrown while loading the value. (As described in the documentation for getUnchecked(java.lang.Object), LoadingCache should be used as a Function only with cache loaders that throw only unchecked exceptions.)
  V apply(K key);

  
Loads a new value for key key, possibly asynchronously. While the new value is loading the previous value (if any) will continue to be returned by get(key) unless it is evicted. If the new value is loaded successfully it will replace the previous value in the cache; if an exception is thrown while refreshing the previous value will remain, and the exception will be logged (using java.util.logging.Logger) and swallowed.

Caches loaded by a CacheLoader will call CacheLoader.reload(java.lang.Object,java.lang.Object) if the cache currently contains a value for key, and CacheLoader.load(java.lang.Object) otherwise. Loading is asynchronous only if CacheLoader.reload(java.lang.Object,java.lang.Object) was overridden with an asynchronous implementation.

Returns without doing anything if another thread is currently loading the value for key. If the cache loader associated with this cache performs refresh asynchronously then this method may return before refresh completes.

Since:
11.0
  void refresh(K key);

  

Note that although the view is modifiable, no method on the returned map will ever cause entries to be automatically loaded.

  ConcurrentMap<K, V> asMap();
New to GrepCode? Check out our FAQ X