Example usage for com.google.common.cache LoadingCache get

List of usage examples for com.google.common.cache LoadingCache get

Introduction

In this page you can find the example usage for com.google.common.cache LoadingCache get.

Prototype

V get(K key) throws ExecutionException;

Source Link

Document

Returns the value associated with key in this cache, first loading that value if necessary.

Usage

From source file:bai4.Bai4.java

/**
 * @param args the command line arguments
 *///w ww .  ja  va  2  s . c  om
public static void main(String[] args) {
    // TODO code application logic here
    LoadingCache<Integer, Integer> cache = CacheBuilder.newBuilder().maximumSize(100)
            .expireAfterAccess(10, TimeUnit.SECONDS).build(new CacheLoader<Integer, Integer>() {
                @Override
                public Integer load(Integer k) throws Exception {
                    return calculate(k);
                }
            });
    get("/factorial", (req, res) -> {
        ///System.out.println("Hello World");
        int n = Integer.parseInt(req.queryParams("n"));
        return cache.get(n);
    });
}

From source file:org.jboss.weld.util.cache.LoadingCacheUtils.java

/**
 * Get the cache value for the given key. Wrap possible {@link ExecutionException} and {@link UncheckedExecutionException}.
 *
 * @param cache/*w ww  . j  a  v  a2 s .c om*/
 * @param key
 * @param wrapExecutionProblem If <code>true</code>, wrap possible {@link ExecutionException} and
 *        {@link UncheckedExecutionException}, otherwise {@link UncheckedExecutionException} may be thrown when execution
 *        problem occurs
 * @param <K> Key type
 * @param <V> Value type
 * @return the cache value
 * @throws ExecutionError if an error is thrown while loading the value
 */
public static <K, V> V getCacheValue(LoadingCache<K, V> cache, K key) {
    try {
        return cache.get(key);
    } catch (ExecutionException e) {
        throw UtilLogger.LOG.unableToLoadCacheValue(key, e.getCause());
    } catch (UncheckedExecutionException e) {
        Throwable cause = e.getCause();
        if (cause instanceof RuntimeException) {
            throw (RuntimeException) cause;
        }
        throw UtilLogger.LOG.unableToLoadCacheValue(key, cause);
    }
}

From source file:ec.tstoolkit.utilities.GuavaCaches.java

@Nonnull
public static <K, V> V getOrThrowIOException(@Nonnull LoadingCache<K, V> cache, @Nonnull K key)
        throws IOException {
    try {// w  w w .j  a  v a2 s .  c o  m
        return cache.get(key);
    } catch (ExecutionException ex) {
        throw unboxToIOException(ex);
    } catch (UncheckedExecutionException ex) {
        throw new RuntimeException(ex.getCause());
    }
}

From source file:google.registry.model.registry.label.BaseDomainLabelList.java

protected static <R> Optional<R> getFromCache(String listName, LoadingCache<String, R> cache) {
    try {//from  www  . j  a v  a2 s . com
        return Optional.of(cache.get(listName));
    } catch (InvalidCacheLoadException e) {
        return Optional.absent();
    } catch (ExecutionException e) {
        throw new UncheckedExecutionException("Could not retrieve list named " + listName, e);
    }
}

From source file:org.gradle.api.internal.plugins.DefaultPluginRegistry.java

private static <K, V> V uncheckedGet(LoadingCache<K, V> cache, K key) {
    try {//from ww  w.  jav  a  2s .  co  m
        return cache.get(key);
    } catch (ExecutionException e) {
        throw UncheckedException.throwAsUncheckedException(e.getCause());
    } catch (UncheckedExecutionException e) {
        throw UncheckedException.throwAsUncheckedException(e.getCause());
    }
}

From source file:com.nearinfinity.honeycomb.hbase.MetadataCache.java

private static <K, V> V cacheGet(LoadingCache<K, V> cache, K key) {
    try {/*from ww  w.ja  v  a 2  s . c  om*/
        return cache.get(key);
    } catch (Exception e) {
        logger.error("Encountered unexpected exception during cache get:", e.getCause());
        throw (RuntimeException) e.getCause();
    }
}

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

/** modifies a function to cache its results
 * //from   ww  w . j  av a2s .  c  o m
 * @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.jclouds.reflect.Reflection2.java

/**
 * ensures that exceptions are not doubly-wrapped
 *///w ww  .j av  a  2s  .co m
private static <K, V> V get(LoadingCache<K, V> cache, K key) {
    try {
        return cache.get(key);
    } catch (UncheckedExecutionException e) {
        throw propagate(e.getCause());
    } catch (ExecutionException e) {
        throw propagate(e.getCause());
    }
}

From source file:com.analog.lyric.dimple.factorfunctions.core.JointFactorFunction.java

@Internal
public static JointFactorFunction getFromCache(LoadingCache<Functions, JointFactorFunction> cache,
        Functions key) {/*from w  w w .  j  ava2 s.c  o m*/
    try {
        return cache.get(key);
    } catch (ExecutionException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:com.facebook.presto.cassandra.CachingCassandraSchemaProvider.java

private static <K, V, E extends Exception> V getCacheValue(LoadingCache<K, V> cache, K key,
        Class<E> exceptionClass) throws E {
    try {//from   w  ww  . ja  v a 2  s .c  o m
        return cache.get(key);
    } catch (ExecutionException | UncheckedExecutionException e) {
        Throwable t = e.getCause();
        Throwables.propagateIfInstanceOf(t, exceptionClass);
        throw Throwables.propagate(t);
    }
}