Class MemoryCache<TKey, TData>
This is a generic cache based on key/value pairs, where both key and value are generic. Keys must be unique. Every entry in the cache has its own timeout. Cache is a thread-safe class, and it deletes expired entries on its own using System.Threading.Timers that run on ThreadPool threads.
public class MemoryCache<TKey, TData> : IDisposable, IMemoryCache<TKey, TData>
Type Parameters
TKeyTData
- Inheritance
-
MemoryCache<TKey, TData>
- Implements
-
IMemoryCache<TKey, TData>
- Derived
- Inherited Members
- Extension Methods
Constructors
MemoryCache()
Initializes a new instance of the MemoryCache<TKey, TData> class.
public MemoryCache()
Properties
this[TKey]
Gets the cache entry with the specified key or returns default(T) if the key is not found.
public TData this[TKey key] { get; }
Parameters
keyTKeyThe key for a cached value.
Property Value
- TData
The object from the cache or
default(T), if not found.
Methods
Add(TKey, TData)
Adds or updates the specified cache-key with the specified cacheObject and applies Timeout.Infinite to this key.
public void Add(TKey key, TData cacheObject)
Parameters
keyTKeyThe key for a cached value.
cacheObjectTDataThe cache object to store.
Add(TKey, TData, int, bool)
Adds or updates the specified cache-key with the specified cacheObject and applies a specified timeout (in seconds) to this key.
public void Add(TKey key, TData data, int cacheTimeout, bool restartTimerIfExists = false)
Parameters
keyTKeyThe key for a cached value.
dataTDataThe cache object to store.
cacheTimeoutintThe lifespan of this object in seconds. Must be 1 or greater. Use Timeout.Infinite to keep the entry forever.
restartTimerIfExistsbool(Optional). If
truethen reset the timer for this cacheObject even if the object already exists in the cache.
Clear()
Clears the cache and disposes all active timers.
public void Clear()
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
Dispose(bool)
Releases unmanaged and - optionally - managed resources.
protected virtual void Dispose(bool disposing)
Parameters
disposingbooltrueto release both managed and unmanaged resources;falseto release only unmanaged resources.
Exists(TKey)
Checks if a specific key exists in the cache.
public bool Exists(TKey key)
Parameters
keyTKeyThe key for a cached value.
Returns
- bool
Trueif the key exists in the cache, otherwiseFalse.
Get(TKey)
Gets the value for a specific key.
public TData Get(TKey key)
Parameters
keyTKeyThe key for a cached value.
Returns
- TData
The corresponding value from the cache if it exists, otherwise
default(T).
GetAll()
Gets all values.
public TData[] GetAll()
Returns
- TData[]
The all values from the cache if it exists, otherwise empty array.
Remove(Predicate<TKey>)
Removes a series of cache entries in a single call for all key that match the specified key pattern.
public void Remove(Predicate<TKey> keyPattern)
Parameters
keyPatternPredicate<TKey>The key pattern to remove. The Predicate has to return true to get key removed.
Remove(TKey)
Removes the specified cache entry with the specified key. If the key is not found, no exception is thrown, the statement is just ignored.
public void Remove(TKey key)
Parameters
keyTKeyThe key for a cached value.
TryGet(TKey, out TData)
Tries to get the value for a specific key.
public bool TryGet(TKey key, out TData value)
Parameters
keyTKeyThe key for a cached value.
valueTData(out) The value, if found, otherwise
default(T).
Returns
- bool
True, ifkeyexists, otherwiseFalse.
Events
ItemAdded
public event MemoryCacheHandler<TKey, TData> ItemAdded
Event Type
- MemoryCacheHandler<TKey, TData>
ItemRemoved
public event MemoryCacheHandler<TKey, TData> ItemRemoved
Event Type
- MemoryCacheHandler<TKey, TData>