UnboundedLocalLoadingCache(Caffeine<K, V> builder, CacheLoader<? super K, V> loader) { super(builder); this.loader = loader; this.hasBulkLoader = hasLoadAll(loader); this.mappingFunction = key -> { try { return loader.load(key); } catch (RuntimeException e) { throw e; } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new CompletionException(e); } catch (Exception e) { throw new CompletionException(e); } }; }
BoundedLocalLoadingCache(Caffeine<K, V> builder, CacheLoader<? super K, V> loader) { super(builder, loader); requireNonNull(loader); hasBulkLoader = hasLoadAll(loader); mappingFunction = key -> { try { return loader.load(key); } catch (RuntimeException e) { throw e; } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new CompletionException(e); } catch (Exception e) { throw new CompletionException(e); } }; }
/** * Asynchronously computes or retrieves the value corresponding to {@code key}. * * @param key the non-null key whose value should be loaded * @param executor the executor that asynchronously loads the entry * @return the future value associated with {@code key} */ @Override @NonNull default CompletableFuture<V> asyncLoad(@NonNull K key, @NonNull Executor executor) { requireNonNull(key); requireNonNull(executor); return CompletableFuture.supplyAsync(() -> { try { return load(key); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new CompletionException(e); } }, executor); }
@Override public V load(K key) throws Exception { V value = delegate.load(key); if (value == null) { throw new CacheMissException(); } return value; } }
/** * Computes or retrieves a replacement value corresponding to an already-cached {@code key}. If * the replacement value is not found then the mapping will be removed if {@code null} is * returned. This method is called when an existing cache entry is refreshed by * {@link Caffeine#refreshAfterWrite}, or through a call to {@link LoadingCache#refresh}. * <p> * <b>Note:</b> <i>all exceptions thrown by this method will be logged and then swallowed</i>. * * @param key the non-null key whose value should be loaded * @param oldValue the non-null old value corresponding to {@code key} * @return the new value associated with {@code key}, or {@code null} if the mapping is to be * removed * @throws Exception or Error, in which case the mapping is unchanged * @throws InterruptedException if this method is interrupted. {@code InterruptedException} is * treated like any other {@code Exception} in all respects except that, when it is * caught, the thread's interrupt status is set */ @Nullable default V reload(@NonNull K key, @NonNull V oldValue) throws Exception { return load(key); }
/** * Computes or retrieves a replacement value corresponding to an already-cached {@code key}. If * the replacement value is not found then the mapping will be removed if {@code null} is * returned. This method is called when an existing cache entry is refreshed by * {@link Caffeine#refreshAfterWrite}, or through a call to {@link LoadingCache#refresh}. * <p> * <b>Note:</b> <i>all exceptions thrown by this method will be logged and then swallowed</i>. * * @param key the non-null key whose value should be loaded * @param oldValue the non-null old value corresponding to {@code key} * @return the new value associated with {@code key}, or {@code null} if the mapping is to be * removed * @throws Exception or Error, in which case the mapping is unchanged * @throws InterruptedException if this method is interrupted. {@code InterruptedException} is * treated like any other {@code Exception} in all respects except that, when it is * caught, the thread's interrupt status is set */ @Nullable default V reload(@Nonnull K key, @Nonnull V oldValue) throws Exception { return load(key); }
/** * Asynchronously computes or retrieves the value corresponding to {@code key}. * * @param key the non-null key whose value should be loaded * @param executor the executor that asynchronously loads the entry * @return the future value associated with {@code key} */ @Override @Nonnull default CompletableFuture<V> asyncLoad(@Nonnull K key, @Nonnull Executor executor) { requireNonNull(key); requireNonNull(executor); return CompletableFuture.supplyAsync(() -> { try { return load(key); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new CompletionException(e); } }, executor); }
BoundedLocalLoadingCache(Caffeine<K, V> builder, CacheLoader<? super K, V> loader) { super(builder, loader); requireNonNull(loader); hasBulkLoader = hasLoadAll(loader); mappingFunction = key -> { try { return loader.load(key); } catch (RuntimeException e) { throw e; } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new CompletionException(e); } catch (Exception e) { throw new CompletionException(e); } }; }
UnboundedLocalLoadingCache(Caffeine<K, V> builder, CacheLoader<? super K, V> loader) { super(builder); this.loader = loader; this.hasBulkLoader = hasLoadAll(loader); this.mappingFunction = key -> { try { return loader.load(key); } catch (RuntimeException e) { throw e; } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new CompletionException(e); } catch (Exception e) { throw new CompletionException(e); } }; }