Class CacheService
ICacheService implementation backed by MemoryCache.
Implements
Inherited Members
Namespace: DataWeb.Cache.MemoryCache
Assembly: DataWeb.Cache.MemoryCache.dll
Syntax
public class CacheService : ICacheService
Constructors
CacheService()
Initializes a new instance of CacheService.
Declaration
public CacheService()
Properties
Name
Gets the name that identifies this cache provider.
Declaration
public string Name { get; }
Property Value
| Type | Description |
|---|---|
| string |
Methods
ClearAsync(CancellationToken)
Removes all entries from the cache.
Declaration
public Task ClearAsync(CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| CancellationToken | cancellationToken | Cancellation token. |
Returns
| Type | Description |
|---|---|
| Task |
GetAsync<T>(string, Func<Task<T>>, string[], IUser, DateTime?, CancellationToken)
Returns a cached value, invoking fetchDelegate on a cache miss to populate it.
Caching is bypassed for DataWeb users (IsDataWeb).
Declaration
public Task<T> GetAsync<T>(string key, Func<Task<T>> fetchDelegate, string[] dependencies = null, IUser user = null, DateTime? expiration = null, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| string | key | Unique cache key. |
| Func<Task<T>> | fetchDelegate | Function invoked to retrieve the value when not found in cache. |
| string[] | dependencies | Dependency keys that, when changed, cause this entry to be invalidated. |
| IUser | user | Current user. When the user is a DataWeb user, caching is skipped. |
| DateTime? | expiration | Absolute expiration date. Defaults to one year from now when null. |
| CancellationToken | cancellationToken | Cancellation token. |
Returns
| Type | Description |
|---|---|
| Task<T> | The value from cache or from the delegate. |
Type Parameters
| Name | Description |
|---|---|
| T | Type of the value to return. |
GetSectionCacheAsync<T>(string, Func<Task<T>>, string[], IUser, DateTime?, CancellationToken)
Returns a cached value using the specified sections as cache dependencies.
Each section name is mapped to the dependency key Dependency_{sectionName}.
Declaration
public Task<T> GetSectionCacheAsync<T>(string key, Func<Task<T>> fetchDelegate, string[] sections, IUser user = null, DateTime? expiration = null, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| string | key | Unique cache key. |
| Func<Task<T>> | fetchDelegate | Function invoked to retrieve the value when not found in cache. |
| string[] | sections | Section names to use as cache dependencies. |
| IUser | user | Current user. When the user is a DataWeb user, caching is skipped. |
| DateTime? | expiration | Absolute expiration date. Defaults to one year from now when null. |
| CancellationToken | cancellationToken | Cancellation token. |
Returns
| Type | Description |
|---|---|
| Task<T> | The value from cache or from the delegate. |
Type Parameters
| Name | Description |
|---|---|
| T | Type of the value to return. |
RemoveAsync(string, CancellationToken)
Removes the cache entry associated with the specified key.
Declaration
public Task RemoveAsync(string key, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| string | key | Key of the entry to remove. |
| CancellationToken | cancellationToken | Cancellation token. |
Returns
| Type | Description |
|---|---|
| Task |
SetAsync<T>(string, T, string[], DateTime?, CancellationToken)
Stores a value in the cache with the specified dependencies and expiration.
Declaration
public Task SetAsync<T>(string key, T value, string[] dependencies = null, DateTime? expiration = null, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| string | key | Unique cache key. |
| T | value | Value to store. When null the entry is not added. |
| string[] | dependencies | Dependency keys that, when changed, cause this entry to be invalidated. |
| DateTime? | expiration | Absolute expiration date. Defaults to one year from now when null. |
| CancellationToken | cancellationToken | Cancellation token. |
Returns
| Type | Description |
|---|---|
| Task |
Type Parameters
| Name | Description |
|---|---|
| T | Type of the value to store. |