Class DaprCache
An ICache backed by a Dapr state store, resolved with the kind Dapr. The values are serialized by the Dapr client (not by an
IObjectSerialization). The name of the state store comes from the DaprCacheOption named after the store.
[Export("Dapr", typeof(ICache))]
public sealed class DaprCache : ICache, IDisposable
- Inheritance
-
DaprCache
- Implements
- Inherited Members
Remarks
The Dapr client serializes with JsonSerializerOptions, or with its own defaults, which use reflection, when it is not set.
In a trimmed or native AOT application, set it with a JsonSerializerContext that declares the types stored in the cache.
Constructors
DaprCache(ILogger<DaprCache>, IOptionsMonitor<DaprCacheOption>)
Initializes a new instance of the DaprCache class.
public DaprCache(ILogger<DaprCache> logger, IOptionsMonitor<DaprCacheOption> options)
Parameters
loggerILogger<DaprCache>The logger.
optionsIOptionsMonitor<DaprCacheOption>The named options of the Dapr caches.
Methods
Dispose()
Disposes the Dapr client.
public void Dispose()
GetAsync<TValue>(string, CancellationToken)
Asynchronously gets the value stored in the state store for the key.
public Task<TValue?> GetAsync<TValue>(string key, CancellationToken cancellation = default)
Parameters
keystringThe key of the value.
cancellationCancellationTokenA token to cancel the operation.
Returns
- Task<TValue>
The value, or the default of
TValuewhen the key does not exist.
Type Parameters
TValueThe type of the value.
Exceptions
- CacheNotInitializedException
The cache is not initialized.
- DataCacheException
The Dapr client or the deserialization failed; the original exception is the inner exception.
Get<TValue>(string)
Gets the value stored in the state store for the key.
public TValue? Get<TValue>(string key)
Parameters
keystringThe key of the value.
Returns
- TValue
The value, or the default of
TValuewhen the key does not exist.
Type Parameters
TValueThe type of the value.
Exceptions
- CacheNotInitializedException
The cache is not initialized.
- DataCacheException
The Dapr client or the deserialization failed; the original exception is the inner exception.
Initialize(string)
Creates the Dapr client and reads the name of the state store from the DaprCacheOption named store. Calling it again on an initialized cache only logs a warning.
public void Initialize(string store)
Parameters
storestringThe name of the cache, as declared in the configuration.
Exceptions
- ArgumentException
storeis empty.
PutAsync<T>(string, TimeSpan, T, bool, CancellationToken)
Asynchronously saves the value in the state store with a time to live (the ttlInSeconds metadata). A sliding expiration is not supported.
public Task PutAsync<T>(string key, TimeSpan timeout, T value, bool isSlided = false, CancellationToken cancellation = default)
Parameters
keystringThe key of the value.
timeoutTimeSpanThe time to live of the value.
valueTThe value to save; it cannot be null.
isSlidedboolMust be false.
cancellationCancellationTokenA token to cancel the operation.
Returns
- Task
A task that completes when the value is saved.
Type Parameters
TThe type of the value.
Exceptions
- CacheNotInitializedException
The cache is not initialized.
- ArgumentNullException
valueis null.- NotSupportedException
isSlidedis true.- DataCacheException
The Dapr client failed; the original exception is the inner exception.
PutAsync<T>(string, T, CancellationToken)
Asynchronously saves the value in the state store, without expiration.
public Task PutAsync<T>(string key, T value, CancellationToken cancellation = default)
Parameters
keystringThe key of the value.
valueTThe value to save; it cannot be null.
cancellationCancellationTokenA token to cancel the operation.
Returns
- Task
A task that completes when the value is saved.
Type Parameters
TThe type of the value.
Exceptions
- CacheNotInitializedException
The cache is not initialized.
- ArgumentNullException
valueis null.- DataCacheException
The Dapr client failed; the original exception is the inner exception.
Put<T>(string, TimeSpan, T, bool)
Saves the value in the state store with a time to live (the ttlInSeconds metadata). A sliding expiration is not supported.
public void Put<T>(string key, TimeSpan timeout, T value, bool isSlided = false)
Parameters
keystringThe key of the value.
timeoutTimeSpanThe time to live of the value.
valueTThe value to save; it cannot be null.
isSlidedboolMust be false.
Type Parameters
TThe type of the value.
Exceptions
- CacheNotInitializedException
The cache is not initialized.
- ArgumentNullException
valueis null.- NotSupportedException
isSlidedis true.- DataCacheException
The Dapr client failed; the original exception is the inner exception.
Put<T>(string, T)
Saves the value in the state store, without expiration.
public void Put<T>(string key, T value)
Parameters
Type Parameters
TThe type of the value.
Exceptions
- CacheNotInitializedException
The cache is not initialized.
- ArgumentNullException
valueis null.- DataCacheException
The Dapr client failed; the original exception is the inner exception.
Remove(string)
Deletes the value from the state store. Errors are logged and reported by the return value.
public bool Remove(string key)
Parameters
keystringThe key of the value.
Returns
Exceptions
- CacheNotInitializedException
The cache is not initialized.
RemoveAsync(string, CancellationToken)
Asynchronously deletes the value from the state store. Errors are logged and reported by the return value.
public Task<bool> RemoveAsync(string key, CancellationToken cancellation = default)
Parameters
keystringThe key of the value.
cancellationCancellationTokenA token to cancel the operation.
Returns
Exceptions
- CacheNotInitializedException
The cache is not initialized.
ToString()
Returns the name of the Dapr state store.
public override string ToString()
Returns
- string
The name of the state store (empty before initialization).
TryGetValueAsync<TValue>(string, CancellationToken)
Asynchronously tries to read the value for the key. Errors raised while reading are swallowed.
public Task<TValue?> TryGetValueAsync<TValue>(string key, CancellationToken cancellation = default)
Parameters
keystringThe key of the value.
cancellationCancellationTokenA token to cancel the operation.
Returns
- Task<TValue>
The value, or the default of
TValuewhen an error occurred.
Type Parameters
TValueThe type of the value.
Exceptions
- CacheNotInitializedException
The cache is not initialized.
TryGetValue<TValue>(string, out TValue?)
Tries to read the value for the key. Errors raised while reading are swallowed.
public bool TryGetValue<TValue>(string key, out TValue? value)
Parameters
keystringThe key of the value.
valueTValueThe value, or the default of
TValuewhen it is not found or an error occurred.
Returns
Type Parameters
TValueThe type of the value.
Exceptions
- CacheNotInitializedException
The cache is not initialized.