Skip to content

Commit

Permalink
feat(getAllExpensesQuery,redis): added redisCache to getAllExpensesQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
mezdelex committed Aug 2, 2024
1 parent 339762c commit 8871b2a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ await _redisCache.SetCachedData<PagedList<CategoryDTO>>(

return pagedCategories;
}
}
}
26 changes: 23 additions & 3 deletions src/Application/Expenses/GetAllAsync/GetAllExpensesQueryHandler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Application.Contexts;
using Application.Shared;
using Domain.Cache;
using MediatR;
using static Domain.Extensions.Collections.Collections;

Expand All @@ -9,18 +10,37 @@ public sealed class GetAllExpensesQueryHandler
: IRequestHandler<GetAllExpensesQuery, PagedList<ExpenseDTO>>
{
private readonly IApplicationDbContext _context;
private readonly IRedisCache _redisCache;

public GetAllExpensesQueryHandler(IApplicationDbContext context)
public GetAllExpensesQueryHandler(IApplicationDbContext context, IRedisCache redisCache)
{
_context = context;
_redisCache = redisCache;
}

public async Task<PagedList<ExpenseDTO>> Handle(
GetAllExpensesQuery request,
CancellationToken cancellationToken
) =>
await _context
)
{
var redisKey = $"{nameof(GetAllExpensesQuery)}#{request.Page}#{request.PageSize}";
var cachedGetAllExpensesQuery = await _redisCache.GetCachedData<PagedList<ExpenseDTO>>(
redisKey
);
if (cachedGetAllExpensesQuery != null)
return cachedGetAllExpensesQuery;

var pagedExpenses = await _context
.Expenses.OrderBy(e => e.Name)
.Select(e => new ExpenseDTO(e.Id, e.Name, e.Description, e.Value, e.CategoryId))
.ToPagedListAsync(request.Page, request.PageSize, cancellationToken);

await _redisCache.SetCachedData<PagedList<ExpenseDTO>>(
redisKey,
pagedExpenses,
DateTimeOffset.Now.AddMinutes(5)
);

return pagedExpenses;
}
}

0 comments on commit 8871b2a

Please sign in to comment.