Method ReadOrAddAsync
ReadOrAddAsync(TKey, Func<TValue>, ReadOptions, CancellationToken)
Reads an object from the cache, adding it if it does not already exist. When adding, callers are synchronized by the ScaleOut service so that only the first caller performs the add operation.
public Task<CacheResponse<TKey, TValue>> ReadOrAddAsync(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 ReadOrAddAsync 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 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 from 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).
|
RetrievedStale | If GeoServer Pull replication is used but the WAN link between datacenters is down, RetrievedStale is returned to indicate that the proxy returned from the local ScaleOut service may be stale. |
The value returned by the factory will be stored in the ScaleOut service with the Cache's
default policies.
Other ReadOrAddAsync overloads accept a valueFactory
method that returns
a ValueFactoryResult<TValue>, which allows the added object's policy and tags
to be explicitly specified.
- See Also
ReadOrAddAsync(TKey, Func<TKey, ValueFactoryResult<TValue>>, ReadOptions, CancellationToken)
Reads an object from the cache, adding it if it does not already exist. When adding, callers are synchronized by the ScaleOut service so that only the first caller performs the add operation.
public Task<CacheResponse<TKey, TValue>> ReadOrAddAsync(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 ReadOrAddAsync 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 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 from 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).
|
RetrievedStale | If GeoServer Pull replication is used but the WAN link between datacenters is down, RetrievedStale is returned to indicate that the proxy returned from the local ScaleOut service may be stale. |
ReadOrAddAsync(Func<TKey, Task<ValueFactoryResult<TValue>>>, TKey, ReadOptions, CancellationToken)
Reads an object from the cache, adding it if it does not already exist. When adding, callers are synchronized by the ScaleOut service so that only the first performs the add operation.
public Task<CacheResponse<TKey, TValue>> ReadOrAddAsync(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 ReadOrAddAsync caller will receive a CacheResponse 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 and the retrieved 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 from 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).
|
RetrievedStale | If GeoServer Pull replication is used but the WAN link between datacenters is down, RetrievedStale is returned to indicate that the proxy returned from the local ScaleOut service may be stale. |