Interface ICache
The interface defines the contract implemented by a cache.
public interface ICache : IDisposable
- Inherited Members
Methods
GetAsync<TValue>(string, CancellationToken)
Asynchronously gets the value stored for the key.
Task<TValue?> GetAsync<TValue>(string key, CancellationToken cancellation = default)
Parameters
keystringThe key used to identify the value in the cache.
cancellationCancellationTokenA CancellationToken to cancel the operation.
Returns
- Task<TValue>
The value, or the default of
TValuewhen no value exists for the key.
Type Parameters
TValueThe type of the value.
Exceptions
- CacheNotInitializedException
The cache is not initialized.
- DataCacheException
The cache engine or the deserialization failed; the original exception is the inner exception.
Get<TValue>(string)
Gets the value stored for the key.
TValue? Get<TValue>(string key)
Parameters
keystringThe key used to identify the value in the cache.
Returns
- TValue
The value, or the default of
TValuewhen no value exists for the key.
Type Parameters
TValueThe type of the value.
Exceptions
- CacheNotInitializedException
The cache is not initialized.
- DataCacheException
The cache engine or the deserialization failed; the original exception is the inner exception.
Initialize(string)
Initializes the cache. This must be called once every time a cache is created.
void Initialize(string store)
Parameters
storestringThe name of the cache, as declared in the configuration; it identifies the options and the store used.
PutAsync<T>(string, TimeSpan, T, bool, CancellationToken)
Asynchronously adds or replaces a value in the cache with an expiration.
Task PutAsync<T>(string key, TimeSpan timeout, T value, bool isSlided = false, CancellationToken cancellation = default)
Parameters
keystringThe key used to identify the value in the cache.
timeoutTimeSpanThe period of validity of the value. When it expires, the value is removed from the cache.
valueTThe value to save; it cannot be null. The default of a value type (
0, false) is saved.isSlidedbooltrue to restart the period each time the value is read (sliding expiration); false for an absolute expiration. Not every cache supports a sliding expiration (the Dapr cache throws NotSupportedException).
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.
- NotSupportedException
isSlidedis true and the cache does not support it.
PutAsync<T>(string, T, CancellationToken)
Asynchronously adds or replaces a value in the cache, without a specific expiration.
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)
Adds or replaces a value in the cache with an expiration.
void Put<T>(string key, TimeSpan timeout, T value, bool isSlided = false)
Parameters
keystringThe key used to identify the value in the cache.
timeoutTimeSpanThe period of validity of the value. When it expires, the value is removed from the cache.
valueTThe value to save; it cannot be null. The default of a value type (
0, false) is saved.isSlidedbooltrue to restart the period each time the value is read (sliding expiration); false for an absolute expiration. Not every cache supports a sliding expiration (the Dapr cache throws NotSupportedException).
Type Parameters
TThe type of the value.
Exceptions
- ArgumentNullException
valueis null.- CacheNotInitializedException
The cache is not initialized.
- NotSupportedException
isSlidedis true and the cache does not support it.
Put<T>(string, T)
Adds or replaces a value in the cache, without a specific expiration.
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.
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.
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)
Asynchronously tries to get the value stored for the key. Errors raised while reading or deserializing are not thrown.
Task<TValue?> TryGetValueAsync<TValue>(string key, CancellationToken cancellation = default)
Parameters
keystringThe key used to identify the value in the cache.
cancellationCancellationTokenA CancellationToken to cancel the operation.
Returns
- Task<TValue>
The value, or the default of
TValuewhen it is not found or could not be read.
Type Parameters
TValueThe type of the value.
Exceptions
- CacheNotInitializedException
The cache is not initialized.
TryGetValue<TValue>(string, out TValue?)
Tries to get the value stored for the key. Errors raised while reading or deserializing are not thrown.
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.