IdempotentAPI requires an implementation of IIdempotencyCache to store data. You can choose between two main implementations:
Choice 1: DistributedCache (Default)
Uses IDistributedCache as the primary storage. This is suitable for most standard scenarios.
Choice 2: FusionCache
Provides advanced features like 2nd-level caching (using IDistributedCache as a secondary layer), Fail-Safe, and Soft/Hard timeouts. To use 2nd-level cache, you must also register a FusionCache Serialization provider (e.g., NewtonsoftJson or SystemTextJson).
// Register an implementation of the IDistributedCache (e.g., Memory Cache).
services.AddDistributedMemoryCache();
// Register the IdempotentAPI.Cache.DistributedCache.
services.AddIdempotentAPIUsingDistributedCache();
// Choice 2: FusionCache
// Register the IdempotentAPI.Cache.FusionCache.
services.AddIdempotentAPIUsingFusionCache();
// Example: FusionCache with 2nd-level cache using Redis
services.AddStackExchangeRedisCache(options =>
{
options.Configuration = "YOUR CONNECTION STRING";
});
services.AddFusionCacheNewtonsoftJsonSerializer();
services.AddIdempotentAPIUsingFusionCache();