Method ReadExclusiveOrAddAsync
ReadExclusiveOrAddAsync(TKey, Func<TValue>, ReadOptions, CancellationToken)
Reads an object from the cache, adding it if it does not already exist, and acquires an exclusive lock on the object if the operation results in a read. When adding, callers are synchronized by the ScaleOut service so that only the first caller performs the add operation.
public Task<CacheResponse<TKey, TValue>> ReadExclusiveOrAddAsync(TKey key, Func<TValue> valueFactory, ReadOptions options = default, CancellationToken cancellationToken = default)
Parameters
key
TKeyIdentifier of the object in the cache.
valueFactory
Func<TValue>The function used to generate a value for the key. The factory can return null or throw an ObjectNotFoundException if a value cannot be created (like in situations where a DB lookup is performed and no rows are found). The ReadExclusiveOrAddAsync caller will receive a CacheResponse with a NotFound status.
options
ReadOptionsOptions that can be used when retrieving an object from the ScaleOut service.
cancellationToken
CancellationTokenA token used to propagate notification that this operation should be canceled.
Returns
- Task<CacheResponse<TKey, TValue>>
A CacheResponse<TKey, TValue> containing the outcome of the operation, the lock token (if the object was read), and the retrieved/added object (if successful).
Remarks
The Result property of the response will contain one of the following ServerResult outcomes:
ServerResult | Description |
---|---|
Retrieved | The object was successfully retrieved and locked in the ScaleOut service. |
Added | A new object was successfully added in the ScaleOut service. |
NotFound |
The requested object was not found in the ScaleOut service,
and no object was added because the valueFactory returned null
(or it threw an ObjectNotFoundException).
|
The value returned by the factory will be stored in the ScaleOut service with the Cache's default policies. Other ReadExclusiveOrAddAsync overloads accept a factory method that returns a ValueFactoryResult<TValue>, which allows the added object's policy and tags to be explicitly specified.
If the operation resulted in a read, use the lock token on the response's LockToken property to perform subsequent operations under the lock.
Use the cancellationToken
parameter to control the amount of time the
caller should wait to acquire an exclusive lock.
- See Also
ReadExclusiveOrAddAsync(TKey, Func<TKey, ValueFactoryResult<TValue>>, ReadOptions, CancellationToken)
Reads an object from the cache, adding it if it does not already exist, and acquires an exclusive lock on the object if the operation results in a read. When adding, callers are synchronized by the ScaleOut service so that only the first caller performs the add operation.
public Task<CacheResponse<TKey, TValue>> ReadExclusiveOrAddAsync(TKey key, Func<TKey, ValueFactoryResult<TValue>> valueFactory, ReadOptions options = default, CancellationToken cancellationToken = default)
Parameters
key
TKeyIdentifier of the object in the cache.
valueFactory
Func<TKey, ValueFactoryResult<TValue>>The function used to generate a value for the key. The function must return a ValueFactoryResult<TValue> containing the value, CreatePolicy, and tags associated with the object to be added. The factory can return null or throw an ObjectNotFoundException if a value cannot be created (like in situations where a DB lookup is performed and no rows are found). The ReadExclusiveOrAddAsync caller will receive a CacheResult with a NotFound status.
options
ReadOptionsOptions that can be used when retrieving an object from the ScaleOut service.
cancellationToken
CancellationTokenA token used to propagate notification that this operation should be canceled.
Returns
- Task<CacheResponse<TKey, TValue>>
A CacheResponse<TKey, TValue> containing the outcome of the operation, the lock token (if the object was read), and the retrieved/added object (if successful).
Remarks
The Result property of the response will contain one of the following ServerResult outcomes:
ServerResult | Description |
---|---|
Retrieved | The object was successfully retrieved and locked in the ScaleOut service. |
Added | A new object was successfully added in the ScaleOut service. |
NotFound |
The requested object was not found in the ScaleOut service,
and no object was added because the valueFactory returned null
(or it threw an ObjectNotFoundException).
|
If the operation resulted in a read, use the lock token on the response's LockToken property to perform subsequent operations under the lock.
Use the cancellationToken
parameter to control the amount of time the
caller should wait to acquire the exclusive lock.
ReadExclusiveOrAddAsync(Func<TKey, Task<ValueFactoryResult<TValue>>>, TKey, ReadOptions, CancellationToken)
Reads an object from the cache, adding it if it does not already exist, and acquires an exclusive lock on the object if the operation results in a read. When adding, callers are synchronized by the ScaleOut service so that only the first caller performs the add operation.
public Task<CacheResponse<TKey, TValue>> ReadExclusiveOrAddAsync(Func<TKey, Task<ValueFactoryResult<TValue>>> asyncValueFactory, TKey key, ReadOptions options = default, CancellationToken cancellationToken = default)
Parameters
asyncValueFactory
Func<TKey, Task<ValueFactoryResult<TValue>>>An async function used to generate a value for the key. The async function must return a ValueFactoryResult<TValue> containing the value, CreatePolicy, and tags associated with the object to be added. The factory can return null or throw an ObjectNotFoundException if a value cannot be created (like in situations where a DB lookup is performed and no rows are found). The ReadExclusiveOrAddAsync caller will receive a CacheResult with a NotFound status.
key
TKeyIdentifier of the object in the cache.
options
ReadOptionsOptions that can be used when retrieving an object from the ScaleOut service.
cancellationToken
CancellationTokenA token used to propagate notification that this operation should be canceled.
Returns
- Task<CacheResponse<TKey, TValue>>
A CacheResponse<TKey, TValue> containing the outcome of the operation, the lock token (if the object was read), and the retrieved/added object (if successful).
Remarks
The Result property of the response will contain one of the following ServerResult outcomes:
ServerResult | Description |
---|---|
Retrieved | The object was successfully retrieved and locked in the ScaleOut service. |
Added | A new object was successfully added in the ScaleOut service. |
NotFound |
The requested object was not found in the ScaleOut service,
and no object was added because the asyncValueFactory returned null
(or it threw an ObjectNotFoundException).
|
If the operation resulted in a read, use the lock token on the response's LockToken property to perform subsequent operations under the lock.
Use the cancellationToken
parameter to control the amount of time the
caller should wait to acquire the exclusive lock.