Class Memoizer<I,O>
- Type Parameters:
I- The type of the input to the calculationO- The type of the output of the calculation
- All Implemented Interfaces:
Computable<I,O>
This is not a fully functional cache: it is unbounded, and there is no way of limiting or removing results once they
have been generated. In particular, note the exception-caching default: unless the recalculate constructor
option is set to true, the first exception thrown by a calculation for a given parameter is cached
and rethrown for every future call with that parameter for the lifetime of this instance - a single transient
failure permanently poisons that key. Set recalculate to true to retry failed calculations on
subsequent calls instead.
Thanks go to Brian Goetz, Tim Peierls and the members of JCP JSR-166 Expert Group for coming up with the original implementation of the class. It was also published within Java Concurrency in Practice as a sample.
- Since:
- 3.6
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs a Memoizer for the provided Function calculation.Constructs a Memoizer for the provided Function calculation, with the option of whether a Function that experiences an error should recalculate on subsequent calls or return the same cached exception.Memoizer(Computable<I, O> computable) Constructs a Memoizer for the provided Computable calculation.Memoizer(Computable<I, O> computable, boolean recalculate) Constructs a Memoizer for the provided Computable calculation, with the option of whether a Computation that experiences an error should recalculate on subsequent calls or return the same cached exception. -
Method Summary
-
Constructor Details
-
Memoizer
Constructs a Memoizer for the provided Computable calculation.If a calculation throws an exception for any reason, this exception will be cached and returned for all future calls with the provided parameter.
- Parameters:
computable- The computation whose results should be memorized
-
Memoizer
Constructs a Memoizer for the provided Computable calculation, with the option of whether a Computation that experiences an error should recalculate on subsequent calls or return the same cached exception.- Parameters:
computable- The computation whose results should be memorizedrecalculate- determines whether the computation should be recalculated on subsequent calls if the previous call failed
-
Memoizer
Constructs a Memoizer for the provided Function calculation.If a calculation throws an exception for any reason, this exception will be cached and returned for all future calls with the provided parameter.
- Parameters:
function- The function whose results should be memorized- Since:
- 2.13.0
-
Memoizer
Constructs a Memoizer for the provided Function calculation, with the option of whether a Function that experiences an error should recalculate on subsequent calls or return the same cached exception.- Parameters:
function- The computation whose results should be memorizedrecalculate- determines whether the computation should be recalculated on subsequent calls if the previous call failed- Since:
- 2.13.0
-
-
Method Details
-
compute
This method will return the result of the calculation and cache it, if it has not previously been calculated.This cache will also cache exceptions that occur during the computation if the
recalculateparameter in the constructor was set tofalse, or not set: the first exception thrown for a given argument is rethrown for every future call with that argument. Otherwise, if an exception happened on the previous calculation, the method will attempt again to generate a value.The calculation for a given argument runs at most once per cached entry and executes outside any internal lock of the backing map (the pattern published in Java Concurrency in Practice): a slow calculation for one key does not block calls for unrelated keys, and a calculation may itself use this Memoizer without deadlocking. Concurrent callers for the same argument wait on the same
Future.- Specified by:
computein interfaceComputable<I,O> - Parameters:
arg- The argument for the calculation- Returns:
- The result of the calculation
- Throws:
InterruptedException- Thrown if the calculation is interrupted.
-