Flaminco.RediPolly 1.0.1

dotnet add package Flaminco.RediPolly --version 1.0.1                
NuGet\Install-Package Flaminco.RediPolly -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="Flaminco.RediPolly" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Flaminco.RediPolly --version 1.0.1                
#r "nuget: Flaminco.RediPolly, 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.
// Install Flaminco.RediPolly as a Cake Addin
#addin nuget:?package=Flaminco.RediPolly&version=1.0.1

// Install Flaminco.RediPolly as a Cake Tool
#tool nuget:?package=Flaminco.RediPolly&version=1.0.1                

Flaminco.RediPolly

The Flaminco.RediPolly library is a powerful tool designed to simplify the implementation of Redis Pub/Sub features with built-in support for Polly retry policies. Redis Pub/Sub allows for high-performance messaging between components of a distributed system, and Polly provides robust transient-fault-handling mechanisms. This library combines the two to offer a seamless experience in managing Redis channels with resilience.

Getting Started

Installation

To install the Flaminco.RediPolly package, use the following command in the .NET CLI:

dotnet add package Flaminco.RediPolly

Setup

Add the Flaminco.RediPolly package to your project and configure it to work with your Redis connection.


//TPublisherScanner is where your publishers will be located.
services.AddRediPolly<TPublisherScanner>("Your Redis Connection String");

// Here to add your listeners
builder.Services.AddHostedService<ChannelListenerExample>();

Usage

Implement listeners and publishers using provided base classes


    // Example of implementing a listener

    public class ChannelListenerExample(IOptions<RedisChannelConfiguration> options) : ChannelListener(options)
    {
        protected override RedisChannel Channel { get => RedisChannel.Literal("Your Redis Channel Name"); }

        // Returning true means don't call the retry policy as i'm managing the call even if it is actually failed. 
        // Returning false means run the retry policy even if it is actually worked.
        protected override ValueTask<bool> Callback(RedisChannel channel, RedisValue value, CancellationToken cancellationToken)
        {
            try
            {
                // Your Logic goes here

                // For Resilient messages only make sure to call MarkAsCompleted
                // MarkAsCompleted(value);

                return ValueTask.FromResult(true); // turn off the retry
            }
            catch
            {
                return ValueTask.FromResult(false); // turn on the retry policy
            }
        }

        // the default implementation will retry the callback 5 times exponentially each 3 seconds, and has no fallback behavior.
        // You can override this method to implement your own resilience logic.
        protected override ResiliencePipeline<bool> GetCallbackResiliencePipeline()
        {
            return base.GetCallbackResiliencePipeline();
        }

    }
    
    // Example of implementing a publisher

    // This should be your publisher (not much to be written here)
    public class PublishAnyMessage(IOptions<RedisChannelConfiguration> options) : ChannelPublisher(options)
    {
        protected override RedisChannel Channel => RedisChannel.Literal("Your Redis Channel Name");
    }

    // this is your publisher locator which it can get your publishers from DI by the publisher type.

    public class Example(IPublisherLocator _locator)
    {
        public async Task Publish()
        {
            if (_locator.GetPublisher<PublishAnyMessage>() is PublishAnyMessage redisPublisher)
            {
                await redisPublisher.PublishAsync(new Counter
                {
                    Count = 5
                });

                // Or you can take advantage of resilient channel by having a reference to the message in redis until it is consumed by the the listener
                // your message modole should implement an `IResilientMessage` interfcae
                // you can impelement your custom handler for the cases that you want to republish or delete with a direct access to your redis store.
                // The Resilient Messages will be stored in 'RediPolly:{Channel}:{ResilientKey}'
                await redisPublisher.ResilientPublishAsync(new Counter
                {
                    Count = 5
                });
            }
        }
    }   

Contribution

Contributions are welcome! If you have suggestions, bug reports, or contributions, please submit them as issues or pull requests on our GitHub repository.

License

This project is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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. 
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.1 110 5/12/2024
1.0.0 124 5/4/2024