Skip to content

Commit

Permalink
fix(cache): made caching keys Command/Query name and metadata argumen…
Browse files Browse the repository at this point in the history
…ts dependant
  • Loading branch information
mezdelex committed Jul 31, 2024
1 parent a0e13cc commit 339762c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ public async Task<PagedList<CategoryDTO>> Handle(
CancellationToken cancellationToken
)
{
var redisKey = $"{nameof(GetAllCategoriesQuery)}#{request.Page}#{request.PageSize}";
var cachedGetAllCategoriesQuery = await _redisCache.GetCachedData<PagedList<CategoryDTO>>(
nameof(GetAllCategoriesQuery)
redisKey
);
if (cachedGetAllCategoriesQuery != null)
return cachedGetAllCategoriesQuery;
Expand All @@ -37,7 +38,7 @@ CancellationToken cancellationToken
.ToPagedListAsync(request.Page, request.PageSize, cancellationToken);

await _redisCache.SetCachedData<PagedList<CategoryDTO>>(
nameof(GetAllCategoriesQuery),
redisKey,
pagedCategories,
DateTimeOffset.Now.AddMinutes(5)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public async Task GetAllCategoriesQueryHandler_ShouldReturnPagedListOfRequestedC
var page = 1;
var pageSize = 2;
var getAllCategoriesQuery = new GetAllCategoriesQuery(page, pageSize);
var redisKey = $"{nameof(GetAllCategoriesQuery)}#{page}#{pageSize}";
var categories = new List<Category>
{
new(Guid.NewGuid(), "Name 1", "Description 1"),
Expand Down Expand Up @@ -62,14 +63,12 @@ public async Task GetAllCategoriesQueryHandler_ShouldReturnPagedListOfRequestedC
.Returns(categories.AsQueryable().GetEnumerator());
_context.Setup(mock => mock.Categories).Returns(_dbSet.Object).Verifiable();
_redisCache
.Setup(mock =>
mock.GetCachedData<PagedList<CategoryDTO>>(nameof(GetAllCategoriesQuery))
)
.Setup(mock => mock.GetCachedData<PagedList<CategoryDTO>>(redisKey))
.ReturnsAsync((PagedList<CategoryDTO>)null!);
_redisCache
.Setup(mock =>
mock.SetCachedData(
nameof(GetAllCategoriesQuery),
redisKey,
It.IsAny<PagedList<CategoryDTO>>(),
It.IsAny<DateTimeOffset>()
)
Expand All @@ -93,13 +92,13 @@ public async Task GetAllCategoriesQueryHandler_ShouldReturnPagedListOfRequestedC
result.HasNextPage.Should().Be(false);
_context.Verify();
_redisCache.Verify(
mock => mock.GetCachedData<PagedList<CategoryDTO>>(nameof(GetAllCategoriesQuery)),
mock => mock.GetCachedData<PagedList<CategoryDTO>>(redisKey),
Times.Once
);
_redisCache.Verify(
mock =>
mock.SetCachedData(
nameof(GetAllCategoriesQuery),
redisKey,
It.IsAny<PagedList<CategoryDTO>>(),
It.IsAny<DateTimeOffset>()
),
Expand Down

0 comments on commit 339762c

Please sign in to comment.