Class FaultTolerantResourceProvider<TKey, TValue>
- Namespace
- Acuit.Pinpoint.ResourceManagement
- Assembly
- Acuit.Pinpoint.ResourceManagement.Abstractions.dll
A base class for implementing a resource provider that provides fault tolerance for other resource values via a cache. When a resource cannot be obtained from its source provider, the cached value is provided.
public abstract class FaultTolerantResourceProvider<TKey, TValue> : ResourceProvider<TKey, TValue, FaultTolerantReferencedResource<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
-
ResourceProvider<TKey, TValue, FaultTolerantReferencedResource<TKey, TValue>>FaultTolerantResourceProvider<TKey, TValue>
- Implements
- Inherited Members
Remarks
Implementations deriving from this class must do the following:
- Expose one or more public methods that call CreateResourceReference(TKey) to create references to the resources. Typically, a resource provider will implement an interface specific to the resource type that can be used via dependency injection.
- To provide error reporting when source resource values are unavailable, use UseLockedReferencedResources(Action<IReadOnlyCollection<TResource>>) to inspect each active resource's SourceGetValueError property. For example, this could be peformed during a periodic health check.
Note that any time a resource value is requested, it will always attempt to obtain the value from the underlying resource value provider first. Values from the distributed cache are only used when the source resource value cannot be obtained.
Constructors
FaultTolerantResourceProvider(Func<TKey, IResourceReference<TValue>>, IEqualityComparer<TKey>, Func<TKey, string>, IDistributedCache, IDistributedCacheSerializer<TValue>, TimeProvider)
Initializes a new instance of the FaultTolerantResourceProvider<TKey, TValue> class with a specified equality comparer to use when comparing keys.
protected FaultTolerantResourceProvider(Func<TKey, IResourceReference<TValue>> sourceResourceReferenceFactory, IEqualityComparer<TKey> keyComparer, Func<TKey, string> cacheKeyGenerator, IDistributedCache cache, IDistributedCacheSerializer<TValue> serializer, TimeProvider timeProvider)
Parameters
sourceResourceReferenceFactoryFunc<TKey, IResourceReference<TValue>>A factory that will create a reference to the source resource given the resource key.
keyComparerIEqualityComparer<TKey>The IEqualityComparer<T> implementation to use when comparing keys.
cacheKeyGeneratorFunc<TKey, string>A generator that will, given a resource key, produce a cache key for the resource, 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
sourceResourceReferenceFactoryis null.- ArgumentNullException
cacheKeyGeneratoris null.- ArgumentNullException
cacheis null.- ArgumentNullException
serializeris null.- ArgumentNullException
timeProvideris null.
FaultTolerantResourceProvider(Func<TKey, IResourceReference<TValue>>, IEqualityComparer<TKey>, string, IDistributedCache, IDistributedCacheSerializer<TValue>, TimeProvider)
Initializes a new instance of the FaultTolerantResourceProvider<TKey, TValue> class with a specified equality comparer to use when comparing keys.
protected FaultTolerantResourceProvider(Func<TKey, IResourceReference<TValue>> sourceResourceReferenceFactory, IEqualityComparer<TKey> keyComparer, string cacheKeyPrefix, IDistributedCache cache, IDistributedCacheSerializer<TValue> serializer, TimeProvider timeProvider)
Parameters
sourceResourceReferenceFactoryFunc<TKey, IResourceReference<TValue>>A factory that will create a reference to the source resource given the resource key.
keyComparerIEqualityComparer<TKey>The IEqualityComparer<T> implementation to use when comparing keys.
cacheKeyPrefixstringA prefix that will be used to generate cache keys by concatenating this prefix with the resource key converted to a string. Cache keys 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
sourceResourceReferenceFactoryis null.- ArgumentNullException
cacheKeyPrefixis null.- ArgumentNullException
cacheis null.- ArgumentNullException
serializeris null.- ArgumentNullException
timeProvideris null.
FaultTolerantResourceProvider(Func<TKey, IResourceReference<TValue>>, Func<TKey, string>, IDistributedCache, IDistributedCacheSerializer<TValue>, TimeProvider)
Initializes a new instance of the FaultTolerantResourceProvider<TKey, TValue> class that uses the default equality comparer for
TKey when comparing keys.
protected FaultTolerantResourceProvider(Func<TKey, IResourceReference<TValue>> sourceResourceReferenceFactory, Func<TKey, string> cacheKeyGenerator, IDistributedCache cache, IDistributedCacheSerializer<TValue> serializer, TimeProvider timeProvider)
Parameters
sourceResourceReferenceFactoryFunc<TKey, IResourceReference<TValue>>A factory that will create a reference to the source resource given the resource key.
cacheKeyGeneratorFunc<TKey, string>A generator that will, given a resource key, produce a cache key for the resource, 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
sourceResourceReferenceFactoryis null.- ArgumentNullException
cacheKeyGeneratoris null.- ArgumentNullException
cacheis null.- ArgumentNullException
serializeris null.- ArgumentNullException
timeProvideris null.
FaultTolerantResourceProvider(Func<TKey, IResourceReference<TValue>>, string, IDistributedCache, IDistributedCacheSerializer<TValue>, TimeProvider)
Initializes a new instance of the FaultTolerantResourceProvider<TKey, TValue> class that uses the default equality comparer for
TKey when comparing keys.
protected FaultTolerantResourceProvider(Func<TKey, IResourceReference<TValue>> sourceResourceReferenceFactory, string cacheKeyPrefix, IDistributedCache cache, IDistributedCacheSerializer<TValue> serializer, TimeProvider timeProvider)
Parameters
sourceResourceReferenceFactoryFunc<TKey, IResourceReference<TValue>>A factory that will create a reference to the source resource given the resource key.
cacheKeyPrefixstringA prefix that will be used to generate cache keys by concatenating this prefix with the resource key converted to a string. Cache keys 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
sourceResourceReferenceFactoryis null.- ArgumentNullException
cacheKeyPrefixis null.- ArgumentNullException
cacheis null.- ArgumentNullException
serializeris null.- ArgumentNullException
timeProvideris null.
Methods
CreateReferencedResource(TKey)
Creates a new referenced resource for a specified key.
protected override FaultTolerantReferencedResource<TKey, TValue> CreateReferencedResource(TKey key)
Parameters
keyTKeyThe resource key.
Returns
- FaultTolerantReferencedResource<TKey, TValue>
A new instance of type Acuit.Pinpoint.ResourceManagement.FaultTolerantReferencedResource{`0,`1} that represents the resource.
Exceptions
- ArgumentNullException
keyis null.