Table of Contents

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

TKey

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

TValue

The resource value type.

Inheritance
FaultTolerantResourceProvider<TKey, TValue>
Implements
Inherited Members

Remarks

Implementations deriving from this class must do the following:

  1. 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.
  2. 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

sourceResourceReferenceFactory Func<TKey, IResourceReference<TValue>>

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

keyComparer IEqualityComparer<TKey>

The IEqualityComparer<T> implementation to use when comparing keys.

cacheKeyGenerator Func<TKey, string>

A generator that will, given a resource key, produce a cache key for the resource, 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

sourceResourceReferenceFactory is null.

ArgumentNullException

cacheKeyGenerator is null.

ArgumentNullException

cache is null.

ArgumentNullException

serializer is null.

ArgumentNullException

timeProvider is 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

sourceResourceReferenceFactory Func<TKey, IResourceReference<TValue>>

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

keyComparer IEqualityComparer<TKey>

The IEqualityComparer<T> implementation to use when comparing keys.

cacheKeyPrefix string

A 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.

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

sourceResourceReferenceFactory is null.

ArgumentNullException

cacheKeyPrefix is null.

ArgumentNullException

cache is null.

ArgumentNullException

serializer is null.

ArgumentNullException

timeProvider is 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

sourceResourceReferenceFactory Func<TKey, IResourceReference<TValue>>

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

cacheKeyGenerator Func<TKey, string>

A generator that will, given a resource key, produce a cache key for the resource, 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

sourceResourceReferenceFactory is null.

ArgumentNullException

cacheKeyGenerator is null.

ArgumentNullException

cache is null.

ArgumentNullException

serializer is null.

ArgumentNullException

timeProvider is 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

sourceResourceReferenceFactory Func<TKey, IResourceReference<TValue>>

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

cacheKeyPrefix string

A 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.

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

sourceResourceReferenceFactory is null.

ArgumentNullException

cacheKeyPrefix is null.

ArgumentNullException

cache is null.

ArgumentNullException

serializer is null.

ArgumentNullException

timeProvider is null.

Methods

CreateReferencedResource(TKey)

Creates a new referenced resource for a specified key.

protected override FaultTolerantReferencedResource<TKey, TValue> CreateReferencedResource(TKey key)

Parameters

key TKey

The resource key.

Returns

FaultTolerantReferencedResource<TKey, TValue>

A new instance of type Acuit.Pinpoint.ResourceManagement.FaultTolerantReferencedResource{`0,`1} that represents the resource.

Exceptions

ArgumentNullException

key is null.