/*
* 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 org.glassfish.jersey.internal.guava;
import static org.glassfish.jersey.internal.guava.Preconditions.checkNotNull;
Computes or retrieves values, based on a key, for use in populating a LoadingCache
.
Most implementations will only need to implement load
. Other methods may be overridden as desired.
Usage example:
<p>
CacheLoader<Key, Graph> loader = new CacheLoader<Key, Graph>() {
public Graph load(Key key) throws AnyException {
return createExpensiveGraph(key);
}
};
LoadingCache<Key, Graph> cache = CacheBuilder.newBuilder().build(loader);
Author: Charles Fry Since: 10.0
/**
* Computes or retrieves values, based on a key, for use in populating a {@link LoadingCache}.
* <p>
* <p>Most implementations will only need to implement {@link #load}. Other methods may be
* overridden as desired.
* <p>
* <p>Usage example: <pre> {@code
* <p>
* CacheLoader<Key, Graph> loader = new CacheLoader<Key, Graph>() {
* public Graph load(Key key) throws AnyException {
* return createExpensiveGraph(key);
* }
* };
* LoadingCache<Key, Graph> cache = CacheBuilder.newBuilder().build(loader);}</pre>
*
* @author Charles Fry
* @since 10.0
*/
public abstract class CacheLoader<K, V> {
Constructor for use by subclasses.
/**
* Constructor for use by subclasses.
*/
protected CacheLoader() {
}
Computes or retrieves the value corresponding to key
. Params: - key – the non-null key whose value should be loaded
Throws: - Exception – if unable to load the result
- InterruptedException – if this method is interrupted.
InterruptedException
is treated like any other Exception
in all respects except that, when it is caught, the thread's interrupt status is set
Returns: the value associated with key
; must not be null
/**
* Computes or retrieves the value corresponding to {@code key}.
*
* @param key the non-null key whose value should be loaded
* @return the value associated with {@code key}; <b>must not be null</b>
* @throws Exception if unable to load the result
* @throws InterruptedException if this method is interrupted. {@code InterruptedException} is
* treated like any other {@code Exception} in all respects except that, when it is caught,
* the thread's interrupt status is set
*/
public abstract V load(K key) throws Exception;
Computes or retrieves a replacement value corresponding to an already-cached key
. This method is called when an existing cache entry is refreshed by CacheBuilder.refreshAfterWrite
, or through a call to LoadingCache.refresh
.
This implementation synchronously delegates to load
. It is recommended that it be overridden with an asynchronous implementation when using CacheBuilder.refreshAfterWrite
.
Note: all exceptions thrown by this method will be logged and then swallowed.
Params: - key – the non-null key whose value should be loaded
- oldValue – the non-null old value corresponding to
key
Throws: - Exception – if unable to reload the result
- InterruptedException – if this method is interrupted.
InterruptedException
is treated like any other Exception
in all respects except that, when it is caught, the thread's interrupt status is set
Returns: the future new value associated with key
; must not be null, must not return null Since: 11.0
/**
* Computes or retrieves a replacement value corresponding to an already-cached {@code key}. This
* method is called when an existing cache entry is refreshed by
* {@link CacheBuilder#refreshAfterWrite}, or through a call to {@link LoadingCache#refresh}.
* <p>
* <p>This implementation synchronously delegates to {@link #load}. It is recommended that it be
* overridden with an asynchronous implementation when using
* {@link CacheBuilder#refreshAfterWrite}.
* <p>
* <p><b>Note:</b> <i>all exceptions thrown by this method will be logged and then swallowed</i>.
*
* @param key the non-null key whose value should be loaded
* @param oldValue the non-null old value corresponding to {@code key}
* @return the future new value associated with {@code key};
* <b>must not be null, must not return null</b>
* @throws Exception if unable to reload the result
* @throws InterruptedException if this method is interrupted. {@code InterruptedException} is
* treated like any other {@code Exception} in all respects except that, when it is caught,
* the thread's interrupt status is set
* @since 11.0
*/
public ListenableFuture<V> reload(K key, V oldValue) throws Exception {
checkNotNull(key);
checkNotNull(oldValue);
return Futures.immediateFuture(load(key));
}
Thrown to indicate that an invalid response was returned from a call to CacheLoader
. Since: 11.0
/**
* Thrown to indicate that an invalid response was returned from a call to {@link CacheLoader}.
*
* @since 11.0
*/
public static final class InvalidCacheLoadException extends RuntimeException {
public InvalidCacheLoadException(String message) {
super(message);
}
}
}