@Test public void asyncUpdate() throws InterruptedException { Function1<Integer, Integer> fn = Memoize.memoizeFunctionAsync(Lambda.<Integer, Integer>λ(i -> i + x), Executors.newScheduledThreadPool(1), 1); assertThat(fn.apply(1),equalTo(1)); assertThat(fn.apply(1),equalTo(1)); x=x+1; Thread.sleep(2); assertThat(fn.apply(1),equalTo(2)); }
public static <T, R> Function1<T, R> memoizeFunctionAsync(final Function<T, R> fn, ScheduledExecutorService ex, long updateRateInMillis){ final Map<T, R> lazy = new ConcurrentHashMap<>(); ReactiveSeq.generate(()->{ lazy.forEach((k,v)->{ lazy.put(k,fn.apply(k)); }); return null; }).scheduleFixedRate(updateRateInMillis,ex); return t -> lazy.computeIfAbsent(t, fn); }
Memoize { public static <T, R> Function1<T, R> memoizeFunctionAsync(final Function<T, R> fn, ScheduledExecutorService ex, long updateRateInMillis){ final Map<T, R> lazy = new ConcurrentHashMap<>(); ReactiveSeq.generate(()->{ lazy.forEach((k,v)->{ lazy.put(k,fn.apply(k)); }); return null; }).scheduleFixedRate(updateRateInMillis,ex); return t -> lazy.computeIfAbsent(t, fn); } }
Memoize { public static <T, R> Function1<T, R> memoizeFunctionAsync(final Function<T, R> fn, ScheduledExecutorService ex, long updateRateInMillis){ final Map<T, R> lazy = new ConcurrentHashMap<>(); ReactiveSeq.generate(()->{ lazy.forEach((k,v)->{ lazy.put(k,fn.apply(k)); }); return null; }).scheduleFixedRate(updateRateInMillis,ex); return t -> lazy.computeIfAbsent(t, fn); }  }
Memoize { public static <T, R> Function1<T, R> memoizeFunctionAsync(final Function<T, R> fn, ScheduledExecutorService ex, long updateRateInMillis){ final Map<T, R> lazy = new ConcurrentHashMap<>(); ReactiveSeq.generate(()->{ lazy.forEach((k,v)->{ lazy.put(k,fn.apply(k)); }); return null; }).scheduleFixedRate(updateRateInMillis,ex); return t -> lazy.computeIfAbsent(t, fn); }  static Function0<T> memoizeSupplier(final Supplier<T> s); static Function0<T> memoizeSupplier(final Supplier<T> s, final Cacheable<T> cache); static Function0<R> memoizeSupplierAsync(final Supplier<R> fn, ScheduledExecutorService ex, long updateRateInMillis); static Function0<R> memoizeSupplierAsync(final Supplier<R> fn, ScheduledExecutorService ex, String cron); static Callable<T> memoizeCallable(final Callable<T> s, final Cacheable<T> cache); static Callable<T> memoizeCallable(final Callable<T> s); static Runnable memoizeRunnable(final Runnable r); static Function1<T, R> memoizeFunction(final Function<T, R> fn); static Function1<T, R> memoizeFunctionAsync(final Function<T, R> fn, ScheduledExecutorService ex, long updateRateInMillis); static Function1<T, R> memoizeFunctionAsync(final Function<T, R> fn, ScheduledExecutorService ex, String cron); static Function1<T, R> memoizeFunction(final Function<T, R> fn, final Cacheable<R> cache); static Function2<T1, T2, R> memoizeBiFunctionAsync(final BiFunction<T1, T2, R> fn, ScheduledExecutorService ex, long updateRateInMillis); static Function2<T1, T2, R> memoizeBiFunctionAsync(final BiFunction<T1, T2, R> fn, ScheduledExecutorService ex, String cron); static Function2<T1, T2, R> memoizeBiFunction(final BiFunction<T1, T2, R> fn); static Function2<T1, T2, R> memoizeBiFunction(final BiFunction<T1, T2, R> fn, final Cacheable<R> cache); static Function3<T1, T2, T3, R> memoizeTriFunction(final Function3<T1, T2, T3, R> fn); static Function3<T1, T2, T3, R> memoizeTriFunctionAsync(final Function3<T1, T2, T3, R> fn, ScheduledExecutorService ex, String cron); static Function3<T1, T2, T3, R> memoizeTriFunction(final Function3<T1, T2, T3, R> fn, final Cacheable<R> cache); static Function3<T1, T2, T3, R> memoizeTriFunctionAsync(final Function3<T1, T2, T3, R> fn, ScheduledExecutorService ex, long updateRateInMillis); static Function4<T1, T2, T3, T4, R> memoizeQuadFunction(final Function4<T1, T2, T3, T4, R> fn); static Function4<T1, T2, T3, T4, R> memoizeQuadFunctionAsync(final Function4<T1, T2, T3, T4, R> fn, ScheduledExecutorService ex, String cron); static Function4<T1, T2, T3, T4, R> memoizeQuadFunction(final Function4<T1, T2, T3, T4, R> fn,
                                                                                       final Cacheable<R> cache); static Function4<T1, T2, T3, T4, R> memoizeQuadFunctionAsync(final Function4<T1, T2, T3, T4, R> fn, ScheduledExecutorService ex, long updateRateInMillis); static Predicate<T> memoizePredicate(final Predicate<T> p); static Predicate<T> memoizePredicate(final Predicate<T> p, final Cacheable<Boolean> cache); }
Memoize { public static <T, R> Function1<T, R> memoizeFunctionAsync(final Function<T, R> fn, ScheduledExecutorService ex, long updateRateInMillis){ final Map<T, R> lazy = new ConcurrentHashMap<>(); ReactiveSeq.generate(()->{ lazy.forEach((k,v)->{ lazy.put(k,fn.apply(k)); }); return null; }).scheduleFixedRate(updateRateInMillis,ex); return t -> lazy.computeIfAbsent(t, fn); }  static Function0<T> memoizeSupplier(final Supplier<T> s); static Function0<T> memoizeSupplier(final Supplier<T> s, final Cacheable<T> cache); static Function0<R> memoizeSupplierAsync(final Supplier<R> fn, ScheduledExecutorService ex, long updateRateInMillis); static Function0<R> memoizeSupplierAsync(final Supplier<R> fn, ScheduledExecutorService ex, String cron); static Callable<T> memoizeCallable(final Callable<T> s, final Cacheable<T> cache); static Callable<T> memoizeCallable(final Callable<T> s); static Runnable memoizeRunnable(final Runnable r); static Function1<T, R> memoizeFunction(final Function<T, R> fn); static Function1<T, R> memoizeFunctionAsync(final Function<T, R> fn, ScheduledExecutorService ex, long updateRateInMillis); static Function1<T, R> memoizeFunctionAsync(final Function<T, R> fn, ScheduledExecutorService ex, String cron); static Function1<T, R> memoizeFunction(final Function<T, R> fn, final Cacheable<R> cache); static Function2<T1, T2, R> memoizeBiFunctionAsync(final BiFunction<T1, T2, R> fn, ScheduledExecutorService ex, long updateRateInMillis); static Function2<T1, T2, R> memoizeBiFunctionAsync(final BiFunction<T1, T2, R> fn, ScheduledExecutorService ex, String cron); static Function2<T1, T2, R> memoizeBiFunction(final BiFunction<T1, T2, R> fn); static Function2<T1, T2, R> memoizeBiFunction(final BiFunction<T1, T2, R> fn, final Cacheable<R> cache); static Function3<T1, T2, T3, R> memoizeTriFunction(final Function3<T1, T2, T3, R> fn); static Function3<T1, T2, T3, R> memoizeTriFunctionAsync(final Function3<T1, T2, T3, R> fn, ScheduledExecutorService ex, String cron); static Function3<T1, T2, T3, R> memoizeTriFunction(final Function3<T1, T2, T3, R> fn, final Cacheable<R> cache); static Function3<T1, T2, T3, R> memoizeTriFunctionAsync(final Function3<T1, T2, T3, R> fn, ScheduledExecutorService ex, long updateRateInMillis); static Function4<T1, T2, T3, T4, R> memoizeQuadFunction(final Function4<T1, T2, T3, T4, R> fn); static Function4<T1, T2, T3, T4, R> memoizeQuadFunctionAsync(final Function4<T1, T2, T3, T4, R> fn, ScheduledExecutorService ex, String cron); static Function4<T1, T2, T3, T4, R> memoizeQuadFunction(final Function4<T1, T2, T3, T4, R> fn,
                                                                                       final Cacheable<R> cache); static Function4<T1, T2, T3, T4, R> memoizeQuadFunctionAsync(final Function4<T1, T2, T3, T4, R> fn, ScheduledExecutorService ex, long updateRateInMillis); static Predicate<T> memoizePredicate(final Predicate<T> p); static Predicate<T> memoizePredicate(final Predicate<T> p, final Cacheable<Boolean> cache);  }