Class SlimMemoryCache
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
-
SlimMemoryCache
- Implements
- Inherited Members
Constructors
SlimMemoryCache(Action<SlimMemoryCacheOptions>)
Initializes a new instance of the SlimMemoryCache class.
public SlimMemoryCache(Action<SlimMemoryCacheOptions> setup = null)
Parameters
setupAction<SlimMemoryCacheOptions>The SlimMemoryCacheOptions which may be configured.
Properties
this[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
keystringThe unique identifier for the cache value to get or set.
nsstringThe 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, long>
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
entryCacheEntryThe object representing the cached value for a cache entry.
invalidationCacheInvalidationThe object that contains expiration details for a specific cache entry.
Returns
- bool
trueif insertion succeeded; otherwise,falsewhen there is already an entry in the cache with the same key.
Exceptions
- ArgumentNullException
entrycannot be null -or-invalidationcannot 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
keystringThe unique identifier of the cache.
valueobjectThe stored value of the cache.
dependencyIDependencyAn 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
valuebecomes invalid and is removed from the cache.nsstringThe optional namespace that provides a scope to the cache.
Returns
- bool
trueif insertion succeeded; otherwise,falsewhen there is already an entry in the cache with the same key.
Exceptions
- ArgumentNullException
keycannot 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
keystringThe unique identifier of the cache.
valueobjectThe stored value of the cache.
dependenciesIEnumerable<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
valuebecomes invalid and is removed from the cache.nsstringThe optional namespace that provides a scope to the cache.
Returns
- bool
trueif insertion succeeded; otherwise,falsewhen there is already an entry in the cache with the same key.
Exceptions
- ArgumentNullException
keycannot 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
keystringThe unique identifier of the cache.
valueobjectThe stored value of the cache.
absoluteExpirationDateTimeThe absolute expiration date time value from when the cached
valuebecomes invalid and is removed from the cache.nsstringThe optional namespace that provides a scope to the cache.
Returns
- bool
trueif insertion succeeded; otherwise,falsewhen there is already an entry in the cache with the same key.
Exceptions
- ArgumentNullException
keycannot 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
keystringThe unique identifier of the cache.
valueobjectThe stored value of the cache.
slidingExpirationTimeSpanThe sliding expiration time from when the cached
valuebecomes invalid and is removed from the cache.nsstringThe optional namespace that provides a scope to the cache.
Returns
- bool
trueif insertion succeeded; otherwise,falsewhen there is already an entry in the cache with the same key.
Exceptions
- ArgumentNullException
keycannot be null.
Contains(string, string)
Determines whether a cache entry exists in the cache.
public bool Contains(string key, string ns = null)
Parameters
keystringThe unique identifier for the cache entry.
nsstringThe optional namespace that provides a scope to the cache.
Returns
- bool
trueif the cache contains a cache entry whose key matcheskey; otherwise,false.
Exceptions
- ArgumentNullException
keycannot be null.
Count(string)
Gets the number of entries associated with the ns contained in the cache.
public int Count(string ns = null)
Parameters
nsstringThe optional namespace that provides a scope to the cache.
Returns
- int
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
keystringThe unique identifier for the cache entry.
nsstringThe 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
- ArgumentNullException
keycannot be null.
GetCacheEntry(string, string)
Returns an entry from the cache as a CacheEntry instance.
public CacheEntry GetCacheEntry(string key, string ns = null)
Parameters
keystringThe unique identifier for the cache entry.
nsstringThe 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
- ArgumentNullException
keycannot be null.
GetEnumerator()
Returns an enumerator that iterates through the collection.
public IEnumerator<KeyValuePair<long, CacheEntry>> GetEnumerator()
Returns
- IEnumerator<KeyValuePair<long, 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(bool) 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
keystringThe unique identifier for the cache entry.
nsstringThe 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
- ArgumentNullException
keycannot be null.
RemoveAll(string)
Removes all entries associated with the ns contained in the cache.
public void RemoveAll(string ns = null)
Parameters
nsstringThe 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
keystringThe unique identifier of the cache.
valueobjectThe stored value of the cache.
invalidationCacheInvalidationThe object that contains expiration details for a specific cache entry.
nsstringThe 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
- ArgumentNullException
keycannot be null -or-invalidationcannot 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
keystringThe unique identifier for the cache entry.
valueobjectWhen this method returns, contains the value associated with the specified
key, ornullif the operation failed.
Returns
- bool
trueif thevaluewas 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
keystringThe unique identifier for the cache entry.
nsstringThe optional namespace that provides a scope to the cache.
valueobjectWhen this method returns, contains the value associated with the specified
keyandns, ornullif the operation failed.
Returns
- bool
trueif thevaluewas found in the cache; otherwise,false.
Exceptions
- ArgumentNullException
keycannot 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
keystringThe unique identifier for the cache entry.
cacheEntryCacheEntryWhen this method returns, contains the cache entry associated with the specified
key, ornullif the operation failed.
Returns
- bool
trueif thecacheEntrywas 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
keystringThe unique identifier for the cache entry.
nsstringThe optional namespace that provides a scope to the cache.
cacheEntryCacheEntryWhen this method returns, contains the cache entry associated with the specified
keyandns, ornullif the operation failed.
Returns
- bool
trueif thecacheEntrywas found in the cache; otherwise,false.
Exceptions
- ArgumentNullException
keycannot be null.