version 0.1.0-rc0011
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>
andOption<T>
types from theTinyFp
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);