Search in sources :

Example 1 with Expirable

use of com.github.benmanes.caffeine.jcache.Expirable in project caffeine by ben-manes.

the class JCacheLoaderAdapter method load.

@Override
public Expirable<V> load(K key) {
    try {
        boolean statsEnabled = statistics.isEnabled();
        long start = statsEnabled ? ticker.read() : 0L;
        V value = delegate.load(key);
        if (value == null) {
            return null;
        }
        dispatcher.publishCreated(cache, key, value);
        if (statsEnabled) {
            // Subtracts the load time from the get time
            statistics.recordGetTime(start - ticker.read());
        }
        return new Expirable<>(value, expireTimeMS());
    } catch (CacheLoaderException e) {
        throw e;
    } catch (RuntimeException e) {
        throw new CacheLoaderException(e);
    }
}
Also used : CacheLoaderException(javax.cache.integration.CacheLoaderException) Expirable(com.github.benmanes.caffeine.jcache.Expirable)

Example 2 with Expirable

use of com.github.benmanes.caffeine.jcache.Expirable in project caffeine by ben-manes.

the class JCacheLoaderAdapter method loadAll.

@Override
public Map<K, Expirable<V>> loadAll(Iterable<? extends K> keys) {
    try {
        boolean statsEnabled = statistics.isEnabled();
        long start = statsEnabled ? ticker.read() : 0L;
        Map<K, Expirable<V>> result = delegate.loadAll(keys).entrySet().stream().filter(entry -> (entry.getKey() != null) && (entry.getValue() != null)).collect(Collectors.toMap(Map.Entry::getKey, entry -> new Expirable<>(entry.getValue(), expireTimeMS())));
        for (Map.Entry<K, Expirable<V>> entry : result.entrySet()) {
            dispatcher.publishCreated(cache, entry.getKey(), entry.getValue().get());
        }
        if (statsEnabled) {
            // Subtracts the load time from the get time
            statistics.recordGetTime(start - ticker.read());
        }
        return result;
    } catch (CacheLoaderException e) {
        throw e;
    } catch (RuntimeException e) {
        throw new CacheLoaderException(e);
    }
}
Also used : Ticker(com.github.benmanes.caffeine.cache.Ticker) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) Collectors(java.util.stream.Collectors) CacheLoader(javax.cache.integration.CacheLoader) CacheLoaderException(javax.cache.integration.CacheLoaderException) TimeUnit(java.util.concurrent.TimeUnit) CacheProxy(com.github.benmanes.caffeine.jcache.CacheProxy) Objects.requireNonNull(java.util.Objects.requireNonNull) Map(java.util.Map) Expirable(com.github.benmanes.caffeine.jcache.Expirable) JCacheStatisticsMXBean(com.github.benmanes.caffeine.jcache.management.JCacheStatisticsMXBean) Duration(javax.cache.expiry.Duration) EventDispatcher(com.github.benmanes.caffeine.jcache.event.EventDispatcher) CacheLoaderException(javax.cache.integration.CacheLoaderException) Expirable(com.github.benmanes.caffeine.jcache.Expirable) Map(java.util.Map)

Aggregations

Expirable (com.github.benmanes.caffeine.jcache.Expirable)2 CacheLoaderException (javax.cache.integration.CacheLoaderException)2 Ticker (com.github.benmanes.caffeine.cache.Ticker)1 CacheProxy (com.github.benmanes.caffeine.jcache.CacheProxy)1 EventDispatcher (com.github.benmanes.caffeine.jcache.event.EventDispatcher)1 JCacheStatisticsMXBean (com.github.benmanes.caffeine.jcache.management.JCacheStatisticsMXBean)1 Map (java.util.Map)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 TimeUnit (java.util.concurrent.TimeUnit)1 Collectors (java.util.stream.Collectors)1 Duration (javax.cache.expiry.Duration)1 ExpiryPolicy (javax.cache.expiry.ExpiryPolicy)1 CacheLoader (javax.cache.integration.CacheLoader)1