Table of Contents

Class MemoryCache<TKey, TData>

Namespace
Shift.Common
Assembly
Shift.Common.dll

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

TKey
TData
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

key TKey

The 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

key TKey

The key for a cached value.

cacheObject TData

The 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

key TKey

The key for a cached value.

data TData

The cache object to store.

cacheTimeout int

The lifespan of this object in seconds. Must be 1 or greater. Use Timeout.Infinite to keep the entry forever.

restartTimerIfExists bool

(Optional). If true then 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

disposing bool

true to release both managed and unmanaged resources; false to release only unmanaged resources.

Exists(TKey)

Checks if a specific key exists in the cache.

public bool Exists(TKey key)

Parameters

key TKey

The key for a cached value.

Returns

bool

True if the key exists in the cache, otherwise False.

Get(TKey)

Gets the value for a specific key.

public TData Get(TKey key)

Parameters

key TKey

The 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

keyPattern Predicate<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

key TKey

The 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

key TKey

The key for a cached value.

value TData

(out) The value, if found, otherwise default(T).

Returns

bool

True, if key exists, otherwise False.

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>