Assembly: KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 5.0.0-rc.1
public Cache( Func<TKey, TValue> itemLoader, int capacity = 128, IEqualityComparer<TKey> comparer = null )
Public Sub New ( itemLoader As Func(Of TKey, TValue), Optional capacity As Integer = 128, Optional comparer As IEqualityComparer(Of TKey) = Nothing )
public: Cache( Func<TKey, TValue>^ itemLoader, int capacity = 128, IEqualityComparer<TKey>^ comparer = nullptr )
new : itemLoader : Func<'TKey, 'TValue> * ?capacity : int * ?comparer : IEqualityComparer<'TKey> (* Defaults: let _capacity = defaultArg capacity 128 let _comparer = defaultArg comparer null *) -> Cache
- Type: SystemFuncTKey, TValue
A delegate that contains the item loader routine. This delegate is accessed whenever a non-cached item is about to be loaded by reading the indexer. If , then similarly to a regular DictionaryTKey, TValue, a KeyNotFoundException will be thrown on accessing a non-existing key.
- capacity (Optional)
- Type: SystemInt32
Capacity of the CacheTKey, TValue (possible maximum value of Count). This parameter is optional.
Default value: 128.
- comparer (Optional)
- Type: System.Collections.GenericIEqualityComparerTKey
The IEqualityComparerT implementation to use when comparing keys. When , EnumComparer<TEnum>.Comparer will be used for enum key types, and EqualityComparer<T>.Default for other types. This parameter is optional.
Default value: .
|ArgumentOutOfRangeException||capacity is less or equal to 0.|
Every key in a CacheTKey, TValue must be unique according to the specified comparer.
The capacity of a CacheTKey, TValue is the maximum number of elements that the CacheTKey, TValue can hold. When EnsureCapacity is , the internal store is allocated when the first element is added to the cache. When EnsureCapacity is , then as elements are added to the CacheTKey, TValue, the inner storage is automatically increased as required until Capacity is reached or exceeded. When EnsureCapacity is turned on while there are elements in the CacheTKey, TValue, then internal storage will be reallocated to have exactly the same size that Capacity defines. The possible exceeding storage will be trimmed in this case.
When CacheTKey, TValue is full (that is, when Count reaches Capacity) and a new element is about to be stored, then an element will be dropped out from the cache. The strategy is controlled by Behavior property.
If you want to add elements manually to the CacheTKey, TValue, then you can pass to the itemLoader parameter. In this case the CacheTKey, TValue can be used similarly to a DictionaryTKey, TValue: before getting an element, its existence must be checked by ContainsKey or TryGetValue methods, though Capacity is still maintained based on the strategy specified in the Behavior property.