Class ServiceCache<TKey, TCachedObject>
- Namespace
- Dynamicweb.Caching
- Assembly
- Dynamicweb.Caching.dll
Thread-safe cache
public abstract class ServiceCache<TKey, TCachedObject> : IDisposable where TKey : IEquatable<TKey>
Type Parameters
TKey- Unique key for cached object
TCachedObject- Cached object
- Inheritance
-
ServiceCache<TKey, TCachedObject>
- Implements
- Inherited Members
Properties
AllowFetchOnNotFound
protected bool AllowFetchOnNotFound { get; set; }
Property Value
AllowRefreshOnClear
protected bool AllowRefreshOnClear { get; set; }
Property Value
Count
protected virtual int Count { get; }
Property Value
CountInternal
protected abstract int CountInternal { get; }
Property Value
FetchOnNotFound
Lambda makes it possible to do lookup of missing cache values, meaning that values not in the cache will be retrieved with this lambda and added to the result and the cache
public virtual Func<IEnumerable<TKey>, IDictionary<TKey, TCachedObject>> FetchOnNotFound { get; set; }
Property Value
- Func<IEnumerable<TKey>, IDictionary<TKey, TCachedObject>>
Remarks
Useful when cache only contains a subset of all possible objects stored in the datababse
Info
Information about the object cache
public CacheInformation Info { get; protected set; }
Property Value
InitializeCache
Lambda makes it possible to initialize all objects in the cache
public virtual Func<IDictionary<TKey, TCachedObject>> InitializeCache { get; set; }
Property Value
- Func<IDictionary<TKey, TCachedObject>>
Remarks
Useful when cache is often used for ids that do no exist in the database
IsolationName
protected string IsolationName { get; set; }
Property Value
RefreshOnClear
Lambda makes it possible to override what happens when clear methods are being called
public virtual Func<IEnumerable<TKey>, IDictionary<TKey, TCachedObject>> RefreshOnClear { get; set; }
Property Value
- Func<IEnumerable<TKey>, IDictionary<TKey, TCachedObject>>
Remarks
Useful when using a fully initialized cache, to refresh the values instead of removing them
SupportsDependencyInvalidation
protected bool SupportsDependencyInvalidation { get; set; }
Property Value
Methods
ClearCache()
Resets all keys to the default value for objects stored in the object cache
public virtual void ClearCache()
ClearCache(TKey)
Resets the specified key to the default value for objects stored in the object cache
public virtual void ClearCache(TKey key)
Parameters
keyTKey- Key to reset
ClearCache(IEnumerable<TKey>)
Resets the specified keys to the default value for objects stored in the object cache
public virtual void ClearCache(IEnumerable<TKey> keys)
Parameters
keysIEnumerable<TKey>- Keys to reset
ClearCacheInternal()
protected abstract void ClearCacheInternal()
Dispose()
Disposes the cache
public void Dispose()
Dispose(bool)
protected virtual void Dispose(bool disposing)
Parameters
disposingbool
GetAllValues()
Gets all values stored in the object cache
public IEnumerable<TCachedObject> GetAllValues()
Returns
- IEnumerable<TCachedObject>
- values
GetAllValuesInternal()
protected abstract IEnumerable<TCachedObject> GetAllValuesInternal()
Returns
- IEnumerable<TCachedObject>
GetAlternativeCache<T>(string, T)
Gets the object that uniquely matches the provided alternative key, if the object is present in the cache.
public virtual TCachedObject GetAlternativeCache<T>(string name, T key) where T : IEquatable<T>
Parameters
namestring- Name of alternative cache
keyT- Alternative key
Returns
- TCachedObject
- cached object
Type Parameters
T- Alternative key type
GetAlternativeCache<T>(string, IEnumerable<T>)
Gets the objects that uniquely matches the provided alternative keys, if the objects are present in the cache.
public virtual IEnumerable<TCachedObject> GetAlternativeCache<T>(string name, IEnumerable<T> keys) where T : IEquatable<T>
Parameters
namestring- Name of alternative cache
keysIEnumerable<T>- Alternative keys
Returns
- IEnumerable<TCachedObject>
- cached objects
Type Parameters
T- Alternative key type
GetAlternativeCacheResult(string, IEnumerable<string>)
Gets a cacheresult that contains found objects and the keys that could not be found
public virtual CacheResult<string, TKey> GetAlternativeCacheResult(string name, IEnumerable<string> keys)
Parameters
namestring- Name of alternative cache
keysIEnumerable<string>- Alternative cachekeys
Returns
- CacheResult<string, TKey>
- cacheresult
GetCache(TKey)
Gets the object that uniquely matches the provided key, if the object is present in the cache.
public virtual TCachedObject GetCache(TKey key)
Parameters
keyTKey- Cachekey that is unique for the cached object
Returns
- TCachedObject
- cached object found
GetCache(IEnumerable<TKey>)
Gets the objects that uniquely matches the provides keys, if the objects are present in the cache.
public virtual IEnumerable<TCachedObject> GetCache(IEnumerable<TKey> keys)
Parameters
keysIEnumerable<TKey>- CacheKeys unique per cacheobject
Returns
- IEnumerable<TCachedObject>
- cached objects found
GetCacheKey(TKey)
protected virtual string GetCacheKey(TKey key)
Parameters
keyTKey
Returns
GetCacheResult(IEnumerable<TKey>)
Gets a cacheresult that contains found objects and the keys that could not be found
public abstract CacheResult<TKey, TCachedObject> GetCacheResult(IEnumerable<TKey> keys)
Parameters
keysIEnumerable<TKey>- Unique cachekeys
Returns
- CacheResult<TKey, TCachedObject>
- cacheresult
IsValid()
protected abstract bool IsValid()
Returns
RemoveFromCache(IEnumerable<TKey>)
protected abstract void RemoveFromCache(IEnumerable<TKey> keys)
Parameters
keysIEnumerable<TKey>
SetCache(TKey, TCachedObject)
Sets the object that should be stored in the object cache with the provided key
public virtual void SetCache(TKey key, TCachedObject cachedObject)
Parameters
keyTKey- Unique cachekey
cachedObjectTCachedObject- Object to cache
SetCacheInternal(TKey, TCachedObject)
protected abstract void SetCacheInternal(TKey key, TCachedObject cachedObject)
Parameters
keyTKeycachedObjectTCachedObject
SetCacheInternal(TKey, TCachedObject, TKey)
protected virtual void SetCacheInternal(TKey key, TCachedObject cachedObject, TKey parentKey)
Parameters
keyTKeycachedObjectTCachedObjectparentKeyTKey
SetupAlternativeCache(string, ServiceCache<string, TKey>, Func<TCachedObject, string>)
Adds a secondary sub-cache supporting alternative key-lookups
public virtual void SetupAlternativeCache(string name, ServiceCache<string, TKey> alternativeCache, Func<TCachedObject, string> getAlternativeKey)
Parameters
namestring- CacheName
alternativeCacheServiceCache<string, TKey>- Alternative cache
getAlternativeKeyFunc<TCachedObject, string>- Mapping from original cacheobject to new key
TrySwitchCache<TObjectCache>()
Makes a new cache of the specified type with the same configuration as the old cache.
public virtual ServiceCache<TKey, TCachedObject> TrySwitchCache<TObjectCache>() where TObjectCache : ServiceCache<TKey, TCachedObject>
Returns
- ServiceCache<TKey, TCachedObject>
- new object cache
Type Parameters
TObjectCache- New cache type