A memoized function stores the output corresponding to some set of specific inputs. Subsequent calls with remembered inputs return the remembered result rather than recalculating it.
| 34 | * rather than recalculating it. |
| 35 | */ |
| 36 | @ThreadSafe |
| 37 | public final class Memoizer { |
| 38 | |
| 39 | private static final Supplier<Long> DEFAULT_EXPIRATION_NANOS = memoize(Memoizer::queryExpirationConfig, |
| 40 | TimeUnit.MINUTES.toNanos(1)); |
| 41 | |
| 42 | private Memoizer() { |
| 43 | } |
| 44 | |
| 45 | private static long queryExpirationConfig() { |
| 46 | return TimeUnit.MILLISECONDS.toNanos(GlobalConfig.get("oshi.util.memoizer.expiration", 300)); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Default exipiration of memoized values in nanoseconds, which will refresh |
| 51 | * after this time elapses. Update by setting {@link GlobalConfig} property |
| 52 | * <code>oshi.util.memoizer.expiration</code> to a value in milliseconds. |
| 53 | * |
| 54 | * @return The number of nanoseconds to keep memoized values before refreshing |
| 55 | */ |
| 56 | public static long defaultExpiration() { |
| 57 | return DEFAULT_EXPIRATION_NANOS.get(); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Store a supplier in a delegate function to be computed once, and only again |
| 62 | * after time to live (ttl) has expired. |
| 63 | * |
| 64 | * @param <T> |
| 65 | * The type of object supplied |
| 66 | * @param original |
| 67 | * The {@link java.util.function.Supplier} to memoize |
| 68 | * @param ttlNanos |
| 69 | * Time in nanoseconds to retain calculation. If negative, retain |
| 70 | * indefinitely. |
| 71 | * @return A memoized version of the supplier |
| 72 | */ |
| 73 | public static <T> Supplier<T> memoize(Supplier<T> original, long ttlNanos) { |
| 74 | // Adapted from Guava's ExpiringMemoizingSupplier |
| 75 | return new Supplier<T>() { |
| 76 | final Supplier<T> delegate = original; |
| 77 | volatile T value; // NOSONAR squid:S3077 |
| 78 | volatile long expirationNanos; |
| 79 | |
| 80 | @Override |
| 81 | public T get() { |
| 82 | long nanos = expirationNanos; |
| 83 | long now = System.nanoTime(); |
| 84 | if (nanos == 0 || (ttlNanos >= 0 && now - nanos >= 0)) { |
| 85 | synchronized (this) { |
| 86 | if (nanos == expirationNanos) { // recheck for lost race |
| 87 | T t = delegate.get(); |
| 88 | value = t; |
| 89 | nanos = now + ttlNanos; |
| 90 | expirationNanos = (nanos == 0) ? 1 : nanos; |
| 91 | return t; |
| 92 | } |
| 93 | } |