Table of Contents

Class FaultTolerantReferencedResource<TKey, TValue>

Namespace
Acuit.Pinpoint.ResourceManagement
Assembly
Acuit.Pinpoint.ResourceManagement.Abstractions.dll

A wrapper class for another IReferencedResource<TKey, TValue> that provides fault tolerance for resource values via a cache. When a resource cannot be obtained from its source provider, the cached value is provided.

public sealed class FaultTolerantReferencedResource<TKey, TValue> : ReferencedResource<TKey, TValue>, IReferencedResource<TKey, TValue>, IDisposable where TKey : notnull

Type Parameters

TKey

The type of the key used to identify different resources, which must be a non-nullable type.

TValue

The resource value type.

Inheritance
ReferencedResource<TKey, TValue>
FaultTolerantReferencedResource<TKey, TValue>
Implements
IReferencedResource<TKey, TValue>
Inherited Members

Remarks

Any unhandled errors getting or setting cache entries will cause background processing in this resource provider to fail, with the error reported via a faulted ProcessingLoopTask.

This class is normally not used directly; instead use FaultTolerantResourceProvider<TKey, TValue> to create the resource provider.

Constructors

FaultTolerantReferencedResource(TKey, Func<IResourceReference<TValue>>, string, IDistributedCache, IDistributedCacheSerializer<TValue>, TimeProvider)

Initializes a new instance of the FaultTolerantReferencedResource<TKey, TValue> class.

public FaultTolerantReferencedResource(TKey key, Func<IResourceReference<TValue>> sourceResourceReferenceFactory, string cacheKey, IDistributedCache cache, IDistributedCacheSerializer<TValue> serializer, TimeProvider timeProvider)

Parameters

key TKey

The resource key.

sourceResourceReferenceFactory Func<IResourceReference<TValue>>

A factory that will create a reference to the source resource.

cacheKey string

The cache key, which must be unique within the cache.

cache IDistributedCache

The cache to use to store resource values.

serializer IDistributedCacheSerializer<TValue>

The serializer to use to serialize cached values. GetDistributedCacheSerializer<T>(IServiceProvider) or TryGetDistributedCacheSerializer<T>(IServiceProvider, out IDistributedCacheSerializer<T>?) can be used to obtain a serializer from a service provider.

timeProvider TimeProvider

The time provider.

Exceptions

ArgumentNullException

key is null.

ArgumentNullException

sourceResourceReferenceFactory is null.

ArgumentNullException

cacheKey is null.

ArgumentNullException

cache is null.

ArgumentNullException

serializer is null.

ArgumentNullException

timeProvider is null.

Properties

CacheEntrySlidingExpiration

Gets the sliding expiration used for cache entries.

public TimeSpan CacheEntrySlidingExpiration { get; }

Property Value

TimeSpan

Remarks

This needs to be well longer than the expected application startup time; the cache entry will be automatically refreshed every half this time as long as there are any active resource references.

ProcessingLoopTask

Gets the Task representing the asynchronous background update processing.

public Task ProcessingLoopTask { get; }

Property Value

Task

Remarks

This can be observed to handle unexpected errors in the asynchronous background update processing. Normally, this task will run to completion when this FaultTolerantReferencedResource<TKey, TValue> object is disposed (which signals update processing to stop, but without waiting for it to actually stop). If an unexpected error occurs in the asynchronous background update processing, update processing will stop, and this task will complete with an exception.

SourceGetValueError

Gets the exception that occurred when last attempting to retrieve the source resource value, or null if there was no error.

public Exception? SourceGetValueError { get; }

Property Value

Exception

Methods

Dispose(bool)

Closes and releases all resources used by the ReferencedResource<TKey, TValue>.

protected override void Dispose(bool disposing)

Parameters

disposing bool

true when this is in response to a call to Dispose().

Remarks

Derived classes should override this when they have any resources that should be disposed.

GetChangeToken()

Returns a IChangeToken that can be used to observe when this resource value changes.

protected override IChangeToken GetChangeToken()

Returns

IChangeToken

A IChangeToken.

GetValueAsync(CancellationToken)

Gets the resource value.

protected override Task<TValue> GetValueAsync(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

A cancellation token that can be used to request canceling retrieving the resource.

Returns

Task<TValue>

The task object representing the asynchronous operation.

Remarks

Once the operation completes, the Result property on the returned task object contains the resource value.

This method will be called on behalf of resource references, ensuring it is never called more than once at a time. The returned value will be cached and returned to all references until the change token (retrieved via GetChangeToken() before calling GetValueAsync(CancellationToken)) signals a change.

If this method throws an exception, it will be propagated to resource references (via their call to GetValueAsync(CancellationToken)), and subsequent calls to GetValueAsync(CancellationToken) by resource references will cause this method to be called again.

Exceptions

Exception

The resource could not be retrieved. Specific exceptions depend on the implementation.