Table of Contents

Method ReadExclusiveOrAdd

Namespace
Scaleout.Client
Assembly
Scaleout.Client.dll

ReadExclusiveOrAdd(Func<TKey, 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 CacheResponse<TKey, TValue> ReadExclusiveOrAdd(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 ReadExclusiveOrAdd 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, 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:

ServerResultDescription
RetrievedThe object was successfully retrieved and locked in 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).

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.

ReadExclusiveOrAdd(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 CacheResponse<TKey, TValue> ReadExclusiveOrAdd(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 ReadExclusiveOrAdd 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, 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:

ServerResultDescription
RetrievedThe object was successfully retrieved and locked in 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).

The value returned by the factory will be stored in the ScaleOut service with the Cache's default policies. Other ReadExclusiveOrAdd 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