Skip to content

fabiolune/func-redis

Repository files navigation

This documentation is in line with the active development, hence should be considered work in progress. To check the documentation for the latest stable version please visit https://fabiolune.github.io/func-redis/

Functional Redis

GitHub CI Quality Gate Status codecov Mutation testing badge

func-redis contains components aimed to simplify the adoption of StackExchange.Redis using a Functional Programming-first approach (thanks to Franco Melandri's tiny-fp).

The design of Func.Redis emphasizes a robust and flexible functional programming approach. Below are the key aspects that highlight the purpose and benefits of using Func.Redis in your .NET applications:

  • Abstraction: These classes and interfaces abstract the complexity of interacting with Redis, providing a clean and consistent API for the rest of the application.
  • Key Transformation: The key transformation functions allow for flexible key management, enabling the modification of keys before they are used in Redis operations.
  • Error Handling: The use of Either<Error, Unit> and Option<T> types from the TinyFp library helps in handling errors and optional values in a functional programming style.
  • Asynchronous Operations: The inclusion of asynchronous methods ensures that the application can perform Redis operations without blocking the main thread, improving performance and scalability.

Usage

Installation

dotnet add package func-redis

To include the functionalities to setup the native dependency injection container you will need to install the associated extensions:

dotnet add package func-redis-extensions

Example

using TinyFp;
using Func.Redis.Extensions;
using Func.Redis;
using Microsoft.Extensions.DependencyInjection;
using Func.Redis.SerDes.Json;
using Microsoft.Extensions.Configuration;
using System.Text.Json;

// Configure services 
var config = new ConfigurationBuilder()
    .AddJsonFile("AppSettings.json", optional: false)
    .Build();

var services = new ServiceCollection();
// Use func-redis-extensions
services
  .AddRedis<SystemJsonRedisSerDes>(config, RedisCapabilities.Keys);

var serviceProvider = services.BuildServiceProvider();

// Resolve the Redis service 
var redisService = serviceProvider.GetRequiredService<IRedisKeyService>();

// Save and retrieve data
var result = await redisService
  .SetAsync("key", new TestData(42))
  .BindAsync(_ => redisService.GetAsync<TestData>("key"));

result
    .OnRight(o => o
        .OnSome(td => Console.WriteLine($"data found: {JsonSerializer.Serialize(td)}"))
        .OnNone(() => Console.WriteLine("data not found")))
    .OnLeft(e => Console.WriteLine($"Returned error {e.Message}"));

internal record TestData(int Id);

About

StackExchange wrapper using a Functional Programming approach

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors 3

  •  
  •  
  •  

Languages