rsalus.CacheProvider
1.1.1
See the version list below for details.
dotnet add package rsalus.CacheProvider --version 1.1.1
NuGet\Install-Package rsalus.CacheProvider -Version 1.1.1
<PackageReference Include="rsalus.CacheProvider" Version="1.1.1" />
paket add rsalus.CacheProvider --version 1.1.1
#r "nuget: rsalus.CacheProvider, 1.1.1"
// Install rsalus.CacheProvider as a Cake Addin
#addin nuget:?package=rsalus.CacheProvider&version=1.1.1
// Install rsalus.CacheProvider as a Cake Tool
#tool nuget:?package=rsalus.CacheProvider&version=1.1.1
CacheProvider
The 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.
How to Instantiate CacheProvider
To instantiate a CacheProvider
, you need to provide the following:
- An instance of
IConnectionMultiplexer
for connecting to the Redis server. - An instance of a class that implements the
IRealProvider<T>
interface. This is the "real provider" that theCacheProvider
will use to retrieve s if they are not found in the cache. - An instance of
CacheSettings
to configure the cache. - An instance of
ILogger
for logging.
Here's an example of how to instantiate a CacheProvider
:
// Get configuration
var provider = _serviceProvider.GetRequiredService<IRealProvider<Payload>>();
var settings = _serviceProvider.GetRequiredService<IOptions<CacheSettings>>().Value;
var logger = _serviceProvider.GetRequiredService<ILogger<MockCachingOperation>>();
var connection = _serviceProvider.GetRequiredService<IConnectionMultiplexer>();
// Create the cache provider
CacheProvider<Payload> cacheProvider = new(connection, provider, settings, logger);
...
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);
});
Cache Implementations
DistributedCache
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.
Usage
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);
Prerequisities
- .NET 8.0 SDK
Dependencies
The project uses the following NuGet packages:
- Polly ~> 8.3.0
- StackExchange.Redis ~> 2.7.23
- Microsoft.EntityFrameworkCore ~> 8.0.2
- Microsoft.Extensions.Caching.StackExchangeRedis ~> 8.0.2
- Microsoft.Extensions.DependencyInjection ~> 8.0.0
- Microsoft.Extensions.Options ~> 8.0.2
License
Cache-Provider is licensed under the MIT License.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- Microsoft.EntityFrameworkCore (>= 8.0.2)
- Microsoft.Extensions.Caching.StackExchangeRedis (>= 8.0.2)
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
- Microsoft.Extensions.Options (>= 8.0.2)
- Polly (>= 8.3.0)
- StackExchange.Redis (>= 2.7.23)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.