Table of Contents

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

K

The type of keys in the cache.

V

The type of values in the cache.

Inheritance
LRUCache<K, V>
Implements
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

capacity int

The maximum number of elements that the cache can hold. Must be greater than zero.

Exceptions

ArgumentOutOfRangeException

Thrown when capacity is zero or negative. (#14)

Properties

Count

Gets the number of elements contained in the cache.

public int Count { get; }

Property Value

int

IsReadOnly

Gets a value indicating whether the cache is read-only.

public bool IsReadOnly { get; }

Property Value

bool

this[K]

Gets or sets the value associated with the specified key.

public V this[K key] { get; set; }

Parameters

key K

The key whose value to get or set.

Property Value

V

The value associated with the specified key.

Exceptions

KeyNotFoundException

Thrown by the getter when key is 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

item KeyValuePair<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

key K

The key of the element to add.

value V

The 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

item KeyValuePair<K, V>

The key-value pair to locate in the cache.

Returns

bool

true if the cache contains the specified key-value pair; otherwise, false.

ContainsKey(K)

Determines whether the cache contains the specified key.

public bool ContainsKey(K key)

Parameters

key K

The key to locate in the cache.

Returns

bool

true if the cache contains an element with the specified key; otherwise, false.

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

array KeyValuePair<K, V>[]

The one-dimensional array that is the destination of the elements copied from the cache.

arrayIndex int

The zero-based index in the array at which copying begins.

Exceptions

ArgumentNullException

Thrown when array is null. (#17)

ArgumentOutOfRangeException

Thrown when arrayIndex is 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

item KeyValuePair<K, V>

The key-value pair to remove.

Returns

bool

true if the key-value pair was found and the value matched; otherwise, false.

Remove(K)

Removes the element with the specified key from the cache.

public bool Remove(K key)

Parameters

key K

The key of the element to remove.

Returns

bool

true if the element is successfully removed; otherwise, false.

TryGetValue(K, out V)

Gets the value associated with the specified key.

public bool TryGetValue(K key, out V value)

Parameters

key K

The key whose value to get.

value V

When 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.

Returns

bool

true if the cache contains an element with the specified key; otherwise, false.