See the Remarks section for details.
Namespace: KGySoft.Collections
Assembly: KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 7.0.0-preview.3
public static IThreadSafeCacheAccessor<TKey, TValue> Create<TKey, TValue>( Func<TKey, TValue> itemLoader, IEqualityComparer<TKey>? comparer, ThreadSafeCacheOptionsBase? options = null )
Parameters
- itemLoader
- Type: SystemFuncTKey, TValue
A delegate for loading a value, which is invoked when a key is not present in the cache. - comparer
- Type: System.Collections.GenericIEqualityComparerTKey
An equality comparer to be used for hashing and comparing keys. If , then a default comparison is used. - options (Optional)
- Type: KGySoft.CollectionsThreadSafeCacheOptionsBase
The options for creating the cache. If , then a default LockFreeCacheOptions instance will be used. This parameter is optional.
Default value: .
Type Parameters
- TKey
- The type of the key in the cache.
- TValue
- The type of the value in the cache.
Return Value
Type: IThreadSafeCacheAccessorTKey, TValueAn IThreadSafeCacheAccessorTKey, TValue instance that can be used to read the underlying cache in a thread-safe manner.
![]() |
---|
If TKey is string and it is safe to use a non-randomized string comparer, then you can pass StringSegmentComparer.Ordinal to the comparer parameter for better performance. Or, you can use StringSegmentComparer.OrdinalRandomized to use a comparer with randomized hash also on platforms where default string hashing is not randomized (eg. .NET Framework 3.5). |
A cache is similar to a dictionary (in terms of using a fast, associative storage) but additionally provides capacity management and transparent access (meaning, all that is needed is to read the indexer of the returned IThreadSafeCacheAccessorTKey, TValue instance, and it is transparent for the consumer whether the returned item was returned from the cache or it was loaded by invoking the specified itemLoader).
If options is , then a lock-free cache instance will be created as if a LockFreeCacheOptions was used with its default settings.
In KGy SOFT Core Libraries there are two predefined classes that can be used to create a thread-safe cache instance: LockFreeCacheOptions and LockingCacheOptions.
![]() |
---|
|