diff --git a/src/Application/Categories/GetAllAsync/GetAllCategoriesQueryHandler.cs b/src/Application/Categories/GetAllAsync/GetAllCategoriesQueryHandler.cs index 65876e5..d30c236 100644 --- a/src/Application/Categories/GetAllAsync/GetAllCategoriesQueryHandler.cs +++ b/src/Application/Categories/GetAllAsync/GetAllCategoriesQueryHandler.cs @@ -45,4 +45,4 @@ await _redisCache.SetCachedData>( return pagedCategories; } -} +} \ No newline at end of file diff --git a/src/Application/Expenses/GetAllAsync/GetAllExpensesQueryHandler.cs b/src/Application/Expenses/GetAllAsync/GetAllExpensesQueryHandler.cs index 2114a6b..fd263cd 100644 --- a/src/Application/Expenses/GetAllAsync/GetAllExpensesQueryHandler.cs +++ b/src/Application/Expenses/GetAllAsync/GetAllExpensesQueryHandler.cs @@ -1,5 +1,6 @@ using Application.Contexts; using Application.Shared; +using Domain.Cache; using MediatR; using static Domain.Extensions.Collections.Collections; @@ -9,18 +10,37 @@ public sealed class GetAllExpensesQueryHandler : IRequestHandler> { 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> Handle( GetAllExpensesQuery request, CancellationToken cancellationToken - ) => - await _context + ) + { + var redisKey = $"{nameof(GetAllExpensesQuery)}#{request.Page}#{request.PageSize}"; + var cachedGetAllExpensesQuery = await _redisCache.GetCachedData>( + 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>( + redisKey, + pagedExpenses, + DateTimeOffset.Now.AddMinutes(5) + ); + + return pagedExpenses; + } }