Class SimpleCache<TKey, KValue>
Simple cache implementation.
Namespace: Evergine.Common.Collections
Assembly: Evergine.Common.dll
Syntax
public class SimpleCache<TKey, KValue>
Type Parameters
Name | Description |
---|---|
TKey | The cache key. |
KValue | The cache value. |
Constructors
SimpleCache()
Initializes a new instance of the SimpleCache<TKey, KValue> class.
Declaration
public SimpleCache()
Properties
Expiration
Gets or sets the cache expiration time.
Declaration
public long Expiration { get; set; }
Property Value
Type | Description |
---|---|
long |
Methods
CheckConsistency(Action<TKey>)
Checks cache consistency and removes unused elements.
Declaration
public void CheckConsistency(Action<TKey> itemRemovedFunc = null)
Parameters
Type | Name | Description |
---|---|---|
Action<TKey> | itemRemovedFunc | Method invoked when an item is removed. |
GetOrAdd(TKey, Func<TKey, KValue>)
Gets an item from the cache using its key. If it doesn't exist, it adds one to the cache.
Declaration
public KValue GetOrAdd(TKey key, Func<TKey, KValue> createFunction = null)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key to search. |
Func<TKey, KValue> | createFunction | The function to create an item if it is not in the cache. |
Returns
Type | Description |
---|---|
KValue | The key item. If it doesn't exist in the cache, a new one is created and added. |
GetOrAdd<TContext>(TKey, TContext, Func<TKey, TContext, KValue>)
Gets an item from the cache using its key. If it doesn't exist, it adds one to the cache.
Declaration
public KValue GetOrAdd<TContext>(TKey key, TContext context, Func<TKey, TContext, KValue> createFunction = null)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key to search. |
TContext | context | The creation function context. |
Func<TKey, TContext, KValue> | createFunction | The creation function in case the element is not in the cache. |
Returns
Type | Description |
---|---|
KValue | The key item. If it doesn't exist in the cache, a new one is created and added. |
Type Parameters
Name | Description |
---|---|
TContext | The createFunction context. |
UpdateOrAdd(TKey, Func<KValue>)
Gets an item of the cache by its key. If it doesn't exist, it adds one to the cache.
Declaration
public KValue UpdateOrAdd(TKey key, Func<KValue> createFunction = null)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key to search for. |
Func<KValue> | createFunction | The function to create an item if it is not in the cache. |
Returns
Type | Description |
---|---|
KValue | The cached item. If it doesn't exist in the cache, a new one is created and added. |