Class CancellableSingleObjectCache<Input,Key,Value>
- Type Parameters:
Input
- The type of the input to the computation of the cached value.Key
- The key type. The cached value is associated with a key, and subsequentget(Input, java.util.function.BooleanSupplier, org.elasticsearch.action.ActionListener<Value>)
calls compare keys of the given input value to determine whether the cached value is fresh or not. SeeisFresh(Key, Key)
.Value
- The type of the cached value.
This is useful for things like computing stats over cluster metadata: the first time stats are requested they are computed, but subsequent calls re-use the computed value as long as they pertain to the same metadata version. If stats are requested for a different metadata version then the cached value is dropped and a new one is computed.
Retrievals happen via the async get(Input, java.util.function.BooleanSupplier, org.elasticsearch.action.ActionListener<Value>)
method. If a retrieval is cancelled (e.g. the channel on which to return the stats is
closed) then the computation carries on running in case another retrieval for the same key arrives in future. However if all of the
retrievals for a key are cancelled and a retrieval occurs for a fresher key then the computation itself is cancelled.
Cancellation is based on polling: the refresh(Input, java.lang.Runnable, org.elasticsearch.action.ActionListener<Value>)
method checks whether it should abort whenever it is convenient to do so, which in
turn checks all the pending retrievals to see whether they have been cancelled.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
get(Input input, BooleanSupplier isCancelled, ActionListener<Value> listener)
Start a retrieval for the value associated with the giveninput
, and pass it to the givenlistener
.protected abstract Key
Compute the key for the given input value.protected boolean
Compute whether thecurrentKey
is fresh enough for a retrieval associated withnewKey
.protected abstract void
refresh(Input input, Runnable ensureNotCancelled, ActionListener<Value> listener)
Compute a new value for the cache.
-
Constructor Details
-
CancellableSingleObjectCache
public CancellableSingleObjectCache()
-
-
Method Details
-
refresh
protected abstract void refresh(Input input, Runnable ensureNotCancelled, ActionListener<Value> listener)Compute a new value for the cache.If an exception is thrown, or passed to the
listener
, then it is passed on to all waiting listeners but it is not cached so that subsequent retrievals will trigger subsequent calls to this method.Implementations of this method should poll for cancellation by running
ensureNotCancelled
whenever appropriate. The computation is cancelled if all of the corresponding retrievals have been cancelled and a retrieval has since happened for a fresher key.- Parameters:
input
- The input to this computation, which will be converted to a key and used to determine whether it is suitably fresh for future requests too.ensureNotCancelled
- ARunnable
which throws aTaskCancelledException
if the result of the computation is no longer needed. On cancellation, notifying thelistener
is optional.listener
- AActionListener
which should be notified when the computation completes. If the computation fails by callingActionListener.onFailure(java.lang.Exception)
then the result is returned to the pending listeners but is not cached.
-
getKey
Compute the key for the given input value. -
isFresh
Compute whether thecurrentKey
is fresh enough for a retrieval associated withnewKey
.- Parameters:
currentKey
- The key of the current (cached or pending) value.newKey
- The key associated with a new retrival.- Returns:
true
if a value computed forcurrentKey
is fresh enough to satisfy a retrieval fornewKey
.
-
get
Start a retrieval for the value associated with the giveninput
, and pass it to the givenlistener
.If a fresh-enough result is available when this method is called then the
listener
is notified immediately, on this thread. If a fresh-enough result is already being computed then thelistener
is captured and will be notified when the result becomes available, on the thread on which the refresh completes. If no fresh-enough result is either pending or available then this method starts to compute one by callingrefresh(Input, java.lang.Runnable, org.elasticsearch.action.ActionListener<Value>)
on this thread.- Parameters:
input
- The input to compute the desired value, converted to aCancellableSingleObjectCache
to determine if the value that's currently cached or pending is fresh enough.isCancelled
- Returnstrue
if the listener no longer requires the value being computed.listener
- The listener to notify when the desired value becomes available.
-