Skip to content

jadsena/Redis.Data

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status Nuget Nuget (with prereleases)

Redis.Data

Componente para facilitar a utilização do Redis como banco de dados.

Instalação

Abrir o gerenciador de pacotes nuget e pesquisar na aba Procurar pelo nome do pacote Redis.Data e então clique em instalar.

Utilização

Na class startup.cs realizar as seguintes alterações:

Utilizando o database padrãodo Redis

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();
    //Metodo default
    services.AddRedisDB(Configuration.GetConnectionString("Redis"));

    // Register the Swagger generator, defining 1 or more Swagger documents
    services.AddSwaggerGen();
}

Utilizando um database diferente do padrão

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();
    //Metodo setando o database
    services.AddRedisDB(Configuration.GetConnectionString("Redis"), 1);

    // Register the Swagger generator, defining 1 or more Swagger documents
    services.AddSwaggerGen();
}

Configurando a conexão com o Redis no arquivo appSettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "ConnectionStrings": {
    "Redis": "127.0.0.1:6379"
  }
}

Usando a conexão em um Controller

[Route("api/[controller]")]
[ApiController]
public class RegraController : ControllerBase
{
    private readonly IRedisContext _context;

    public RegraController(IRedisContext context)
    {
        _context = context;
    }
    // GET: api/<RegraController>
    [HttpGet]
    public async Task<ModeloDadosCollection> Get()
    {
        return new ModeloDadosCollection(await _context.GetAllAsync());
    }

    // GET api/<RegraController>/5
    [HttpGet("{key}")]
    public async Task<ModeloDados> Get(string key)
    {
        return await _context.GetAsync<ModeloDados>(key);
    }

    // POST api/<RegraController>
    [HttpPost]
    public async Task Post([FromBody] ModeloDados value)
    {
        if (!ModelState.IsValid) return;
        await _context.SetAsync(value.Nome, JsonSerializer.Serialize(value));
    }

    // PUT api/<RegraController>/5
    [HttpPut("{key}")]
    public async Task Put(string key, [FromBody] ModeloDados value)
    {
        if (!ModelState.IsValid) return;
        await _context.SetAsync(key, JsonSerializer.Serialize(value));
    }

    // DELETE api/<RegraController>/5
    [HttpDelete("{key}")]
    public async Task Delete(string key)
    {
        await _context.DeleteAsync(key);
    }
}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages