Class LruCache<TKey, TValue>
A cache implementation that uses the least recently used (LRU) policy to evict stale key-value pairs.
Inherited Members
Namespace: Flame.Collections
Assembly: Flame.dll
Syntax
public sealed class LruCache<TKey, TValue> : Cache<TKey, TValue>
Type Parameters
| Name | Description |
|---|---|
| TKey | |
| TValue |
Constructors
| Edit this page View SourceLruCache(int)
Creates an LRU cache with a particular capacity.
Declaration
public LruCache(int capacity)
Parameters
| Type | Name | Description |
|---|---|---|
| int | capacity | The maximal number of key-value pairs in the LRU cache. |
LruCache(int, IEqualityComparer<TKey>)
Creates an LRU cache with a particular capacity and a key equality comparer.
Declaration
public LruCache(int capacity, IEqualityComparer<TKey> comparer)
Parameters
| Type | Name | Description |
|---|---|---|
| int | capacity | The maximal number of key-value pairs in the LRU cache. |
| IEqualityComparer<TKey> | comparer | An equality comparer for keys. |
Properties
| Edit this page View SourceCapacity
Gets the LRU cache's capacity.
Declaration
public int Capacity { get; }
Property Value
| Type | Description |
|---|---|
| int | The maximal number of key-value pairs in the LRU cache. |
Methods
| Edit this page View SourceGet(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 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 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 |
|