Class MemoizingMRUCache<TParam, TVal>
- Namespace
- Splat
- Assembly
- Splat.dll
This data structure is a representation of a memoizing cache - i.e. a class that will evaluate a function, but keep a cache of recently evaluated parameters.
Since this is a memoizing cache, it is important that this function be a "pure" function in the mathematical sense - that a key *always* maps to a corresponding return value.
Type Parameters
TParam
The type of the parameter to the calculation function.
TVal
The type of the value returned by the calculation function.
- Inheritance
-
MemoizingMRUCache<TParam, TVal>
- Extension Methods
Constructors
MemoizingMRUCache(Func<TParam, object?, TVal>, int)
Initializes a new instance of the MemoizingMRUCache<TParam, TVal> class.
Parameters
calculationFunc
Func<TParam, object, TVal>The function whose results you want to cache, which is provided the key value, and an Tag object that is user-defined.
maxSize
intThe size of the cache to maintain, after which old items will start to be thrown out.
MemoizingMRUCache(Func<TParam, object?, TVal>, int, Action<TVal>)
Initializes a new instance of the MemoizingMRUCache<TParam, TVal> class.
public MemoizingMRUCache(Func<TParam, object?, TVal> calculationFunc, int maxSize, Action<TVal> onRelease)
Parameters
calculationFunc
Func<TParam, object, TVal>The function whose results you want to cache, which is provided the key value, and an Tag object that is user-defined.
maxSize
intThe size of the cache to maintain, after which old items will start to be thrown out.
onRelease
Action<TVal>A function to call when a result gets evicted from the cache (i.e. because Invalidate was called or the cache is full).
MemoizingMRUCache(Func<TParam, object?, TVal>, int, Action<TVal>?, IEqualityComparer<TParam>)
Initializes a new instance of the MemoizingMRUCache<TParam, TVal> class.
public MemoizingMRUCache(Func<TParam, object?, TVal> calculationFunc, int maxSize, Action<TVal>? onRelease, IEqualityComparer<TParam> paramComparer)
Parameters
calculationFunc
Func<TParam, object, TVal>The function whose results you want to cache, which is provided the key value, and an Tag object that is user-defined.
maxSize
intThe size of the cache to maintain, after which old items will start to be thrown out.
onRelease
Action<TVal>A function to call when a result gets evicted from the cache (i.e. because Invalidate was called or the cache is full).
paramComparer
IEqualityComparer <TParam>A comparer for the parameter.
MemoizingMRUCache(Func<TParam, object?, TVal>, int, IEqualityComparer<TParam>)
Initializes a new instance of the MemoizingMRUCache<TParam, TVal> class.
public MemoizingMRUCache(Func<TParam, object?, TVal> calculationFunc, int maxSize, IEqualityComparer<TParam> paramComparer)
Parameters
calculationFunc
Func<TParam, object, TVal>The function whose results you want to cache, which is provided the key value, and an Tag object that is user-defined.
maxSize
intThe size of the cache to maintain, after which old items will start to be thrown out.
paramComparer
IEqualityComparer <TParam>A comparer for the parameter.
Methods
CachedValues()
Returns all values currently in the cache.
Returns
- IEnumerable<TVal>
The values in the cache.
Get(TParam)
Gets the value from the specified key.
Parameters
key
TParamThe value to pass to the calculation function.
Returns
- TVal
The value that we have got.
Get(TParam, object?)
Evaluates the function provided, returning the cached value if possible.
Parameters
key
TParamThe value to pass to the calculation function.
context
objectAn additional optional user-specific parameter.
Returns
- TVal
The value that we have got.
Invalidate(TParam)
Ensure that the next time this key is queried, the calculation function will be called.
Parameters
key
TParamThe key to invalidate the value for.
InvalidateAll(bool)
Invalidate all the items in the cache.
Parameters
aggregateReleaseExceptions
boolFlag to indicate whether Exceptions during the resource Release call should not fail on the first item. But should try all items then throw an aggregate exception.
TryGet(TParam, out TVal?)
Tries to get the value if it's available.
Parameters
key
TParamThe input value of the key to use.
result
TValThe result if available, otherwise it will be the default value.
Returns
- bool
If we were able to retrieve the value or not.