GoatFam.AzureRedisCache 1.0.0

dotnet add package GoatFam.AzureRedisCache --version 1.0.0
                    
NuGet\Install-Package GoatFam.AzureRedisCache -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="GoatFam.AzureRedisCache" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="GoatFam.AzureRedisCache" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="GoatFam.AzureRedisCache" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add GoatFam.AzureRedisCache --version 1.0.0
                    
#r "nuget: GoatFam.AzureRedisCache, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package GoatFam.AzureRedisCache@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=GoatFam.AzureRedisCache&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=GoatFam.AzureRedisCache&version=1.0.0
                    
Install as a Cake Tool

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 IDistributedCache interface 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:

License

This project is licensed under the MIT License.

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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