Table of Contents

Class SlimMemoryCache

Namespace
Cuemon.Runtime.Caching
Assembly
Cuemon.Runtime.Caching.dll

Represents the type that implements an in-memory cache for an application.

public class SlimMemoryCache : Disposable, IDisposable, ICacheEnumerable<long>, IEnumerable<KeyValuePair<long, CacheEntry>>, IEnumerable
Inheritance
Object
SlimMemoryCache
Implements
IDisposable
IEnumerable<KeyValuePair<Int64, CacheEntry>>
IEnumerable
Inherited Members

Constructors

SlimMemoryCache(Action<SlimMemoryCacheOptions>)

Initializes a new instance of the SlimMemoryCache class.

public SlimMemoryCache(Action<SlimMemoryCacheOptions> setup = null)

Parameters

setup Action<SlimMemoryCacheOptions>

The SlimMemoryCacheOptions which may be configured.

Properties

Item[String, String]

Gets or sets a value in the cache by using the default indexer property for an instance of the SlimMemoryCache class.

public object this[string key, string ns = null ] { get; set; }

Parameters

key String

The unique identifier for the cache value to get or set.

ns String

The optional namespace that provides a scope to the cache.

Property Value

Object

The value in the cache for the specified key, if the entry exists; otherwise, null.

KeyProvider

Gets the function delegate that is responsible for providing a unique identifier for the cache entry.

public Func<string, string, long> KeyProvider { get; }

Property Value

Func<String, String, Int64>

The function delegate that is responsible for providing a unique identifier for the cache entry.

Methods

Add(CacheEntry, CacheInvalidation)

Inserts a cache entry into the cache and adds details about how the entry should be evicted.

public bool Add(CacheEntry entry, CacheInvalidation invalidation)

Parameters

entry CacheEntry

The object representing the cached value for a cache entry.

invalidation CacheInvalidation

The object that contains expiration details for a specific cache entry.

Returns

Boolean

true if insertion succeeded; otherwise, false when there is already an entry in the cache with the same key.

Exceptions

System.ArgumentNullException

entry cannot be null -or- invalidation cannot be null.

Add(String, Object, IDependency, String)

Inserts a cache entry into the cache and adds details about how the entry should be evicted.

public bool Add(string key, object value, IDependency dependency, string ns = null)

Parameters

key String

The unique identifier of the cache.

value Object

The stored value of the cache.

dependency IDependency

An IDependency implementations that monitors changes in the state of the data which a cache entry depends on. If a state change is registered, the cached value becomes invalid and is removed from the cache.

ns String

The optional namespace that provides a scope to the cache.

Returns

Boolean

true if insertion succeeded; otherwise, false when there is already an entry in the cache with the same key.

Exceptions

System.ArgumentNullException

key cannot be null.

Add(String, Object, IEnumerable<IDependency>, String)

Inserts a cache entry into the cache and adds details about how the entry should be evicted.

public bool Add(string key, object value, IEnumerable<IDependency> dependencies, string ns = null)

Parameters

key String

The unique identifier of the cache.

value Object

The stored value of the cache.

dependencies IEnumerable<IDependency>

A sequence of IDependency implementations that monitors changes in the state of the data which a cache entry depends on. If a state change is registered, the cached value becomes invalid and is removed from the cache.

ns String

The optional namespace that provides a scope to the cache.

Returns

Boolean

true if insertion succeeded; otherwise, false when there is already an entry in the cache with the same key.

Exceptions

System.ArgumentNullException

key cannot be null.

Add(String, Object, DateTime, String)

Inserts a cache entry into the cache and adds details about how the entry should be evicted.

public bool Add(string key, object value, DateTime absoluteExpiration, string ns = null)

Parameters

key String

The unique identifier of the cache.

value Object

The stored value of the cache.

absoluteExpiration DateTime

The absolute expiration date time value from when the cached value becomes invalid and is removed from the cache.

ns String

The optional namespace that provides a scope to the cache.

Returns

Boolean

true if insertion succeeded; otherwise, false when there is already an entry in the cache with the same key.

Exceptions

System.ArgumentNullException

key cannot be null.

Add(String, Object, TimeSpan, String)

Inserts a cache entry into the cache and adds details about how the entry should be evicted.

public bool Add(string key, object value, TimeSpan slidingExpiration, string ns = null)

Parameters

key String

The unique identifier of the cache.

value Object

The stored value of the cache.

slidingExpiration TimeSpan

The sliding expiration time from when the cached value becomes invalid and is removed from the cache.

ns String

The optional namespace that provides a scope to the cache.

Returns

Boolean

true if insertion succeeded; otherwise, false when there is already an entry in the cache with the same key.

Exceptions

System.ArgumentNullException

key cannot be null.

Contains(String, String)

Determines whether a cache entry exists in the cache.

public bool Contains(string key, string ns = null)

Parameters

key String

The unique identifier for the cache entry.

ns String

The optional namespace that provides a scope to the cache.

Returns

Boolean

true if the cache contains a cache entry whose key matches key; otherwise, false.

Exceptions

System.ArgumentNullException

key cannot be null.

Count(String)

Gets the number of entries associated with the ns contained in the cache.

public int Count(string ns = null)

Parameters

ns String

The optional namespace that provides a scope to the cache.

Returns

Int32

The number of entries contained in the cache.

Get(String, String)

Returns an entry from the cache.

public object Get(string key, string ns = null)

Parameters

key String

The unique identifier for the cache entry.

ns String

The optional namespace that provides a scope to the cache.

Returns

Object

A reference to the value in the cache container that is identified by key, if the entry exists; otherwise, null.

Exceptions

System.ArgumentNullException

key cannot be null.

GetCacheEntry(String, String)

Returns an entry from the cache as a CacheEntry instance.

public CacheEntry GetCacheEntry(string key, string ns = null)

Parameters

key String

The unique identifier for the cache entry.

ns String

The optional namespace that provides a scope to the cache.

Returns

CacheEntry

A reference to the CacheEntry that is identified by key, if the entry exists; otherwise, null.

Exceptions

System.ArgumentNullException

key cannot be null.

GetEnumerator()

Returns an enumerator that iterates through the collection.

public IEnumerator<KeyValuePair<long, CacheEntry>> GetEnumerator()

Returns

IEnumerator<KeyValuePair<Int64, CacheEntry>>

An enumerator that can be used to iterate through the collection.

OnDisposeManagedResources()

Called when this object is being disposed by either Dispose() or Dispose(Boolean) having disposing set to true and Disposed is false.

protected override void OnDisposeManagedResources()

Remove(String, String)

Removes a cache entry from the cache.

public object Remove(string key, string ns = null)

Parameters

key String

The unique identifier for the cache entry.

ns String

The optional namespace that provides a scope to the cache.

Returns

Object

If the entry is found in the cache, a reference to the value in the cache container of the removed cache entry; otherwise, null.

Exceptions

System.ArgumentNullException

key cannot be null.

RemoveAll(String)

Removes all entries associated with the ns contained in the cache.

public void RemoveAll(string ns = null)

Parameters

ns String

The optional namespace that provides a scope to the cache.

Set(String, Object, CacheInvalidation, String)

Inserts a cache entry into the cache.

public void Set(string key, object value, CacheInvalidation invalidation, string ns = null)

Parameters

key String

The unique identifier of the cache.

value Object

The stored value of the cache.

invalidation CacheInvalidation

The object that contains expiration details for a specific cache entry.

ns String

The optional namespace that provides a scope to the cache.

Remarks

The Set(String, Object, CacheInvalidation, String) method always puts a cache value in the cache, regardless whether an entry already exists with the same key. If the specified entry does not exist in the cache, a new cache entry is inserted. If the specified entry exists, its value is updated.

Exceptions

System.ArgumentNullException

key cannot be null -or- invalidation cannot be null.

TryGet(String, out Object)

Attempts to get the value associated with the specified key from the cache.

public bool TryGet(string key, out object value)

Parameters

key String

The unique identifier for the cache entry.

value Object

When this method returns, contains the value associated with the specified key, or null if the operation failed.

Returns

Boolean

true if the value was found in the cache; otherwise, false.

TryGet(String, String, out Object)

Attempts to get the value associated with the specified key and ns from the cache.

public bool TryGet(string key, string ns, out object value)

Parameters

key String

The unique identifier for the cache entry.

ns String

The optional namespace that provides a scope to the cache.

value Object

When this method returns, contains the value associated with the specified key and ns, or null if the operation failed.

Returns

Boolean

true if the value was found in the cache; otherwise, false.

Exceptions

System.ArgumentNullException

key cannot be null.

TryGetCacheEntry(String, out CacheEntry)

Attempts to get the CacheEntry associated with the specified key from the cache.

public bool TryGetCacheEntry(string key, out CacheEntry cacheEntry)

Parameters

key String

The unique identifier for the cache entry.

cacheEntry CacheEntry

When this method returns, contains the cache entry associated with the specified key, or null if the operation failed.

Returns

Boolean

true if the cacheEntry was found in the cache; otherwise, false.

TryGetCacheEntry(String, String, out CacheEntry)

Attempts to get the CacheEntry associated with the specified key and ns from the cache.

public bool TryGetCacheEntry(string key, string ns, out CacheEntry cacheEntry)

Parameters

key String

The unique identifier for the cache entry.

ns String

The optional namespace that provides a scope to the cache.

cacheEntry CacheEntry

When this method returns, contains the cache entry associated with the specified key and ns, or null if the operation failed.

Returns

Boolean

true if the cacheEntry was found in the cache; otherwise, false.

Exceptions

System.ArgumentNullException

key cannot be null.

Explicit Interface Implementations

IEnumerable.GetEnumerator()

Returns an enumerator that iterates through a collection.

IEnumerator IEnumerable.GetEnumerator()

Returns

IEnumerator

An System.Collections.IEnumerator object that can be used to iterate through the collection.

See Also