Example usage for com.google.common.cache CacheBuilder build

List of usage examples for com.google.common.cache CacheBuilder build

Introduction

In this page you can find the example usage for com.google.common.cache CacheBuilder build.

Prototype

public <K1 extends K, V1 extends V> LoadingCache<K1, V1> build(CacheLoader<? super K1, V1> loader) 

Source Link

Document

Builds a cache, which either returns an already-loaded value for a given key or atomically computes or retrieves it using the supplied CacheLoader .

Usage

From source file:net.sourcedestination.util.MemoizedFunction.java

/** modifies a function to cache its results
 * /*from  ww w.jav  a  2  s  .  com*/
 * @param f function to be memoized
 * @param builder a cache builder which creates a cache for mapping inputs to outputs
 * @return memozied function f
 */
public static <F, T> MemoizedFunction<F, T> memoize(final Function<F, T> f,
        final CacheBuilder<? super F, ? super T> builder) {
    final LoadingCache<F, T> cache = builder.build(CacheLoader.from(f::apply));
    return new MemoizedFunction<F, T>() {

        public T apply(final F input) {
            try {
                return cache.get(input);
            } catch (ExecutionException e) {
                //Functions can't throw checked exceptions, so this never happens
                return null;
            }
        }

        @Override
        public LoadingCache<F, T> getCache() {
            return cache;
        }

        @Override
        public Function<F, T> getOriginalFunction() {
            return f;
        }
    };
}

From source file:org.sprinkle.CacheTool.java

public static <K, V> Cache<K, CompletableFuture<Try<V>>> constructCache(
        CacheBuilder<K, CompletableFuture<Try<V>>> cacheBuilder,
        Function<K, CompletableFuture<Try<V>>> future) {
    return cacheBuilder.build(new CacheLoader<K, CompletableFuture<Try<V>>>() {
        public CompletableFuture<Try<V>> load(K key) { // no checked exception
            return future.apply(key);
        }/*  w  w  w.ja  va2 s . c  o m*/
    });
}

From source file:org.sprinkle.CacheTool.java

public static <K, V> Function<K, CompletableFuture<Try<V>>> withCache(
        CacheBuilder<K, CompletableFuture<Try<V>>> cacheBuilder,
        Function<K, CompletableFuture<Try<V>>> future) {
    final Cache<K, CompletableFuture<Try<V>>> cache = cacheBuilder
            .build(new CacheLoader<K, CompletableFuture<Try<V>>>() {
                public CompletableFuture<Try<V>> load(K key) { // no checked exception
                    return future.apply(key);
                }//from   ww  w.  j a va  2s.  c  o  m
            });

    // Close around the cache
    return withCache(cache);
}

From source file:org.sprinkle.CacheTool.java

public static <K, V, KK> Function<KK, CompletableFuture<Try<V>>> withCache(
        CacheBuilder<K, CompletableFuture<Try<V>>> cacheBuilder, Function<K, CompletableFuture<Try<V>>> future,
        Function<KK, Try<K>> getKeyFunc) {
    final Cache<K, CompletableFuture<Try<V>>> cache = cacheBuilder
            .build(new CacheLoader<K, CompletableFuture<Try<V>>>() {
                public CompletableFuture<Try<V>> load(K key) { // no checked exception
                    return future.apply(key);
                }//from   ww w .j  a  va 2s  .  c  om
            });

    // Close around the cache
    return withCache(cache, getKeyFunc);
}

From source file:org.eclipse.emf.compare.utils.EqualityHelper.java

/**
 * Create a cache as required by EqualityHelper.
 * //from  w  w w  .j a  va  2s .co  m
 * @param cacheBuilder
 *            The builder to use to instantiate the cache.
 * @return the new cache.
 */
public static LoadingCache<EObject, URI> createDefaultCache(CacheBuilder<Object, Object> cacheBuilder) {
    return cacheBuilder.build(CacheLoader.from(new URICacheFunction()));
}

From source file:com.vityuk.ginger.provider.DefaultLocalizationProvider.java

private static LoadingCache<Locale, PropertyResolver> createPropertyResolverCache(Builder builder,
        CacheLoader<Locale, PropertyResolver> cacheLoader) {
    CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder();
    if (builder.maxCacheTimeInSec >= 0) {
        cacheBuilder.expireAfterWrite(builder.maxCacheTimeInSec, TimeUnit.SECONDS);
    }/*from   ww  w. j a  va  2 s  . c  o m*/
    return cacheBuilder.build(cacheLoader);
}

From source file:com.google.javascript.jscomp.bundle.CachedTransformer.java

public CachedTransformer(Source.Transformer delegate, CacheBuilder<? super Source, ? super Source> builder) {
    this.cache = builder.build(CacheLoader.from(delegate::transform));
}

From source file:com.github.mjeanroy.springmvc.uadetector.cache.guava.GuavaCache.java

/**
 * Create Guava cache instance.//w w w .  jav  a  2 s.c om
 *
 * @param builder Guava cache builder.
 * @param parser User Agent parser.
 */
public GuavaCache(CacheBuilder<Object, Object> builder, UserAgentStringParser parser) {
    super(parser);
    this.cache = builder.build(new GuavaCacheLoader(parser));
}

From source file:com.google.javascript.jscomp.transpile.CachingTranspiler.java

public CachingTranspiler(final Transpiler delegate, CacheBuilder<Object, ? super TranspileResult> builder) {
    checkNotNull(delegate);/*from   w w  w.j a v a2s . c om*/
    this.cache = builder.build(new CacheLoader<Key, TranspileResult>() {
        @Override
        public TranspileResult load(Key key) {
            return delegate.transpile(key.path, key.code);
        }
    });
    this.runtime = Suppliers.memoize(delegate::runtime);
}

From source file:com.hortonworks.streamline.storage.cache.impl.GuavaCache.java

public GuavaCache(final StorageManager dao, CacheBuilder guavaCacheBuilder) {
    this.dao = dao;
    this.guavaCache = guavaCacheBuilder.build(new CacheLoader<StorableKey, Storable>() {
        @Override/*from  w w w .  j  a  va2 s .  c om*/
        public Storable load(StorableKey key) throws StorageException, NonexistentStorableKeyException {
            Storable val = dao.get(key);
            if (val != null) {
                return val;
            }
            throw new NonexistentStorableKeyException("Nonexistent key : [" + key + "]");
        }
    });
}