Example usage for com.google.common.cache CacheLoader load

List of usage examples for com.google.common.cache CacheLoader load

Introduction

In this page you can find the example usage for com.google.common.cache CacheLoader load.

Prototype

public abstract V load(K key) throws Exception;

Source Link

Document

Computes or retrieves the value corresponding to key .

Usage

From source file:net.conquiris.lucene.search.GuavaCachingFilter.java

/**
 * Caching factory method. The primary cache is that from the filter key to the cachinf filter.
 * The seondary cache is that contained in the caching filter itself.
 * @param loader Filter loader. Its result will be wrapped in a caching filter.
 * @param primarySize the maximum size of the primary cache.
 * @param primaryDuration the length of time after an entry is last accessed that it should be
 *          automatically removed from the primary cache.
 * @param primaryUnit the unit that {@code primaryDuration} is expressed in
 * @param secondarySize the maximum size of the secondary cache.
 * @param secondaryDuration the length of time after an entry is last accessed that it should be
 *          automatically removed from the secondary cache.
 * @param secondaryUnit the unit that {@code secondaryDuration} is expressed in
 * @throws IllegalArgumentException if any of the sizes is negative
 * @throws IllegalArgumentException if any of the durations is negative
 *//*from   w  w  w  .  j  a v a 2s.  c o  m*/
public static <K, V extends Filter> LoadingCache<K, Filter> cachingFactory(final CacheLoader<K, V> loader,
        final int primarySize, final long primaryDuration, final TimeUnit primaryUnit, final int secondarySize,
        final long secondaryDuration, final TimeUnit secondaryUnit) {
    checkArgument(primarySize >= 0 && primaryDuration >= 0 && secondarySize >= 0 && secondaryDuration >= 0);
    checkNotNull(primaryUnit);
    checkNotNull(secondaryUnit);
    final CacheLoader<K, Filter> primaryLoader = new CacheLoader<K, Filter>() {
        public Filter load(K key) throws Exception {
            return new GuavaCachingFilter(loader.load(key), secondarySize, secondaryDuration, secondaryUnit);
        }
    };
    return CacheBuilder.newBuilder().softValues().maximumSize(primarySize)
            .expireAfterAccess(primaryDuration, primaryUnit).build(primaryLoader);
}