Redis.Sharp 1.1.7

There is a newer version of this package available.
See the version list below for details.
dotnet add package Redis.Sharp --version 1.1.7
                    
NuGet\Install-Package Redis.Sharp -Version 1.1.7
                    
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="Redis.Sharp" Version="1.1.7" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Redis.Sharp" Version="1.1.7" />
                    
Directory.Packages.props
<PackageReference Include="Redis.Sharp" />
                    
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 Redis.Sharp --version 1.1.7
                    
#r "nuget: Redis.Sharp, 1.1.7"
                    
#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.
#addin nuget:?package=Redis.Sharp&version=1.1.7
                    
Install Redis.Sharp as a Cake Addin
#tool nuget:?package=Redis.Sharp&version=1.1.7
                    
Install Redis.Sharp as a Cake Tool

RedisSharp

RedisSharp is a powerful, asynchronous C# library designed to simplify interactions with Redis, providing a robust model-based abstraction layer. Built on top of StackExchange.Redis, it offers features like automatic indexing, model hydration, unique constraints, and flexible querying, making it ideal for managing complex data structures in Redis.

Features

  • Asynchronous Operations: Fully async API for non-blocking Redis interactions.
  • Model-Based Abstraction: Define data models with IAsyncModel and manage them seamlessly.
  • Hydration: Load model data lazily; use HydrateAsync() to populate models, including nested IAsyncModel instances with the [Hydrate] attribute.
  • Indexing: Automatic index generation for efficient querying with customizable index types (Text, Numeric, Tag).
  • Unique Constraints: Enforce uniqueness on properties with the [Unique] attribute.
  • Querying: Flexible RedisQuery system for searching and filtering data.
  • Linked Data: Manage relationships with AsyncLink and AsyncLinks components.
  • Batch Operations: Efficiently push and pull multiple models in a single batch.

Installation

Available on Nuget

dotnet add package Redis.Sharp

Getting Started

Initialization

Initialize the RedisSingleton with your Redis server details:

RedisSingleton.Initialize("localhost", 6379, "yourpassword");

Defining a Model

Implement IAsyncModel to create a Redis-backed model:

Note Profile or any other IAsyncModels nested within a IAsyncModel will be instantiated at the time the model is created.

using RedisSharp;

public class User : IAsyncModel
{
    public string Id { get; set; }
    public DateTime CreatedAt { get; set; }
    [Unique]
    public string Email { get; set; }
    [Indexed(IndexType.Text)]
    public string Name { get; set; }

    [Hydrate]
    public Profile Profile { get; set; }

    public string IndexName() => "users";
}

public class Profile : IAsyncModel
{
    public string Id { get; set; }
    public DateTime CreatedAt { get; set; }
    public string Bio { get; set; }

    public string IndexName() => "profiles";
}

Creating and Pushing Models

Models are created and stored in Redis using RedisRepository:

var user = new User { Id = "user1", Email = "user@example.com", Name = "John Doe", CreatedAt = DateTime.UtcNow };
var result = await RedisRepository.CreateAsync(user);
if (result.Succeeded)
{
    Console.WriteLine("User created successfully!");
}

Hydration

Models are loaded unhydrated by default. Use .HydrateAsync(); to populate them:

var user = await RedisRepository.LoadAsync<User>("user1");

// Hydrate specific properties
await user.HydrateAsync(u => u.Email, u => u.Name);

// Hydrate the entire model, including nested [Hydrate]-marked IAsyncModels (e.g., Profile)
await user.HydrateAsync();
Console.WriteLine($"User: {user.Name}, Profile Bio: {user.Profile.GetAsync().Result.Bio}");

Use the [Hydrate] attribute to fully load all of a nested IAsyncModels contents; however be warned that this is a recursive action that also loads it's own nested IAsyncModels

Simple Saving

Save data effortlessly using:

myModel.PushAsync(s => s.MyProperty, s => s.MyOtherProperty);

To prevent accidental overwrites with incomplete or uninitialized models, PushAsync without selectors is intentionally restricted, ensuring only specified properties are saved.

Querying

Use RedisQuery to search and filter models:

var query = new RedisQuery<User>(s => s.Name == "John" && s => s.Name != "Benny")
    .Where(s => s.Email == "user@example.com"));

var users = await query.ToListAsync();
foreach (var u in users)
{
    Console.WriteLine($"Found: {u.Name}");
}

typical usage:

var myModel = RedisRepository.Query<MyModel>(s => s.MyProperty == xyz).ToListAsync();

Optional query extensions Return the first item (if any) .FirstOrDefaultAsync() This will select specific properties to be pre-populated by the server .SelectAsync(s => s.ItemA, s => s.ItemB) ToPagedList(pageNumber, pageSize) PagedSelectedAsync(pageNumber, pageSize) AnyAsync(s => s.IsValid)

Managing Relationships

Use AsyncLink for single references and AsyncLinks for collections:

var profile = new Profile { Id = "profile1", Bio = "Software Engineer", CreatedAt = DateTime.UtcNow };
await RedisRepository.CreateAsync(profile);

await user.Profile.SetAsync(profile);
var loadedProfile = await user.Profile.GetAsync();
loadedProfile.HydrateAsync(s => s.Bio);
Console.WriteLine(loadedProfile.Bio); // "Software Engineer"

Deleting Models

Delete models and their associated data:

await user.DeleteAsync(); // Cleans up all nested models and links

Key Components

  • RedisSingleton: Centralized Redis connection management.
  • ModelPushHelper: Handles model persistence with unique constraint checks.
  • ModelHydrationHelper: Manages lazy loading and hydration.
  • RedisQuery: Provides a fluent API for querying Redis.
  • AsyncLink/AsyncLinks: Simplifies relationship management.
  • IndexDefinitionBuilder: Generates Redis Search indexes automatically.

Usage Notes

  • Hydration: Loaded models are not hydrated by default. Explicitly call HydrateAsync to fetch data.
  • Indexing: Use [Indexed] to mark searchable properties; specify IndexType for custom behavior.
  • Performance: Leverage batch operations (PushAsync, HydrateAsync for collections) to minimize round-trips.
  • Error Handling: Check PushResult or ModelCreationResult for operation success.

Contributing

Feel free to submit issues or pull requests! Ensure all contributions align with the project's coding standards and include tests where applicable.

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 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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  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 (1)

Showing the top 1 NuGet packages that depend on Redis.Sharp:

Package Downloads
Redis.Sharp.Identity

A better, smarter, and faster way to interact with Redis, leveraging RedisSharp.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.3.0 137 5/18/2025
1.2.9 133 5/18/2025
1.2.8 92 5/2/2025
1.2.7 145 4/30/2025
1.2.6 144 4/30/2025
1.2.5 147 4/30/2025
1.2.4 97 4/26/2025
1.2.3 93 4/26/2025
1.2.2 93 4/26/2025
1.2.1 77 4/26/2025
1.2.0 79 4/26/2025
1.1.9 78 4/26/2025
1.1.8 79 4/26/2025
1.1.7 180 4/21/2025
1.1.6 160 4/21/2025
1.1.5 161 4/20/2025
1.1.4 197 4/15/2025
1.1.3 194 4/15/2025
1.1.2 140 4/12/2025
1.1.1 168 4/10/2025
1.1.0 160 4/10/2025
1.0.9 158 4/9/2025
1.0.8 169 4/8/2025
1.0.7 156 4/8/2025
1.0.6 162 4/8/2025
1.0.5 169 4/8/2025
1.0.4 470 3/25/2025
1.0.3 475 3/25/2025
1.0.2 509 3/25/2025
1.0.1 479 3/25/2025
1.0.0 402 3/24/2025

RedisSharp is a powerful, asynchronous C# library designed to simplify interactions with Redis, providing a robust model-based abstraction layer. Built on top of StackExchange.Redis, it offers features like automatic indexing, model hydration, unique constraints, and flexible querying, making it ideal for managing complex data structures in Redis.