Class ExpiringDictionary<TKey, TValue>
Represents a collection of keys and values, where each element will expire and be automatically removed according to the properties "SlidingExpiration" and "AbsoluteExpiration"
Implements
Inherited Members
Namespace: SuperOffice.Util
Assembly: SoCore.dll
Syntax
public class ExpiringDictionary<TKey, TValue> : IDisposable
Type Parameters
Name | Description |
---|---|
TKey | The type of the keys in the ExpiringDictionary. |
TValue | The type of the values in the ExpiringDictionary |
Remarks
The ExpiringDictionary generic class provides a mapping from a set of keys to a set of values. Each addition to the ExpiringDictionary consists of a value and its associated key. Retrieving a value by using its key is very fast, close to O(1), because the ExpiringDictionary class is implemented with an internal Dictionary witch is implemented as a hash table.
Note
The speed of retrieval depends on the quality of the hashing algorithm of the type specified for TKey.
As long as an object is used as a key in the ExpiringDictionary, it must not change in any way that affects its hash value. Every key in an ExpiringDictionary must be unique according to the ExpiringDictionary's equality comparer. A key cannot be a null reference, but a value can be, if the value type TValue is a reference type.
ExpiringDictionary requires an equality implementation to determine whether keys are equal. You can specify an implementation of the IEqualityComparer generic interface by using a constructor that accepts a comparer parameter; if you do not specify an implementation, the default generic equality comparer EqualityComparer.Default is used. If type TKey implements the System.IEquatable generic interface, the default equality comparer uses that implementation.
The capacity of an ExpiringDictionary is the number of elements the ExpiringDictionary can hold. In this implementation, the default initial capacity for an ExpiringDictionary is 3; however, that default might change in future versions of the .NET Framework. As elements are added to an ExpiringDictionary, the capacity is automatically increased as required by reallocating the internal array.
Constructors
ExpiringDictionary()
Initializes a new instance of the ExpiringDictionary class that is empty, has the default initial capacity, and uses the default equality comparer for the key type.
Declaration
public ExpiringDictionary()
Remarks
Every key in an ExpiringDictionary must be unique according to the default equality comparer. ExpiringDictionary requires an equality implementation to determine whether keys are equal. This constructor uses the default generic equality comparer, EqualityComparer.Default. If type TKey implements the System.IEquatable generic interface, the default equality comparer uses that implementation. Alternatively, you can specify an implementation of the IEqualityComparer generic interface by using a constructor that accepts a comparer parameter. This constructor is an O(1) operation.
ExpiringDictionary(IEqualityComparer<TKey>)
Initializes a new instance of the ExpiringDictionary class that is empty, has the default initial capacity, and uses the specified IEqualityComparer.
Declaration
public ExpiringDictionary(IEqualityComparer<TKey> comparer)
Parameters
Type | Name | Description |
---|---|---|
IEqualityComparer<TKey> | comparer | The IEqualityComparer implementation to use when comparing keys, or a null reference to use the default EqualityComparer for the type of the key. |
Remarks
Every key in an ExpiringDictionary must be unique according to the specified comparer. ExpiringDictionary requires an equality implementation to determine whether keys are equal. If comparer is a null reference, this constructor uses the default generic equality comparer, EqualityComparer.Default. If type TKey implements the System.IEquatable generic interface, the default equality comparer uses that implementation. This constructor is an O(1) operation.
ExpiringDictionary(IEqualityComparer<TKey>, TimeSpan)
Initializes a new instance of the ExpiringDictionary class that is empty, has the default initial capacity, and uses the specified IEqualityComparer, and uses a specific sliding expiration.
Declaration
public ExpiringDictionary(IEqualityComparer<TKey> comparer, TimeSpan slidingExpiration)
Parameters
Type | Name | Description |
---|---|---|
IEqualityComparer<TKey> | comparer | The IEqualityComparer implementation to use when comparing keys, or a null reference to use the default EqualityComparer for the type of the key. |
TimeSpan | slidingExpiration | A |
Remarks
Every key in an ExpiringDictionary must be unique according to the specified comparer. ExpiringDictionary requires an equality implementation to determine whether keys are equal. If comparer is a null reference, this constructor uses the default generic equality comparer, EqualityComparer.Default. If type TKey implements the System.IEquatable generic interface, the default equality comparer uses that implementation. This constructor is an O(1) operation.
ExpiringDictionary(int)
Initializes a new instance of the ExpiringDictionary class that is empty, has the specified initial capacity, and uses the default equality comparer for the key type.
Declaration
public ExpiringDictionary(int capacity)
Parameters
Type | Name | Description |
---|---|---|
int | capacity | The initial number of elements that the ExpiringDictionary can contain. |
Remarks
Every key in an ExpiringDictionary must be unique according to the default equality comparer. The capacity of an ExpiringDictionary is the number of elements that can be added to the ExpiringDictionary before resizing is necessary. As elements are added to an ExpiringDictionary, the capacity is automatically increased as required by reallocating the internal array. If the size of the collection can be estimated, specifying the initial capacity eliminates the need to perform a number of resizing operations while adding elements to the ExpiringDictionary. ExpiringDictionary requires an equality implementation to determine whether keys are equal. This constructor uses the default generic equality comparer, EqualityComparer.Default. If type TKey implements the System.IEquatable generic interface, the default equality comparer uses that implementation. Alternatively, you can specify an implementation of the IEqualityComparer generic interface by using a constructor that accepts a comparer parameter. This constructor is an O(1) operation.
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException | capacity is less than 0. |
ExpiringDictionary(int, IEqualityComparer<TKey>)
Initializes a new instance of the ExpiringDictionary class that is empty, has the specified initial capacity, and uses the specified IEqualityComparer.
Declaration
public ExpiringDictionary(int capacity, IEqualityComparer<TKey> comparer)
Parameters
Type | Name | Description |
---|---|---|
int | capacity | The initial number of elements that the ExpiringDictionary can contain. |
IEqualityComparer<TKey> | comparer | The IEqualityComparer implementation to use when comparing keys, or a null reference to use the default EqualityComparer for the type of the key. |
Remarks
Every key in an ExpiringDictionary must be unique according to the specified comparer. The capacity of an ExpiringDictionary is the number of elements that can be added to the ExpiringDictionary before resizing is necessary. As elements are added to an ExpiringDictionary, the capacity is automatically increased as required by reallocating the internal array. If the size of the collection can be estimated, specifying the initial capacity eliminates the need to perform a number of resizing operations while adding elements to the ExpiringDictionary. ExpiringDictionary requires an equality implementation to determine whether keys are equal. If comparer is a null reference, this constructor uses the default generic equality comparer, EqualityComparer.Default. If type TKey implements the System.IEquatable generic interface, the default equality comparer uses that implementation. This constructor is an O(1) operation.
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException | capacity is less than 0. |
ExpiringDictionary(int, IEqualityComparer<TKey>, TimeSpan)
Initializes a new instance of the ExpiringDictionary class that is empty, has the specified initial capacity, and uses the specified IEqualityComparer, and uses a specific sliding expiration.
Declaration
public ExpiringDictionary(int capacity, IEqualityComparer<TKey> comparer, TimeSpan slidingExpiration)
Parameters
Type | Name | Description |
---|---|---|
int | capacity | The initial number of elements that the ExpiringDictionary can contain. |
IEqualityComparer<TKey> | comparer | The IEqualityComparer implementation to use when comparing keys, or a null reference to use the default EqualityComparer for the type of the key. |
TimeSpan | slidingExpiration | A |
Remarks
Every key in an ExpiringDictionary must be unique according to the specified comparer. The capacity of an ExpiringDictionary is the number of elements that can be added to the ExpiringDictionary before resizing is necessary. As elements are added to an ExpiringDictionary, the capacity is automatically increased as required by reallocating the internal array. If the size of the collection can be estimated, specifying the initial capacity eliminates the need to perform a number of resizing operations while adding elements to the ExpiringDictionary. ExpiringDictionary requires an equality implementation to determine whether keys are equal. If comparer is a null reference, this constructor uses the default generic equality comparer, EqualityComparer.Default. If type TKey implements the System.IEquatable generic interface, the default equality comparer uses that implementation. This constructor is an O(1) operation.
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException | capacity is less than 0. |
ExpiringDictionary(int, TimeSpan)
Initializes a new instance of the ExpiringDictionary class that is empty, has the specified initial capacity, and uses the default equality comparer for the key type, and uses a specific sliding expiration.
Declaration
public ExpiringDictionary(int capacity, TimeSpan slidingExpiration)
Parameters
Type | Name | Description |
---|---|---|
int | capacity | The initial number of elements that the ExpiringDictionary can contain. |
TimeSpan | slidingExpiration | A |
Remarks
Every key in an ExpiringDictionary must be unique according to the default equality comparer. The capacity of an ExpiringDictionary is the number of elements that can be added to the ExpiringDictionary before resizing is necessary. As elements are added to an ExpiringDictionary, the capacity is automatically increased as required by reallocating the internal array. If the size of the collection can be estimated, specifying the initial capacity eliminates the need to perform a number of resizing operations while adding elements to the ExpiringDictionary. ExpiringDictionary requires an equality implementation to determine whether keys are equal. This constructor uses the default generic equality comparer, EqualityComparer.Default. If type TKey implements the System.IEquatable generic interface, the default equality comparer uses that implementation. Alternatively, you can specify an implementation of the IEqualityComparer generic interface by using a constructor that accepts a comparer parameter. This constructor is an O(1) operation.
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException | capacity is less than 0. |
ExpiringDictionary(TimeSpan)
Initializes a new instance of the ExpiringDictionary class that is empty, has the default initial capacity, and uses the default equality comparer for the key type, and uses a specific sliding expiration.
Declaration
public ExpiringDictionary(TimeSpan slidingExpiration)
Parameters
Type | Name | Description |
---|---|---|
TimeSpan | slidingExpiration | A |
Remarks
Every key in an ExpiringDictionary must be unique according to the default equality comparer. ExpiringDictionary requires an equality implementation to determine whether keys are equal. This constructor uses the default generic equality comparer, EqualityComparer.Default. If type TKey implements the System.IEquatable generic interface, the default equality comparer uses that implementation. Alternatively, you can specify an implementation of the IEqualityComparer generic interface by using a constructor that accepts a comparer parameter. This constructor is an O(1) operation.
Properties
AbsoluteExpiration
The interval between the time the element was created and the time at which that element expires.
Declaration
public TimeSpan AbsoluteExpiration { get; set; }
Property Value
Type | Description |
---|---|
TimeSpan | A |
Remarks
Setting this property to TimeSpan.Zero
will prevent the ExpiringDictionary from using absolute expiration.
Comparer
Gets the IEqualityComparer that is used to determine equality of keys for the ExpiringDictionary.
Declaration
public IEqualityComparer<TKey> Comparer { get; }
Property Value
Type | Description |
---|---|
IEqualityComparer<TKey> | The IEqualityComparer generic interface implementation that is used to determine equality of keys for the current ExpiringDictionary and to provide hash values for the keys. |
Remarks
ExpiringDictionary requires an equality implementation to determine whether keys are equal. You can specify an implementation of the IEqualityComparer generic interface by using a constructor that accepts a comparer parameter; if you do not specify one, the default generic equality comparer EqualityComparer.Default is used. Getting the value of this property is an O(1) operation.
Count
Gets the number of key/value pairs contained in the ExpiringDictionary.
Declaration
public int Count { get; }
Property Value
Type | Description |
---|---|
int | The number of key/value pairs contained in the ExpiringDictionary. |
Remarks
Count also includes elements that has expired but is not yet removed by the timer function. The capacity of an ExpiringDictionary is the number of elements that the ExpiringDictionary can store. The Count property is the number of elements that are actually in the ExpiringDictionary. The capacity is always greater than or equal to Count. If Count exceeds the capacity while adding elements, the capacity is increased by automatically reallocating the internal array before copying the old elements and adding the new elements. Getting the value of this property is an O(1) operation.
this[TKey]
Gets or sets the value associated with the specified key.
Declaration
public TValue this[TKey key] { get; set; }
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key of the value to get or set. |
Property Value
Type | Description |
---|---|
TValue | The value associated with the specified key. If the specified key is not found, a get operation throws a KeyNotFoundException, and a set operation creates a new element with the specified key. |
Remarks
The ExpiringDictionary generic class provides a mapping from a set of keys to a set of values. Each addition to the ExpiringDictionary consists of a value and its associated key. Retrieving a value by using its key is very fast, close to O(1), because the ExpiringDictionary class is implemented with an internal Dictionary witch is implemented as a hash table.
Note
The speed of retrieval depends on the quality of the hashing algorithm of the type specified for TKey.
As long as an object is used as a key in the ExpiringDictionary, it must not change in any way that affects its hash value. Every key in an ExpiringDictionary must be unique according to the ExpiringDictionary's equality comparer. A key cannot be a null reference, but a value can be, if the value type TValue is a reference type.
ExpiringDictionary requires an equality implementation to determine whether keys are equal. You can specify an implementation of the IEqualityComparer generic interface by using a constructor that accepts a comparer parameter; if you do not specify an implementation, the default generic equality comparer EqualityComparer.Default is used. If type TKey implements the System.IEquatable generic interface, the default equality comparer uses that implementation.
The capacity of an ExpiringDictionary is the number of elements the ExpiringDictionary can hold. In this implementation, the default initial capacity for an ExpiringDictionary is 3; however, that default might change in future versions of the .NET Framework. As elements are added to an ExpiringDictionary, the capacity is automatically increased as required by reallocating the internal array.
Exceptions
Type | Condition |
---|---|
ArgumentNullException | key is a null reference |
KeyNotFoundException | The property is retrieved and key does not exist in the collection. |
SlidingExpiration
The interval between the time the element was last accessed and the time at which that element expires.
Declaration
public TimeSpan SlidingExpiration { get; set; }
Property Value
Type | Description |
---|---|
TimeSpan | A |
Remarks
Setting this property to TimeSpan.Zero
will prevent the ExpiringDictionary from using sliding expiration.
Methods
Add(TKey, TValue)
Adds the specified key and value to the ExpiringDictionary.
Declaration
public void Add(TKey key, TValue value)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key of the element to add. |
TValue | value | The value of the element to add. The value can be a null reference |
Remarks
You can also use the Item property to add new elements by setting the value of a key that does not exist in the ExpiringDictionary; for example, myExpiringDictionary[myKey] = myValue. However, if the specified key already exists in the ExpiringDictionary, setting the Item property overwrites the old value. In contrast, the Add method throws an exception if a value with the specified key already exists. If the Count property value already equals the capacity, the capacity of the ExpiringDictionary is increased by automatically reallocating the internal array, and the existing elements are copied to the new array before the new element is added. A key cannot be a null reference, but a value can be, if TValue is a reference type. If Count is less than the capacity, this method approaches an O(1) operation. If the capacity must be increased to accommodate the new element, this method becomes an O(n) operation, where n is Count.
Exceptions
Type | Condition |
---|---|
ArgumentNullException | key is a null reference |
ArgumentException | An element with the same key already exists in the ExpiringDictionary. |
Clear()
Removes all keys and values from the ExpiringDictionary.
Declaration
public void Clear()
Remarks
The Count property is set to 0, and references to other objects from elements of the collection are also released. The capacity remains unchanged. This method is an O(n) operation, where n is Count.
ContainsKey(TKey)
Determines whether the ExpiringDictionary contains the specified key.
Declaration
public bool ContainsKey(TKey key)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key to locate in the ExpiringDictionary. |
Returns
Type | Description |
---|---|
bool | true if the ExpiringDictionary contains an element with the specified key; otherwise, false. |
Remarks
The ExpiringDictionary generic class provides a mapping from a set of keys to a set of values. Each addition to the ExpiringDictionary consists of a value and its associated key. Retrieving a value by using its key is very fast, close to O(1), because the ExpiringDictionary class is implemented with an internal Dictionary witch is implemented as a hash table.
Note
The speed of retrieval depends on the quality of the hashing algorithm of the type specified for TKey.
As long as an object is used as a key in the ExpiringDictionary, it must not change in any way that affects its hash value. Every key in an ExpiringDictionary must be unique according to the ExpiringDictionary's equality comparer. A key cannot be a null reference, but a value can be, if the value type TValue is a reference type.
ExpiringDictionary requires an equality implementation to determine whether keys are equal. You can specify an implementation of the IEqualityComparer generic interface by using a constructor that accepts a comparer parameter; if you do not specify an implementation, the default generic equality comparer EqualityComparer.Default is used. If type TKey implements the System.IEquatable generic interface, the default equality comparer uses that implementation.
The capacity of an ExpiringDictionary is the number of elements the ExpiringDictionary can hold. In this implementation, the default initial capacity for an ExpiringDictionary is 3; however, that default might change in future versions of the .NET Framework. As elements are added to an ExpiringDictionary, the capacity is automatically increased as required by reallocating the internal array.
Exceptions
Type | Condition |
---|---|
ArgumentNullException | key is a null reference |
Dispose()
Represents a collection of keys and values, where each element will expire and be automatically removed according to the properties "SlidingExpiration" and "AbsoluteExpiration"
Declaration
public void Dispose()
Remarks
The ExpiringDictionary generic class provides a mapping from a set of keys to a set of values. Each addition to the ExpiringDictionary consists of a value and its associated key. Retrieving a value by using its key is very fast, close to O(1), because the ExpiringDictionary class is implemented with an internal Dictionary witch is implemented as a hash table.
Note
The speed of retrieval depends on the quality of the hashing algorithm of the type specified for TKey.
As long as an object is used as a key in the ExpiringDictionary, it must not change in any way that affects its hash value. Every key in an ExpiringDictionary must be unique according to the ExpiringDictionary's equality comparer. A key cannot be a null reference, but a value can be, if the value type TValue is a reference type.
ExpiringDictionary requires an equality implementation to determine whether keys are equal. You can specify an implementation of the IEqualityComparer generic interface by using a constructor that accepts a comparer parameter; if you do not specify an implementation, the default generic equality comparer EqualityComparer.Default is used. If type TKey implements the System.IEquatable generic interface, the default equality comparer uses that implementation.
The capacity of an ExpiringDictionary is the number of elements the ExpiringDictionary can hold. In this implementation, the default initial capacity for an ExpiringDictionary is 3; however, that default might change in future versions of the .NET Framework. As elements are added to an ExpiringDictionary, the capacity is automatically increased as required by reallocating the internal array.
~ExpiringDictionary()
Represents a collection of keys and values, where each element will expire and be automatically removed according to the properties "SlidingExpiration" and "AbsoluteExpiration"
Declaration
protected ~ExpiringDictionary()
Remarks
The ExpiringDictionary generic class provides a mapping from a set of keys to a set of values. Each addition to the ExpiringDictionary consists of a value and its associated key. Retrieving a value by using its key is very fast, close to O(1), because the ExpiringDictionary class is implemented with an internal Dictionary witch is implemented as a hash table.
Note
The speed of retrieval depends on the quality of the hashing algorithm of the type specified for TKey.
As long as an object is used as a key in the ExpiringDictionary, it must not change in any way that affects its hash value. Every key in an ExpiringDictionary must be unique according to the ExpiringDictionary's equality comparer. A key cannot be a null reference, but a value can be, if the value type TValue is a reference type.
ExpiringDictionary requires an equality implementation to determine whether keys are equal. You can specify an implementation of the IEqualityComparer generic interface by using a constructor that accepts a comparer parameter; if you do not specify an implementation, the default generic equality comparer EqualityComparer.Default is used. If type TKey implements the System.IEquatable generic interface, the default equality comparer uses that implementation.
The capacity of an ExpiringDictionary is the number of elements the ExpiringDictionary can hold. In this implementation, the default initial capacity for an ExpiringDictionary is 3; however, that default might change in future versions of the .NET Framework. As elements are added to an ExpiringDictionary, the capacity is automatically increased as required by reallocating the internal array.
Remove(TKey)
Removes the value with the specified key from the ExpiringDictionary.
Declaration
public bool Remove(TKey key)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key of the element to remove. |
Returns
Type | Description |
---|---|
bool | true if the element is successfully found and removed; otherwise, false. This method returns false if key is not found in the ExpiringDictionary. |
Remarks
The ExpiringDictionary generic class provides a mapping from a set of keys to a set of values. Each addition to the ExpiringDictionary consists of a value and its associated key. Retrieving a value by using its key is very fast, close to O(1), because the ExpiringDictionary class is implemented with an internal Dictionary witch is implemented as a hash table.
Note
The speed of retrieval depends on the quality of the hashing algorithm of the type specified for TKey.
As long as an object is used as a key in the ExpiringDictionary, it must not change in any way that affects its hash value. Every key in an ExpiringDictionary must be unique according to the ExpiringDictionary's equality comparer. A key cannot be a null reference, but a value can be, if the value type TValue is a reference type.
ExpiringDictionary requires an equality implementation to determine whether keys are equal. You can specify an implementation of the IEqualityComparer generic interface by using a constructor that accepts a comparer parameter; if you do not specify an implementation, the default generic equality comparer EqualityComparer.Default is used. If type TKey implements the System.IEquatable generic interface, the default equality comparer uses that implementation.
The capacity of an ExpiringDictionary is the number of elements the ExpiringDictionary can hold. In this implementation, the default initial capacity for an ExpiringDictionary is 3; however, that default might change in future versions of the .NET Framework. As elements are added to an ExpiringDictionary, the capacity is automatically increased as required by reallocating the internal array.
Exceptions
Type | Condition |
---|---|
ArgumentNullException | key is a null reference |
TryGetValue(TKey, out TValue)
Gets the value associated with the specified key.
Declaration
public bool TryGetValue(TKey key, out TValue value)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key of the value to get. |
TValue | value | When this method returns, contains the value associated with the specified key, if the key is found; otherwise, the default value for the type of the value parameter. This parameter is passed uninitialized. |
Returns
Type | Description |
---|---|
bool |
|
Remarks
The ExpiringDictionary generic class provides a mapping from a set of keys to a set of values. Each addition to the ExpiringDictionary consists of a value and its associated key. Retrieving a value by using its key is very fast, close to O(1), because the ExpiringDictionary class is implemented with an internal Dictionary witch is implemented as a hash table.
Note
The speed of retrieval depends on the quality of the hashing algorithm of the type specified for TKey.
As long as an object is used as a key in the ExpiringDictionary, it must not change in any way that affects its hash value. Every key in an ExpiringDictionary must be unique according to the ExpiringDictionary's equality comparer. A key cannot be a null reference, but a value can be, if the value type TValue is a reference type.
ExpiringDictionary requires an equality implementation to determine whether keys are equal. You can specify an implementation of the IEqualityComparer generic interface by using a constructor that accepts a comparer parameter; if you do not specify an implementation, the default generic equality comparer EqualityComparer.Default is used. If type TKey implements the System.IEquatable generic interface, the default equality comparer uses that implementation.
The capacity of an ExpiringDictionary is the number of elements the ExpiringDictionary can hold. In this implementation, the default initial capacity for an ExpiringDictionary is 3; however, that default might change in future versions of the .NET Framework. As elements are added to an ExpiringDictionary, the capacity is automatically increased as required by reallocating the internal array.
Exceptions
Type | Condition |
---|---|
ArgumentNullException | key is a null reference |