Class ValueCache
- java.lang.Object
-
- com.inductiveautomation.perspective.gateway.binding.ValueCache
-
public class ValueCache extends java.lang.Object
A cache of value shared across all sessions. Used to store "expensive" values, which are usually CPU or I/O intensive results of a polling binding or model delegate's polling task. To use this cache, you must be able to wrap up everything that fetching/calculating your result requires into a single object suitable for use as a key in a map (i.e. equals/hashcode implemented). This is called the "params" object (P). Then you ask the cache for your value, giving it P, a function that will fetch/calculate your value given P, and your acceptable max age to re-use an existing object in the cache.
The cache will return a CompletableFuture for your value. If the value was already present, this future may already be completed. If another caller has already asked for this value and is calculating it, you may receive the future they also have, thus piggy-backing on the ongoing calculation. If no suitable value or ongoing fetch attempt is in the cache, then your fetch function will be invoked to create a new value.
There are three important rules for using this cache: 1. Your params object
P
must be immutable, and suitable as a map key, and contain everything needed for fetching. 2. Your fetchf(P): V
function must be a pure function of P. It will be stored, and must not leak references to other objects outside of P. It is recommended to use static functions for f(P) to ensure this rule is satisfied.Notes. It is not expected that subsequent invocations of f(P) return the same values. Values will be removed from the cache after one minute if not used again. This can be changed with system property
MAX_AGE_SYSPROP_KEY
, specified in milliseconds.
-
-
Field Summary
Fields Modifier and Type Field Description static long
MAX_AGE
static java.lang.String
MAX_AGE_SYSPROP_KEY
-
Constructor Summary
Constructors Constructor Description ValueCache(java.util.concurrent.ScheduledExecutorService scheduler, java.util.concurrent.ExecutorService executor)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <P,V>
java.util.concurrent.CompletableFuture<V>getValue(P params, java.util.function.Function<P,V> fetchFunction, long maxAge, java.util.concurrent.TimeUnit timeUnit)
<P> void
invalidateValue(P params)
-