Table of Contents

Class CacheTokens

Namespace
Arc4u.OAuth2.Client.Authentication.Cache
Assembly
Arc4u.OAuth2.Client.Authentication.dll

An ISecureCache that persists each value as a JSON file in the OAuth2 folder of the local application data of the user. The file name is built from the logging name and the environment name of the application configuration and the key. The expiration arguments are not supported.

[Export(typeof(ISecureCache))]
[Shared]
public sealed class CacheTokens : ISecureCache, ICache, IDisposable
Inheritance
CacheTokens
Implements
Inherited Members

Remarks

The values stored by Arc4u (TokenRefreshInfo, TokenInfo, a list of ClaimDto and string) are serialized with source-generated metadata. Other types are serialized with reflection, which is only available when IsReflectionEnabledByDefault is true; it is not in a Native AOT application, where they throw a NotSupportedException.

Constructors

CacheTokens(IOptions<ApplicationConfig>)

Initializes a new instance of the CacheTokens class and creates the cache folder when it does not exist.

public CacheTokens(IOptions<ApplicationConfig> config)

Parameters

config IOptions<ApplicationConfig>

The application configuration, giving the logging name and the environment name.

Methods

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

GetAsync<TValue>(string, CancellationToken)

Gets the value stored under a 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 file does not exist.

Type Parameters

TValue

The type of the value.

Exceptions

KeyNotFoundException

The file contains a null value.

Get<TValue>(string)

Gets the value stored under a 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 file does not exist.

Type Parameters

TValue

The type of the value.

Exceptions

KeyNotFoundException

The file contains a null value.

Initialize(string)

Does nothing.

public void Initialize(string store)

Parameters

store string

The store name, which is ignored.

PutAsync<T>(string, TimeSpan, T, bool, CancellationToken)

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 expiration time.

value T

The value.

isSlided bool

The sliding expiration flag.

cancellation CancellationToken

A token to cancel the operation.

Returns

Task

Never returns.

Type Parameters

T

The type of the value.

Exceptions

NotImplementedException

Always thrown.

PutAsync<T>(string, T, CancellationToken)

Asynchronously adds or replaces a value in the cache, without a specific expiration.

public Task PutAsync<T>(string key, T value, CancellationToken cancellation = default)

Parameters

key string

The key used to identify the value in the cache.

value T

The value to save; it cannot be null. The default of a value type (0, false) is saved.

cancellation CancellationToken

A CancellationToken to cancel the operation.

Returns

Task

A task that completes when the value is saved.

Type Parameters

T

The type of the value.

Exceptions

ArgumentNullException

value is null.

CacheNotInitializedException

The cache is not initialized.

DataCacheException

The cache engine or the serialization failed (the original exception is the inner exception), or the memory cache is full.

Put<T>(string, TimeSpan, T, bool)

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 expiration time.

value T

The value.

isSlided bool

The sliding expiration flag.

Type Parameters

T

The type of the value.

Exceptions

NotImplementedException

Always thrown.

Put<T>(string, T)

Adds or replaces a value in the cache, without a specific expiration.

public void Put<T>(string key, T value)

Parameters

key string

The key used to identify the value in the cache.

value T

The value to save; it cannot be null. The default of a value type (0, false) is saved.

Type Parameters

T

The type of the value.

Exceptions

ArgumentNullException

value is null.

CacheNotInitializedException

The cache is not initialized.

DataCacheException

The cache engine or the serialization failed (the original exception is the inner exception), or the memory cache is full.

Remove(string)

Removes the value stored for the key.

public bool Remove(string key)

Parameters

key string

The key used to identify the value in the cache.

Returns

bool

true when the operation succeeded; false when the underlying cache raised an error.

Exceptions

CacheNotInitializedException

The cache is not initialized.

RemoveAsync(string, CancellationToken)

Asynchronously removes the value stored for the key.

public Task<bool> RemoveAsync(string key, CancellationToken cancellation = default)

Parameters

key string

The key used to identify the value in the cache.

cancellation CancellationToken

A CancellationToken to cancel the operation.

Returns

Task<bool>

true when the operation succeeded; false when the underlying cache raised an error.

Exceptions

CacheNotInitializedException

The cache is not initialized.

TryGetValueAsync<TValue>(string, CancellationToken)

Not supported.

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>

Never returns.

Type Parameters

TValue

The type of the value.

Exceptions

NotImplementedException

Always thrown.

TryGetValue<TValue>(string, out TValue)

Tries to get the value stored for the key. Errors raised while reading or deserializing are not thrown.

public bool TryGetValue<TValue>(string key, out TValue value)

Parameters

key string

The key used to identify the value in the cache.

value TValue

The value, or the default of TValue when it is not found or could not be read.

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.