FGV.Lib.Persistence.Redis 1.0.0

dotnet add package FGV.Lib.Persistence.Redis --version 1.0.0
                    
NuGet\Install-Package FGV.Lib.Persistence.Redis -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="FGV.Lib.Persistence.Redis" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="FGV.Lib.Persistence.Redis" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="FGV.Lib.Persistence.Redis" />
                    
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 FGV.Lib.Persistence.Redis --version 1.0.0
                    
#r "nuget: FGV.Lib.Persistence.Redis, 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 FGV.Lib.Persistence.Redis@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=FGV.Lib.Persistence.Redis&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=FGV.Lib.Persistence.Redis&version=1.0.0
                    
Install as a Cake Tool

FGV.Lib.Persistence.Redis

A .NET library that provides helper classes and methods for Redis database interactions, offering an easy-to-use repository pattern implementation for Redis operations.

Overview

FGV.Lib.Persistence.Redis is designed to simplify Redis database operations in .NET applications. It provides a generic repository pattern implementation with support for:

  • CRUD operations (Create, Read, Update, Delete)
  • Batch operations for performance optimization
  • Automatic JSON serialization/deserialization of objects
  • Integrated monitoring with Elastic APM
  • Type-safe Redis operations

Installation

NuGet Package Manager

Install-Package FGV.Lib.Persistence.Redis

.NET CLI

dotnet add package FGV.Lib.Persistence.Redis

Package Reference (in .csproj)

<PackageReference Include="FGV.Lib.Persistence.Redis" Version="x.x.x" />

Configuration

Dependency Injection Setup

Register the Redis repository in your application's dependency injection container:

using FGV.Lib.Persistence.Redis;
using FGV.Lib.Persistence.Redis.Interface;

// In your Startup.cs or Program.cs
services.AddSingleton<IRedisRepository, RedisRepository>(sp => 
    new RedisRepository(Configuration["Redis:ConnectionString"]));

Redis Connection String Format

The Redis connection string should follow this format:

[password@]host[:port][,host2[:port2],...][?parameters]

Examples:

localhost:6379
password@redis.example.com:6379
redis.server:6379,redis.backup:6379?ssl=true&abortConnect=false

Common parameters:

  • ssl: Enable SSL (true/false)
  • abortConnect: Whether to abort connection if connection fails (true/false)
  • connectTimeout: Connection timeout in milliseconds
  • syncTimeout: Synchronous operation timeout in milliseconds

Usage Examples

Basic Operations

// Inject IRedisRepository in your class
private readonly IRedisRepository _redisRepository;

public MyService(IRedisRepository redisRepository)
{
    _redisRepository = redisRepository;
}

// Store an object
public async Task SaveUserAsync(User user)
{
    // The first parameter is the key, the second is the object to store
    await _redisRepository.StoreAsync($"user:{user.Id}", user);
}

// Retrieve an object
public async Task<User> GetUserAsync(int userId)
{
    return await _redisRepository.GetAsync<User>($"user:{userId}");
}

// Delete an object
public async Task DeleteUserAsync(int userId)
{
    await _redisRepository.DeleteAsync($"user:{userId}");
}

Working with Collections

// Store multiple objects
public async Task SaveUsersAsync(IEnumerable<User> users)
{
    var keyValuePairs = users.Select(user => 
        new KeyValuePair<string, User>($"user:{user.Id}", user));
        
    await _redisRepository.StoreBatchAsync(keyValuePairs);
}

// Retrieve multiple objects
public async Task<IEnumerable<User>> GetUsersAsync(IEnumerable<int> userIds)
{
    var keys = userIds.Select(id => $"user:{id}").ToArray();
    return await _redisRepository.GetBatchAsync<User>(keys);
}

// Delete multiple objects
public async Task DeleteUsersAsync(IEnumerable<int> userIds)
{
    var keys = userIds.Select(id => $"user:{id}").ToArray();
    await _redisRepository.DeleteBatchAsync(keys);
}

Working with Expiration

// Store with expiration
public async Task SaveTemporaryTokenAsync(string token, TimeSpan expiry)
{
    await _redisRepository.StoreAsync($"token:{token}", new { Token = token }, expiry);
}

API Documentation

IRedisRepository Interface

Basic Operations
  • Task<T> GetAsync<T>(string key)
    Retrieves an object from Redis by key.

  • Task<bool> StoreAsync<T>(string key, T value, TimeSpan? expiry = null)
    Stores an object in Redis with an optional expiration time.

  • Task<bool> DeleteAsync(string key)
    Deletes an object from Redis by key.

  • Task<bool> ExistsAsync(string key)
    Checks if a key exists in Redis.

Batch Operations
  • Task<IEnumerable<T>> GetBatchAsync<T>(string[] keys)
    Retrieves multiple objects from Redis by their keys.

  • Task<bool> StoreBatchAsync<T>(IEnumerable<KeyValuePair<string, T>> keyValuePairs, TimeSpan? expiry = null)
    Stores multiple objects in Redis with an optional expiration time.

  • Task<bool> DeleteBatchAsync(string[] keys)
    Deletes multiple objects from Redis by their keys.

Transaction Operations
  • Task<bool> StoreWithTransactionAsync<T>(string key, T value, string compareKey, TimeSpan? expiry = null)
    Stores an object only if the compare key does not exist (atomic operation).
Utility Methods
  • Task<string> GetConnectionStringAsync()
    Returns the connection string used by the repository.

Dependencies

  • .NET 7.0 or higher
  • StackExchange.Redis - For Redis connection handling
  • Newtonsoft.Json - For object serialization/deserialization
  • Elastic.Apm.StackExchange.Redis - For Redis APM monitoring

License

This project is licensed under the MIT License - see the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  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 was computed.  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. 
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 226 5/29/2025