GoatFam.AzureRedisCache
1.0.0
dotnet add package GoatFam.AzureRedisCache --version 1.0.0
NuGet\Install-Package GoatFam.AzureRedisCache -Version 1.0.0
<PackageReference Include="GoatFam.AzureRedisCache" Version="1.0.0" />
<PackageVersion Include="GoatFam.AzureRedisCache" Version="1.0.0" />
<PackageReference Include="GoatFam.AzureRedisCache" />
paket add GoatFam.AzureRedisCache --version 1.0.0
#r "nuget: GoatFam.AzureRedisCache, 1.0.0"
#:package GoatFam.AzureRedisCache@1.0.0
#addin nuget:?package=GoatFam.AzureRedisCache&version=1.0.0
#tool nuget:?package=GoatFam.AzureRedisCache&version=1.0.0
AzureRedisCache
A .NET Standard 2.1 library that provides a wrapper service for Azure Redis Cache with built-in serialization.
Features
- Set and get cached data with automatic serialization/deserialization - Store and retrieve complex objects with automatic JSON serialization, eliminating manual serialization code
- Support for custom cache expiration policies - Configure flexible expiration strategies including absolute, sliding, and custom time-to-live (TTL) values
- Distributed cache interface implementation - Fully compatible with
IDistributedCacheinterface for seamless integration with ASP.NET Core caching - Result pattern for cache operations - Use the Result<T> pattern to handle cache operations with explicit success/failure states and error messages
Getting Started
Prerequisites
- .NET Standard 2.1 compatible runtime (.NET Core 3.1+, .NET 5+, .NET 6+, .NET 8+)
- Azure Redis Cache instance
- StackExchange.Redis library (included as dependency)
Installation
Install the NuGet package in your project:
dotnet add package AzureRedisCache
Or add it via Package Manager:
Install-Package AzureRedisCache
For local development, reference the project directly:
<ProjectReference Include="..\..\src\AzureRedisCache\AzureRedisCache.csproj" />
Configuration
Add Azure Redis Cache configuration to your appsettings.json:
{
"RedisOptions": {
"Configuration": "your-redis-cache.redis.cache.windows.net:6380,password=your-access-key,ssl=True,abortConnect=False",
"InstanceName": "your-app-name:"
}
}
Registration
Register the service in your Program.cs or Startup.cs:
using AzureRedisCache.Extensions;
// Register with default configuration
builder.Services.AddAzureRedisWithOptions();
Basic Usage
using AzureRedisCache.Services;
using AzureRedisCache.Models;
public class ProductService
{
private readonly IWrapperCacheService _cacheService;
public ProductService(IWrapperCacheService cacheService)
{
_cacheService = cacheService;
}
// Cache a complex object
public async Task CacheProductAsync(string productId, Product product)
{
var cacheKey = $"product:{productId}";
var expiry = TimeSpan.FromHours(1);
var result = await _cacheService.SetRecordAsync(cacheKey, product, expiry);
if (!result.IsSuccess)
{
// Handle cache failure
Console.WriteLine($"Cache failed: {result.ErrorMessage}");
}
}
// Retrieve from cache with Result pattern
public async Task<Product> GetProductAsync(string productId)
{
var cacheKey = $"product:{productId}";
var result = await _cacheService.GetRecordAsync<Product>(cacheKey);
if (result.IsSuccess)
{
return result.Value;
}
// Cache miss or error - fetch from database
return await FetchProductFromDatabaseAsync(productId);
}
}
Advanced Usage
// Using Result pattern for error handling
public async Task<Result<Product>> GetCachedProductAsync(string productId)
{
var cacheKey = $"product:{productId}";
return await _cacheService.GetRecordAsync<Product>(cacheKey);
}
// Custom expiration
var slidingExpiration = TimeSpan.FromMinutes(30);
await _cacheService.SetRecordAsync("key", value, slidingExpiration);
// Check result status
var result = await _cacheService.GetRecordAsync<Product>("product:123");
if (result.IsSuccess)
{
Console.WriteLine($"Found product: {result.Value.Name}");
}
else
{
Console.WriteLine($"Error: {result.ErrorMessage}");
}
Result Pattern
The library uses the Result<T> pattern for explicit error handling:
public class Result<T>
{
public bool IsSuccess { get; set; }
public T Value { get; set; }
public string ErrorMessage { get; set; }
}
This pattern allows you to:
- Distinguish between expected failures (cache miss) and unexpected errors
- Handle errors explicitly without exceptions
- Chain operations safely
Documentation
For detailed patterns and examples:
- docs/coding-patterns.md - Result pattern and distributed cache interface
- docs/sample-applications.md - Integration examples
- examples/ArticleManagementAPI - Working implementation with caching
License
This project is licensed under the MIT License.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- Microsoft.Azure.StackExchangeRedis (>= 3.2.0)
- Microsoft.Extensions.Caching.StackExchangeRedis (>= 8.0.3)
- Microsoft.Extensions.Configuration.Abstractions (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 8.0.0)
- Microsoft.Extensions.Options.DataAnnotations (>= 8.0.0)
- System.Text.Json (>= 8.0.5)
-
net10.0
- Microsoft.Azure.StackExchangeRedis (>= 3.2.0)
- Microsoft.Extensions.Caching.StackExchangeRedis (>= 8.0.3)
- Microsoft.Extensions.Configuration.Abstractions (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 8.0.0)
- Microsoft.Extensions.Options.DataAnnotations (>= 8.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.0 | 113 | 5/24/2026 |