Skip to content

Latest commit

 

History

History
137 lines (98 loc) · 4.53 KB

README.md

File metadata and controls

137 lines (98 loc) · 4.53 KB

RedisStorm

CI NuGet License: MIT .NET

A lightweight wrapper around StackExchange.Redis that simplifies Redis pub/sub with dependency injection, attribute-based channel binding, and optional MessagePack serialization.

Features

  • JSON or MessagePack serialization for pub/sub payloads
  • Register subscribers from your assembly with [RedisChannel("channel-name")]
  • Manual channel binding via ConfigSubscriber<TSubscriber>(channel)
  • Scoped subscriber handlers with full DI support (inject your own services into Handle)
  • Works with IConnectionMultiplexer registered in your container or passed directly

Requirements

  • .NET 10 SDK
  • Redis 6+ (required when running subscribers)

Installation

dotnet add package RedisStorm --version 10.0.0

Package Manager:

Install-Package RedisStorm -Version 10.0.0

Quick start

using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using RedisStorm.Extensions;
using RedisStorm.Registration;
using StackExchange.Redis;

var builder = Host.CreateApplicationBuilder(args);

builder.Services.AddSingleton<IConnectionMultiplexer>(
    ConnectionMultiplexer.Connect(builder.Configuration.GetConnectionString("Redis")!));

builder.Services.AddRedisStorm(Assembly.GetExecutingAssembly(), factory =>
{
    factory.AddConnectionMultiplexerFromServiceCollection();

    factory.AddPublisher(p => p.SerializationType = SerializationType.Json);

    factory.AddSubscribers(s =>
    {
        s.SerializationType = SerializationType.Json;
        s.AddSubscribersFromAssembly();
    });
});

var host = builder.Build();
await host.RunAsync();

Subscriber

using RedisStorm.Attributes;
using RedisStorm.Interfaces;

[RedisChannel("orders-created")]
public sealed class OrderCreatedSubscriber : ISubscriber<OrderCreatedMessage>
{
    private readonly ILogger<OrderCreatedSubscriber> _logger;

    public OrderCreatedSubscriber(ILogger<OrderCreatedSubscriber> logger) => _logger = logger;

    public Task Handle(OrderCreatedMessage message, CancellationToken cancellationToken)
    {
        _logger.LogInformation("Order {OrderId} created", message.OrderId);
        return Task.CompletedTask;
    }
}

Use [RedisChannel] when registering subscribers via AddSubscribersFromAssembly().

Publisher

using RedisStorm.Services;

public sealed class OrderService(IRedisPublisher publisher)
{
    public Task NotifyOrderCreatedAsync(OrderCreatedMessage message, CancellationToken cancellationToken) =>
        publisher.Publish("orders-created", message, cancellationToken);
}

AddPublisher must be called inside AddRedisStorm for IRedisPublisher to be registered.

Configuration

Setting Where Default Description
Publisher serialization AddPublisher → SerializationType Json Format for published payloads
Subscriber serialization AddSubscribers → SerializationType Json Format for incoming payloads
Multiplexer AddConnectionMultiplexer or AddConnectionMultiplexerFromServiceCollection — How Redis connects
Assembly scan AddSubscribersFromAssembly() off Auto-bind types with [RedisChannel]
Manual channel ConfigSubscriber<T>(channel) — Bind a subscriber without attributes

MessagePack note: Message types must be compatible with MessagePack (e.g. [MessagePackObject]). Payloads are sent as Base64-encoded strings on the wire.

Building and testing

dotnet restore
dotnet build -c Release
dotnet test -c Release --filter "Category!=Integration"

Integration tests require Redis on localhost:6379:

docker run -d -p 6379:6379 redis:7-alpine
dotnet test -c Release --filter "Category=Integration"

Contributing

See CONTRIBUTING.md. Bug reports and feature requests are welcome via GitHub Issues.

License

This project is licensed under the MIT License.