Table of Contents

Class DaprCache

Namespace
Arc4u.Caching.Dapr
Assembly
Arc4u.Caching.Dapr.dll

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

logger ILogger<DaprCache>

The logger.

options IOptionsMonitor<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

key string

The key of the value.

cancellation CancellationToken

A token to cancel the operation.

Returns

Task<TValue>

The value, or the default of TValue when the key does not exist.

Type Parameters

TValue

The 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

key string

The key of the value.

Returns

TValue

The value, or the default of TValue when the key does not exist.

Type Parameters

TValue

The 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

store string

The name of the cache, as declared in the configuration.

Exceptions

ArgumentException

store is 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

key string

The key of the value.

timeout TimeSpan

The time to live of the value.

value T

The value to save; it cannot be null.

isSlided bool

Must be false.

cancellation CancellationToken

A token to cancel the operation.

Returns

Task

A task that completes when the value is saved.

Type Parameters

T

The type of the value.

Exceptions

CacheNotInitializedException

The cache is not initialized.

ArgumentNullException

value is null.

NotSupportedException

isSlided is 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

key string

The key of the value.

value T

The value to save; it cannot be null.

cancellation CancellationToken

A token to cancel the operation.

Returns

Task

A task that completes when the value is saved.

Type Parameters

T

The type of the value.

Exceptions

CacheNotInitializedException

The cache is not initialized.

ArgumentNullException

value is 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

key string

The key of the value.

timeout TimeSpan

The time to live of the value.

value T

The value to save; it cannot be null.

isSlided bool

Must be false.

Type Parameters

T

The type of the value.

Exceptions

CacheNotInitializedException

The cache is not initialized.

ArgumentNullException

value is null.

NotSupportedException

isSlided is 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

key string

The key of the value.

value T

The value to save; it cannot be null.

Type Parameters

T

The type of the value.

Exceptions

CacheNotInitializedException

The cache is not initialized.

ArgumentNullException

value is 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

key string

The key of the value.

Returns

bool

true when the value was deleted; false when an error occurred.

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

key string

The key of the value.

cancellation CancellationToken

A token to cancel the operation.

Returns

Task<bool>

true when the value was deleted; false when an error occurred.

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

key string

The key of the value.

cancellation CancellationToken

A token to cancel the operation.

Returns

Task<TValue>

The value, or the default of TValue when an error occurred.

Type Parameters

TValue

The 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

key string

The key of the value.

value TValue

The value, or the default of TValue when it is not found or an error occurred.

Returns

bool

true when the key exists and a non-null value was read; otherwise false.

Type Parameters

TValue

The type of the value.

Exceptions

CacheNotInitializedException

The cache is not initialized.