Search in sources :

Example 1 with Collectors

use of java.util.stream.Collectors in project caffeine by ben-manes.

the class LoadingCacheProxy method getAll.

/** Returns the entries, loading if necessary, and optionally updates their access expiry time. */
@SuppressWarnings("PMD.AvoidCatchingNPE")
private Map<K, V> getAll(Set<? extends K> keys, boolean updateAccessTime) {
    requireNotClosed();
    boolean statsEnabled = statistics.isEnabled();
    long start = statsEnabled ? ticker.read() : 0L;
    try {
        Map<K, Expirable<V>> entries = getAndFilterExpiredEntries(keys, updateAccessTime);
        if (entries.size() != keys.size()) {
            List<K> keysToLoad = keys.stream().filter(key -> !entries.containsKey(key)).collect(Collectors.<K>toList());
            entries.putAll(cache.getAll(keysToLoad));
        }
        Map<K, V> result = copyMap(entries);
        if (statsEnabled) {
            statistics.recordGetTime(ticker.read() - start);
        }
        return result;
    } catch (NullPointerException | IllegalStateException | ClassCastException | CacheException e) {
        throw e;
    } catch (RuntimeException e) {
        throw new CacheException(e);
    } finally {
        dispatcher.awaitSynchronous();
    }
}
Also used : Ticker(com.github.benmanes.caffeine.cache.Ticker) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) Executor(java.util.concurrent.Executor) LoadingCache(com.github.benmanes.caffeine.cache.LoadingCache) Set(java.util.Set) CompletionListener(javax.cache.integration.CompletionListener) Collectors(java.util.stream.Collectors) CacheLoader(javax.cache.integration.CacheLoader) Objects(java.util.Objects) List(java.util.List) Map(java.util.Map) Cache(javax.cache.Cache) Optional(java.util.Optional) JCacheStatisticsMXBean(com.github.benmanes.caffeine.jcache.management.JCacheStatisticsMXBean) CacheException(javax.cache.CacheException) CacheManager(javax.cache.CacheManager) EventDispatcher(com.github.benmanes.caffeine.jcache.event.EventDispatcher) CaffeineConfiguration(com.github.benmanes.caffeine.jcache.configuration.CaffeineConfiguration) CacheException(javax.cache.CacheException)

Aggregations

LoadingCache (com.github.benmanes.caffeine.cache.LoadingCache)1 Ticker (com.github.benmanes.caffeine.cache.Ticker)1 CaffeineConfiguration (com.github.benmanes.caffeine.jcache.configuration.CaffeineConfiguration)1 EventDispatcher (com.github.benmanes.caffeine.jcache.event.EventDispatcher)1 JCacheStatisticsMXBean (com.github.benmanes.caffeine.jcache.management.JCacheStatisticsMXBean)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Executor (java.util.concurrent.Executor)1 Collectors (java.util.stream.Collectors)1 Cache (javax.cache.Cache)1 CacheException (javax.cache.CacheException)1 CacheManager (javax.cache.CacheManager)1 ExpiryPolicy (javax.cache.expiry.ExpiryPolicy)1 CacheLoader (javax.cache.integration.CacheLoader)1 CompletionListener (javax.cache.integration.CompletionListener)1