⚠ Deprecated: Legacy
CacheProvider is a generic class designed to provide caching functionality for any type of object. It packages a distributed Redis caching implementation into one Provider class, allowing for easy pairing with any RealProvider component following the ASP.NET Provider model.
$ dotnet add package rsalus.CacheProviderThe CacheProvider class is a generic class that implements the ICacheProvider<T> interface. It is designed to provide caching functionality for any type of object. The class uses two types of caches: MemoryCache and DistributedCache. It also uses the IRealProvider<T> interface to retrieve s from the real provider when they are not found in the cache. The type of cache to use is determined by the CacheType parameter passed to the CacheProvider constructor.
To instantiate a CacheProvider, you need to provide the following:
IRealProvider<T> interface. This is the "real provider" that the CacheProvider will use to retrieve s if they are not found in the cache.CacheType value to specify the type of cache to use.CacheSettings to configure the cache.ILogger for logging.IConnectionMultiplexer instance for connecting to a Redis server (only required if using CacheType.Distributed).Here's an example of how to instantiate a CacheProvider:
// Using the Dependency Injection pattern
var provider = _serviceProvider.GetService<IRealProvider<YourPayload>>();
var appsettings = _serviceProvider.GetService<IOptions<AppSettings>>();
var cachesettings = _serviceProvider.GetService<IOptions<CacheSettings>>().Value;
var logger = _serviceProvider.GetService<ILogger<YourClass>>();
var connection = _serviceProvider.GetService<IConnectionMultiplexer>() ?? null;
// Setup the CacheProvider
CacheProvider<YourPayload> cacheProvider;
CacheType cache = appsettings.Value.CacheType switch
{
"Local" => CacheType.Local,
"Distributed" => CacheType.Distributed,
_ => throw new ArgumentException("The CacheType is invalid.")
};
// Try to instantiate the cache provider
try
{
cacheProvider = new(provider, cache, cachesettings, logger, connection);
}
...
Note that you'll need to inject an IConnectionMultiplexer into your service collection if you plan on using DistributedCache. EX:
services.AddSingleton<IConnectionMultiplexer>(serviceProvider =>
{
return ConnectionMultiplexer.Connect(YourConnectionString);
});
MemoryCache is an in-memory caching implementation of IMemoryCache that uses a ConcurrentDictionary<string, (object value, DateTime timeStamp)> to store cached s. Each in the cache is associated with a timestamp, which is used in conjunction with the AbsoluteExpiration setting to manage cache invalidation. This class is designed as a singleton to prevent multiple instances from being created per process. It provides synchronous operations for retrieving, adding, and removing s from the cache.
DistributedCache is an implementation of IDistributedCache that uses the StackExchange.Redis library as its foundation. It leverages the Polly library to handle exceptions and retries, making it robust and resilient. This implementation is compatible with various distributed cache providers, including Redis, AWS ElastiCache, and Azure Blob Storage.
The DistributedCache class requires a CacheSettings object for configuration and an IConnectionMultiplexer for connecting to the cache provider. It also uses an AsyncPolicyWrap<object> to define a policy for handling exceptions when accessing the cache. This policy includes a retry policy, which retries a specified number of times with a delay, and a fallback policy, which executes a fallback action if an exception is thrown. The class provides asynchronous operations for retrieving, adding, and removing s from the cache.
Once you have an instance of the CacheProvider class, you can use the CheckCacheAsync or CheckCache method to check the cache for an with a specified key. If the is found in the cache, it is returned. If not, the is retrieved from the real provider and then cached before being returned. Here is an example:
object = new();
var key = "myKey";
var cached = await cacheProvider.CheckCacheAsync(, key);
The project uses the following NuGet packages:
Cache-Provider is licensed under the MIT License.