Class LRUCache<K, V>
- Namespace
- Utils.Collections
- Assembly
- Utils.dll
A Least Recently Used (LRU) cache implementation. This cache evicts the least recently accessed items when it reaches its capacity.
public class LRUCache<K, V> : IDictionary<K, V>, ICollection<KeyValuePair<K, V>>, IEnumerable<KeyValuePair<K, V>>, IEnumerable where K : notnull
Type Parameters
KThe type of keys in the cache.
VThe type of values in the cache.
- Inheritance
-
LRUCache<K, V>
- Implements
-
IDictionary<K, V>ICollection<KeyValuePair<K, V>>IEnumerable<KeyValuePair<K, V>>
- Inherited Members
- Extension Methods
Remarks
This type is thread-safe: every member, including enumeration and the Keys/ Values views, is synchronized through a single internal lock (a private object, not this instance — locking on the cache instance itself from calling code has no effect on it). Enumerating the cache (directly, or through Keys or Values) takes a point-in-time snapshot under that lock rather than returning a live cursor, so it never throws because of a concurrent modification and never observes a torn state — it just won't reflect mutations made after the snapshot was taken. As with most thread-safe collections, individual members are atomic but compound operations spanning more than one call (e.g. "add if not already present", or "read-modify-write") are not currently supported atomically by this type; there is no public way to make such a sequence atomic from outside the class.
Constructors
LRUCache(int)
Initializes a new instance of the LRUCache<K, V> class with the specified capacity.
public LRUCache(int capacity)
Parameters
capacityintThe maximum number of elements that the cache can hold. Must be greater than zero.
Exceptions
- ArgumentOutOfRangeException
Thrown when
capacityis zero or negative. (#14)
Properties
Count
Gets the number of elements contained in the cache.
public int Count { get; }
Property Value
IsReadOnly
Gets a value indicating whether the cache is read-only.
public bool IsReadOnly { get; }
Property Value
this[K]
Gets or sets the value associated with the specified key.
public V this[K key] { get; set; }
Parameters
keyKThe key whose value to get or set.
Property Value
- V
The value associated with the specified key.
Exceptions
- KeyNotFoundException
Thrown by the getter when
keyis not present in the cache. (#15)
Keys
Gets the keys of the cache as a live read-only view — no allocation on each access.
public ICollection<K> Keys { get; }
Property Value
- ICollection<K>
Values
Gets the values of the cache as a live read-only view — no allocation on each access.
public ICollection<V> Values { get; }
Property Value
- ICollection<V>
Methods
Add(KeyValuePair<K, V>)
Adds the specified key-value pair to the cache.
public void Add(KeyValuePair<K, V> item)
Parameters
itemKeyValuePair<K, V>The key-value pair to add.
Add(K, V)
Adds the specified key and value to the cache. If the cache exceeds its capacity, it removes the least recently used item.
public void Add(K key, V value)
Parameters
keyKThe key of the element to add.
valueVThe value of the element to add.
Clear()
Removes all elements from the cache.
public void Clear()
Contains(KeyValuePair<K, V>)
Determines whether the cache contains a specific key-value pair.
public bool Contains(KeyValuePair<K, V> item)
Parameters
itemKeyValuePair<K, V>The key-value pair to locate in the cache.
Returns
ContainsKey(K)
Determines whether the cache contains the specified key.
public bool ContainsKey(K key)
Parameters
keyKThe key to locate in the cache.
Returns
CopyTo(KeyValuePair<K, V>[], int)
Copies the elements of the cache to an array, starting at a particular array index.
public void CopyTo(KeyValuePair<K, V>[] array, int arrayIndex)
Parameters
arrayKeyValuePair<K, V>[]The one-dimensional array that is the destination of the elements copied from the cache.
arrayIndexintThe zero-based index in the array at which copying begins.
Exceptions
- ArgumentNullException
Thrown when
arrayis null. (#17)- ArgumentOutOfRangeException
Thrown when
arrayIndexis negative. (#17)- ArgumentException
Thrown when the destination array does not have enough room. (#17)
GetEnumerator()
Returns an enumerator over a point-in-time snapshot of the cache, taken under the internal lock.
public IEnumerator<KeyValuePair<K, V>> GetEnumerator()
Returns
- IEnumerator<KeyValuePair<K, V>>
An enumerator for the cache.
Remove(KeyValuePair<K, V>)
Removes the first occurrence of a specific key-value pair from the cache. Both the key and value must match for the entry to be removed. (#16)
public bool Remove(KeyValuePair<K, V> item)
Parameters
itemKeyValuePair<K, V>The key-value pair to remove.
Returns
Remove(K)
Removes the element with the specified key from the cache.
public bool Remove(K key)
Parameters
keyKThe key of the element to remove.
Returns
TryGetValue(K, out V)
Gets the value associated with the specified key.
public bool TryGetValue(K key, out V value)
Parameters
keyKThe key whose value to get.
valueVWhen this method returns, the value associated with the specified key, if the key is found; otherwise, the default value for the type of the value parameter.