java.lang.Object
com.inductiveautomation.perspective.gateway.binding.ValueCache

public class ValueCache extends 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 fetch f(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 Details

    • MAX_AGE_SYSPROP_KEY

      public static String MAX_AGE_SYSPROP_KEY
    • MAX_AGE

      public static final long MAX_AGE
  • Constructor Details

  • Method Details

    • getValue

      public <P, V> CompletableFuture<V> getValue(P params, Function<P,V> fetchFunction, long maxAge, TimeUnit timeUnit)
    • invalidateValue

      public <P> void invalidateValue(P params)