Table of Contents

Method ReadOrAdd

Namespace
Scaleout.Client
Assembly
Scaleout.Client.dll

ReadOrAdd(Func<TKey, 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 CacheResponse<TKey, TValue> ReadOrAdd(Func<TKey, ValueFactoryResult<TValue>> valueFactory, TKey key, ReadOptions options = default, CancellationToken cancellationToken = default)

Parameters

valueFactory Func<TKey, ValueFactoryResult<TValue>>

A factory that can be used to generate a value for the key. The callback 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 ReadOrAdd caller will receive a CacheResponse with a NotFound status.

key TKey

Identifier of the object in the cache.

options ReadOptions

Options that can be used when retrieving an object from the ScaleOut service.

cancellationToken CancellationToken

A token used to propagate notification that this operation should be canceled.

Returns

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:

ServerResultDescription
RetrievedThe object was successfully retrieved from the ScaleOut service.
AddedA 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.

ReadOrAdd(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 CacheResponse<TKey, TValue> ReadOrAdd(TKey key, Func<TValue> valueFactory, ReadOptions options = default, CancellationToken cancellationToken = default)

Parameters

key TKey

Identifier 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 ReadOrAdd caller will receive a CacheResponse with a NotFound status.

options ReadOptions

Options that can be used when retrieving an object from the ScaleOut service.

cancellationToken CancellationToken

A token used to propagate notification that this operation should be canceled.

Returns

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:

ServerResultDescription
RetrievedThe object was successfully retrieved from the ScaleOut service.
AddedA 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. The other ReadOrAdd overload accepts a valueFactory method that returns a ValueFactoryResult<TValue>, which allows the added object's policy and tags to be explicitly specified.

See Also