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
TKeyThe type of the key used to identify different resources, which must be a non-nullable type.
TValueThe 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
keyTKeyThe resource key.
sourceResourceReferenceFactoryFunc<IResourceReference<TValue>>A factory that will create a reference to the source resource.
cacheKeystringThe cache key, which must be unique within the
cache.cacheIDistributedCacheThe cache to use to store resource values.
serializerIDistributedCacheSerializer<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.
timeProviderTimeProviderThe time provider.
Exceptions
- ArgumentNullException
keyis null.- ArgumentNullException
sourceResourceReferenceFactoryis null.- ArgumentNullException
cacheKeyis null.- ArgumentNullException
cacheis null.- ArgumentNullException
serializeris null.- ArgumentNullException
timeProvideris null.
Properties
CacheEntrySlidingExpiration
Gets the sliding expiration used for cache entries.
public TimeSpan CacheEntrySlidingExpiration { get; }
Property Value
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
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
Methods
Dispose(bool)
Closes and releases all resources used by the ReferencedResource<TKey, TValue>.
protected override void Dispose(bool disposing)
Parameters
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
GetValueAsync(CancellationToken)
Gets the resource value.
protected override Task<TValue> GetValueAsync(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationTokenA 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.