@Override @SuppressWarnings("PMD.PreserveStackTrace") public V get(K key, Function<? super K, ? extends V> mappingFunction) { requireNonNull(mappingFunction); CompletableFuture<V> future = asyncCache().get(key, (k, executor) -> CompletableFuture.supplyAsync(() -> mappingFunction.apply(key), executor)); try { return future.get(); } catch (ExecutionException e) { if (e.getCause() instanceof RuntimeException) { throw (RuntimeException) e.getCause(); } else if (e.getCause() instanceof Error) { throw (Error) e.getCause(); } throw new CompletionException(e.getCause()); } catch (InterruptedException e) { throw new CompletionException(e); } }
@Override public void put(K key, V value) { requireNonNull(value); asyncCache().cache().put(key, CompletableFuture.completedFuture(value)); }
@Override public Map<K, V> getAllPresent(Iterable<?> keys) { Set<Object> uniqueKeys = new LinkedHashSet<>(); for (Object key : keys) { uniqueKeys.add(key); } int misses = 0; Map<Object, Object> result = new LinkedHashMap<>(); for (Object key : uniqueKeys) { CompletableFuture<V> future = asyncCache().cache().get(key); Object value = Async.getIfReady(future); if (value == null) { misses++; } else { result.put(key, value); } } asyncCache().cache().statsCounter().recordMisses(misses); asyncCache().cache().statsCounter().recordHits(result.size()); @SuppressWarnings("unchecked") Map<K, V> castedResult = (Map<K, V>) result; return Collections.unmodifiableMap(castedResult); }
@Override public long estimatedSize() { return asyncCache().cache().size(); }
@Override public void invalidate(Object key) { asyncCache().cache().remove(key); }
@Override public Policy<K, V> policy() { return asyncCache().policy(); }
@Override public void cleanUp() { asyncCache().cache().cleanUp(); }
@Override public void invalidateAll(Iterable<?> keys) { asyncCache().cache().invalidateAll(keys); }
@Override public void invalidateAll() { asyncCache().cache().clear(); }
@Override public CacheStats stats() { return asyncCache().cache().statsCounter().snapshot(); }
@Override public ConcurrentMap<K, V> asMap() { return (asMapView == null) ? (asMapView = new AsMapView<>(asyncCache().cache())) : asMapView; } }
@Override public @Nullable V getIfPresent(Object key) { CompletableFuture<V> future = asyncCache().cache().getIfPresent(key, /* recordStats */ true); return Async.getIfReady(future); }