Msb.Redis.Extensions 1.0.1

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

Msb.Redis.Extensions

Msb.Redis.Extensions is a utility library that simplifies working with IDistributedCache (such as Redis) in .NET applications. It provides strongly-typed extension methods to store, fetch, and manipulate single objects and lists — with support for both synchronous and asynchronous operations.


✅ Features

  • 🚀 Simple and consistent API to work with Redis
  • 🔁 Support for object list operations: Add, Delete, Update, ToList
  • 🔐 Custom key resolution via generic types or IRedisKey interface
  • ⚡ Sync and Async support
  • 🧰 Works seamlessly with dependency injection and IDistributedCache

📦 Requirements

  • .NET 5.0+
  • Redis server (or any IDistributedCache implementation)
  • NuGet Packages:
    • Microsoft.Extensions.Caching.StackExchangeRedis

⚙️ Plug & Play Set up Redis in Program.cs (or Startup.cs):

services.AddStackExchangeRedisCache(options =>
{
    options.Configuration = "localhost:6379";
});

🧰 Setup

1. Add Model

User.cs
public class User
{
    public int Id { get; set; }
    public string Name { get; set; }

}

public class UserKey : IRedisKey
{
    public string RedisKey { get; set; }
}

2. Add Service

RedisService.cs

public class RedisService
{
    private readonly IDistributedCache _cache;

    public RedisService(IDistributedCache cache)
    {
        _cache = cache;
    }


 	public async Task RunAsync()
    {
        // Example model
        var user = new User { Id = 1, Name = "Alice" };

		// Use your session user ID or any unique identifier here
        var userKey = new UserKey { RedisKey = "user-1" };// Example: "user-1" could be "user-{sessionUserId}"

        // --- Store single object ---
        await _cache.StoreAsync(user); // uses typeof(User) as key
        await _cache.StoreAsync(userKey, user); // uses key.Key

        // --- Fetch object ---
        var fetchedUser = await _cache.FetchAsync<User>();
        var fetchedByKey = await _cache.FetchAsync<User>(userKey);

        // --- Add to list ---
        await _cache.AddAsync(user); // adds to "user:list"
        await _cache.AddAsync(userKey, user); // adds to "user:user-1:list"

        // --- Update in list ---
        user.Name = "Alice Updated";
        await _cache.UpdateAsync(user, x => x.Id == user.Id);
        await _cache.UpdateAsync(userKey, user, x => x.Id == user.Id);

        // --- Delete from list ---
        await _cache.DeleteAsync(user);
        await _cache.DeleteAsync(userKey, user);

        // --- Fetch list ---
        var userList = await _cache.ToListAsync<User>();
        var userListWithKey = await _cache.ToListAsync<User>(userKey);

        // --- Remove single key ---
        await _cache.RemoveAsync<User>();
        await _cache.RemoveAsync<User>(userKey);

        // --- Remove list ---
        await _cache.RemoveListAsync<User>();
        await _cache.RemoveListAsync<User>(userKey);
    }

    public void Run()
    {
        // Example model
        var user = new User { Id = 1, Name = "Alice" };
        // Use your session user ID or any unique identifier here

        var userKey = new UserKey { RedisKey = "user-1" };// Example: "user-1" could be "user-{sessionUserId}"

        // --- Store single object ---
        _cache.Store(user);
        _cache.Store(userKey, user); 

        // --- Fetch object ---
        var fetchedUser = _cache.Fetch<User>();
        var fetchedByKey = _cache.Fetch<User>(userKey);

        // --- Add to list ---
        _cache.Add(user); // to "user:list"
        _cache.Add(userKey, user); // to "user:user-1:list"

        // --- Update in list ---
        user.Name = "Alice Updated";
        _cache.Update(user, x => x.Id == user.Id); // default key
        _cache.Update(userKey, user, x => x.Id == user.Id); // with key

        // --- Delete from list ---
        _cache.Delete(user);
        _cache.Delete(userKey, user);

        // --- Fetch list ---
        var userList = _cache.ToList<User>();
        var userListWithKey = _cache.ToList<User>(userKey);

        // --- Remove single object ---
        _cache.Remove<User>();
        _cache.Remove<User>(userKey);

        // --- Remove list ---
        _cache.RemoveList<User>();
        _cache.RemoveList<User>(userKey);
    }
}
Method Description Sync & Async
Store, Fetch Store/fetch a single object
Add, Delete, Update Modify objects in a cached list
ToList Get the entire cached list
Remove, RemoveList Remove cached object or list entirely
Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net5.0

    • No dependencies.

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.1 93 7/13/2025
1.0.0 84 7/12/2025