integration redis database - grecosoft/NetFusion GitHub Wiki

IMAGERedis: IDatabase Service

The Redis plugin process access to the following two interfaces:

  • IDatabase
  • IPublisher

When the Redis plugin is registered and the microservice bootstraps, these two interfaces are registered within the dependency-injection container and can be injected into others services.

Accessing the IDatabase Interface

Once the Redis plug-in is configured, the IDatabase and IPublisher interfaces provided by StackExchange can be referenced. An application component registered within the Service Collection can inject an instance of the IRedisService interface. This interface provides the GetDatabase and GetSubscriber methods used to obtain references to the above interfaces. Since these are the interfaces provided by the StackExchange.Redis library, the following documentation can be used as a reference:

The following shows a WebApi controller injecting a IRedisService reference used to access a reference to the IDatabase interface implementation:

using Microsoft.AspNetCore.Mvc;
using NetFusion.Core.Bootstrap.Container;
using NetFusion.Integration.Redis;
using StackExchange.Redis;

namespace Examples.Redis.WebApi.Controllers;

[ApiController, Route("api/[controller]")]
public class ExamplesController : ControllerBase
{
    private readonly IDatabase _redisDb;
    
    public ExamplesController(IRedisService redis)
    {
        _redisDb = redis.GetDatabase("testDb");
    }

    [HttpPost("set/add/{key}/{value}")]
    public async Task<IActionResult> SetAdd(string key, string value)
    {
        Console.WriteLine($"Database Number: {_redisDb.Database}");
        await _redisDb.SetAddAsync(key, value);
        return Ok();
    }

    [HttpPost("set/pop/{key}")]
    public async Task<IActionResult> SetPop(string key)
    {
        var value = await _redisDb.SetPopAsync(key);
        return Ok(value.ToString());
    }

    [HttpGet("set/members/{key}")]
    public async Task<IActionResult> SetMembers(string key)
    {
        var values = await _redisDb.SetMembersAsync(key);
        return Ok(values.Select(v => v.ToString()));
    }
}

The above controller methods invoke methods on the Redis set data structure.

Execute Example

Complete the following to run the example microservice and send requests to the example controller:

Execute Microservice

cd ./Examples.Redis/src/Examples.Redis.WebApi/
dotnet run

Send Requests

Post the following requests to add data to the set having "cars" as the key:

POST http://localhost:5005/api/examples/set/add/cars/bmw

POST http://localhost:5005/api/examples/set/add/cars/honda

POST http://localhost:5005/api/examples/set/add/cars/volvo

POST http://localhost:5005/api/examples/set/add/cars/saab

Next, list the members of the set by invoking the following:

GET http://localhost:5005/api/examples/set/members/cars

[
    "bmw",
    "saab",
    "vovo",
    "honda"
]

Lastly, execute the following to pop two members off of the set:

POST http://localhost:5005/api/examples/set/pop/cars

bmw

POST http://localhost:5005/api/examples/set/pop/cars

honda