ThreadSafeCacheFactoryCreateTKey, TValue(FuncTKey, TValue, IEqualityComparerTKey, ThreadSafeCacheOptionsBase) Method
Namespace: KGySoft.CollectionsAssembly: KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 9.0.0
public static IThreadSafeCacheAccessor<TKey, TValue> Create<TKey, TValue>(
Func<TKey, TValue> itemLoader,
IEqualityComparer<TKey>? comparer,
ThreadSafeCacheOptionsBase? options = null
)
Public Shared Function Create(Of TKey, TValue) (
itemLoader As Func(Of TKey, TValue),
comparer As IEqualityComparer(Of TKey),
Optional options As ThreadSafeCacheOptionsBase = Nothing
) As IThreadSafeCacheAccessor(Of TKey, TValue)
public:
generic<typename TKey, typename TValue>
static IThreadSafeCacheAccessor<TKey, TValue>^ Create(
Func<TKey, TValue>^ itemLoader,
IEqualityComparer<TKey>^ comparer,
ThreadSafeCacheOptionsBase^ options = nullptr
)
static member Create :
itemLoader : Func<'TKey, 'TValue> *
comparer : IEqualityComparer<'TKey> *
?options : ThreadSafeCacheOptionsBase
(* Defaults:
let _options = defaultArg options null
*)
-> IThreadSafeCacheAccessor<'TKey, 'TValue>
- itemLoader FuncTKey, TValue
- A delegate for loading a value, which is invoked when a key is not present in the cache.
- comparer IEqualityComparerTKey
- An equality comparer to be used for hashing and comparing keys. If , then a default comparison is used.
- options ThreadSafeCacheOptionsBase (Optional)
- The options for creating the cache. If , then a default LockFreeCacheOptions instance will be used. This parameter is optional.
Default value: .
- TKey
- The type of the key in the cache.
- TValue
- The type of the value in the cache.
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 (e.g. .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.
- LockFreeCacheOptions: Use this one if you want the fastest, well scalable solution and it is not a problem that the itemLoader delegate might
be called concurrently, or capacity management is not too strict (when cache is full, about the half of the elements are dropped at once). Though rarely, it may also happen
that itemLoader is invoked multiple times when accessing the same key consecutively and the first call occurred during an internal merge session.
- LockingCacheOptions: Use this one if you need strict capacity management, you want to dispose the dropped-out values, you want to ensure that the oldest
or least recent used element is dropped in the first place, you want to protect the itemLoader delegate from calling it concurrently, or if you want
to specify an expiration time period for the values. If elements are often dropped, then it also uses less memory than the lock-free implementation. Depending on the configuration
the actual type of the returned instance may vary but in all cases an instance of the public CacheTKey, TValue type will be wrapped internally.