Class WeakCache<TKey, TValue>
A cache that maps keys to values for as long as neither the key nor the value of a key-value pair is garbage-collected. Weak caches do not prevent keys or values from being garbage-collected.
Inherited Members
Namespace: Flame.Collections
Assembly: Flame.dll
Syntax
public sealed class WeakCache<TKey, TValue> : Cache<TKey, TValue> where TKey : class where TValue : class
Type Parameters
| Name | Description |
|---|---|
| TKey | |
| TValue |
Remarks
Public instance methods of this class are thread-safe.
Constructors
| Edit this page View SourceWeakCache()
Creates a weak cache.
Declaration
public WeakCache()
WeakCache(IEqualityComparer<TKey>)
Creates a weak cache that uses a particular key comparer under the hood.
Declaration
public WeakCache(IEqualityComparer<TKey> keyComparer)
Parameters
| Type | Name | Description |
|---|---|---|
| IEqualityComparer<TKey> | keyComparer | A key comparer. |
WeakCache(IEqualityComparer<TKey>, int)
Creates a weak cache that uses a particular key comparer under the hood and can be accessed by up to maxConcurrency threads simultaneously (under ideal circumstances).
Declaration
public WeakCache(IEqualityComparer<TKey> keyComparer, int maxConcurrency)
Parameters
| Type | Name | Description |
|---|---|---|
| IEqualityComparer<TKey> | keyComparer | A key comparer. |
| int | maxConcurrency | The maximum number of threads that can access the weak cache simultaneously under optimal circumstances. |
Methods
| Edit this page View SourceCleanup()
Explictly cleans up all outdated keys in the weak cache.
Declaration
public void Cleanup()
Get(TKey, Func<TKey, TValue>)
Queries the cache for the value with a particular key. If that value cannot be found, the key is recomputed.
Declaration
public override TValue Get(TKey key, Func<TKey, TValue> createValue)
Parameters
| Type | Name | Description |
|---|---|---|
| TKey | key | The key of the value to query. |
| Func<TKey, TValue> | createValue | A callback that creates the value for the key, in case the key was not in the cache. |
Returns
| Type | Description |
|---|---|
| TValue | The value for the key. |
Overrides
| Edit this page View SourceInsert(TKey, TValue)
Inserts a new key-value pair into this cache, or overwrites the value for an existing key.
Declaration
public override sealed void Insert(TKey key, TValue value)
Parameters
| Type | Name | Description |
|---|---|---|
| TKey | key | The cached value's key. |
| TValue | value | The value to cache. |
Overrides
| Edit this page View SourceTryGet(TKey, out TValue)
Tries to query the cache for the value with a particular key.
Declaration
public override sealed bool TryGet(TKey key, out TValue value)
Parameters
| Type | Name | Description |
|---|---|---|
| TKey | key | The key of the value to query. |
| TValue | value | A cached value, if any. |
Returns
| Type | Description |
|---|---|
| bool |
|