adding cache
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Npgsql;
|
||||
using OpenCand.Core.Models;
|
||||
|
||||
@@ -6,33 +7,44 @@ namespace OpenCand.Repository
|
||||
{
|
||||
public class CandidatoRepository : BaseRepository
|
||||
{
|
||||
public CandidatoRepository(IConfiguration configuration) : base(configuration)
|
||||
public CandidatoRepository(IConfiguration configuration, IMemoryCache? cache = null) : base(configuration, cache)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<List<Candidato>> SearchCandidatosAsync(string query)
|
||||
public async Task<List<Candidato>?> SearchCandidatosAsync(string query)
|
||||
{
|
||||
using var connection = new NpgsqlConnection(ConnectionString);
|
||||
return (await connection.QueryAsync<Candidato>(@"
|
||||
SELECT c.*,
|
||||
GREATEST(similarity(c.apelido, @q), similarity(c.nome, @q)) AS sim
|
||||
FROM candidato c
|
||||
WHERE c.apelido % @q
|
||||
OR c.nome % @q
|
||||
ORDER BY c.popularidade DESC, sim DESC, length(c.nome) ASC
|
||||
LIMIT 10;
|
||||
", new { q = query })).AsList();
|
||||
string cacheKey = GenerateCacheKey("Search", query);
|
||||
|
||||
return await GetOrSetCacheAsync(cacheKey, async () =>
|
||||
{
|
||||
using var connection = new NpgsqlConnection(ConnectionString);
|
||||
return (await connection.QueryAsync<Candidato>(@"
|
||||
SELECT c.*,
|
||||
GREATEST(similarity(c.apelido, @q), similarity(c.nome, @q)) AS sim
|
||||
FROM candidato c
|
||||
WHERE c.apelido % @q
|
||||
OR c.nome % @q
|
||||
ORDER BY c.popularidade DESC, sim DESC, length(c.nome) ASC
|
||||
LIMIT 10;
|
||||
", new { q = query })).AsList();
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
public async Task<Candidato?> GetCandidatoAsync(Guid idcandidato)
|
||||
{
|
||||
using (var connection = new NpgsqlConnection(ConnectionString))
|
||||
string cacheKey = GenerateCacheKey("Candidato", idcandidato);
|
||||
|
||||
return await GetOrSetCacheAsync(cacheKey, async () =>
|
||||
{
|
||||
return await connection.QueryFirstOrDefaultAsync<Candidato>(@"
|
||||
SELECT * FROM candidato
|
||||
WHERE idcandidato = @idcandidato;",
|
||||
new { idcandidato });
|
||||
}
|
||||
using (var connection = new NpgsqlConnection(ConnectionString))
|
||||
{
|
||||
return await connection.QueryFirstOrDefaultAsync<Candidato>(@"
|
||||
SELECT * FROM candidato
|
||||
WHERE idcandidato = @idcandidato;",
|
||||
new { idcandidato });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<string?> GetCandidatoCpfAsync(Guid idcandidato)
|
||||
|
Reference in New Issue
Block a user