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
configIOptions<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
keystringThe key of the value.
cancellationCancellationTokenA token to cancel the operation.
Returns
- Task<TValue>
The value, or the default of
TValuewhen the file does not exist.
Type Parameters
TValueThe type of the value.
Exceptions
- KeyNotFoundException
The file contains a
nullvalue.
Get<TValue>(string)
Gets the value stored under a key.
public TValue Get<TValue>(string key)
Parameters
keystringThe key of the value.
Returns
- TValue
The value, or the default of
TValuewhen the file does not exist.
Type Parameters
TValueThe type of the value.
Exceptions
- KeyNotFoundException
The file contains a
nullvalue.
Initialize(string)
Does nothing.
public void Initialize(string store)
Parameters
storestringThe 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
keystringThe key of the value.
timeoutTimeSpanThe expiration time.
valueTThe value.
isSlidedboolThe sliding expiration flag.
cancellationCancellationTokenA token to cancel the operation.
Returns
- Task
Never returns.
Type Parameters
TThe 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
keystringThe key used to identify the value in the cache.
valueTThe value to save; it cannot be null. The default of a value type (
0, false) is saved.cancellationCancellationTokenA CancellationToken to cancel the operation.
Returns
- Task
A task that completes when the value is saved.
Type Parameters
TThe type of the value.
Exceptions
- ArgumentNullException
valueis 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
keystringThe key of the value.
timeoutTimeSpanThe expiration time.
valueTThe value.
isSlidedboolThe sliding expiration flag.
Type Parameters
TThe 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
keystringThe key used to identify the value in the cache.
valueTThe value to save; it cannot be null. The default of a value type (
0, false) is saved.
Type Parameters
TThe type of the value.
Exceptions
- ArgumentNullException
valueis 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
keystringThe key used to identify the value in the cache.
Returns
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
keystringThe key used to identify the value in the cache.
cancellationCancellationTokenA CancellationToken to cancel the operation.
Returns
Exceptions
- CacheNotInitializedException
The cache is not initialized.
TryGetValueAsync<TValue>(string, CancellationToken)
Not supported.
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>
Never returns.
Type Parameters
TValueThe 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
keystringThe key used to identify the value in the cache.
valueTValueThe value, or the default of
TValuewhen it is not found or could not be read.
Returns
Type Parameters
TValueThe type of the value.
Exceptions
- CacheNotInitializedException
The cache is not initialized.