Class PropCache<TKey, TValue>
Caches the result of a computation, and invalidates based on ComputeContext invalidation rules. This class allows you to cache the result of a function from TKey to TValue, where the function observes values using a ComputeContext. When the ComputeContext is invalidated, the cache entry will be cleared, and any downstream observers will be invalidated as well.
Note that this cache currently invalidates values only when the ComputeContext is invalidated; in particular, if TKey is a unity object which is destroyed, this in itself will not result in the associated value being freed from memory.
This class is not thread-safe; all calls must be made from the Unity main thread. (This may change in the future)
public sealed class PropCache<TKey, TValue>
Type Parameters
TKeyTValue
- Inheritance
-
PropCache<TKey, TValue>
- Inherited Members
Constructors
PropCache(string, Func<ComputeContext, TKey, TValue>, Func<TValue, TValue, bool>?)
Creates a new propcache
public PropCache(string debugName, Func<ComputeContext, TKey, TValue> operatorFunc, Func<TValue, TValue, bool>? equalityComparer = null)
Parameters
debugNamestringThe debug name, which is displayed in the preview trace and other debug contexts
operatorFuncFunc<ComputeContext, TKey, TValue>The function to cache the result of
equalityComparerFunc<TValue, TValue, bool>If not null, a function used to determine if the cached function changed its result. If this function returns true, then downstream consumers will not be invalidated. Note that if the equality comparator is present, the function may be re-evaluated multiple times per cache invalidation.
Methods
Get(ComputeContext, TKey)
Fetches a value from the cache, computing it if necessary.
public TValue Get(ComputeContext context, TKey key)
Parameters
contextComputeContextThe compute context to use to observe the value
keyTKeyThe key to look up
Returns
- TValue
The computed value
InvalidateAll()
public void InvalidateAll()