Skip to content

Commit ca17d46

Browse files
committed
Added Cache invalidation on Delete and Update
1 parent eab1956 commit ca17d46

2 files changed

Lines changed: 37 additions & 3 deletions

File tree

Shortify.NET.Application/Url/Commands/DeleteUrl/DeleteShortenedUrlByIdCommandHandler.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
using Shortify.NET.Application.Abstractions.Repositories;
1+
using Shortify.NET.Application.Abstractions;
2+
using Shortify.NET.Application.Abstractions.Repositories;
3+
using Shortify.NET.Application.Shared;
24
using Shortify.NET.Common.FunctionalTypes;
35
using Shortify.NET.Common.Messaging.Abstractions;
46
using Shortify.NET.Core;
7+
using Shortify.NET.Core.Entites;
58
using Shortify.NET.Core.Errors;
69

710
namespace Shortify.NET.Application.Url.Commands.DeleteUrl;
@@ -11,10 +14,12 @@ namespace Shortify.NET.Application.Url.Commands.DeleteUrl;
1114
/// </summary>
1215
internal sealed class DeleteShortenedUrlByIdCommandHandler(
1316
IShortenedUrlRepository shortenedUrlRepository,
17+
ICachingServices cachingServices,
1418
IUnitOfWork unitOfWork)
1519
: ICommandHandler<DeleteShortenedUrlByIdCommand>
1620
{
1721
private readonly IShortenedUrlRepository _shortenedUrlRepository = shortenedUrlRepository;
22+
private readonly ICachingServices _cachingServices = cachingServices;
1823
private readonly IUnitOfWork _unitOfWork = unitOfWork;
1924

2025
/// <summary>
@@ -29,8 +34,15 @@ public async Task<Result> Handle(DeleteShortenedUrlByIdCommand command, Cancella
2934
if (url is null) return Result.Failure(DomainErrors.ShortenedUrl.ShortenedUrlNotFound);
3035

3136
_shortenedUrlRepository.Delete(url);
37+
await RemoveFromCacheAsync(url, cancellationToken);
3238
await _unitOfWork.SaveChangesAsync(cancellationToken);
3339

3440
return Result.Success();
3541
}
42+
43+
private async Task RemoveFromCacheAsync(ShortenedUrl shortenedUrl, CancellationToken cancellationToken)
44+
{
45+
var cacheKey = $"{Constant.Cache.Prefixes.OriginalUrls}{shortenedUrl.Code}";
46+
await _cachingServices.RemoveAsync(cacheKey, cancellationToken);
47+
}
3648
}

Shortify.NET.Application/Url/Commands/UpdateUrl/UpdateShortenedUrlCommandHandler.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using Shortify.NET.Application.Abstractions.Repositories;
1+
using Shortify.NET.Application.Abstractions;
2+
using Shortify.NET.Application.Abstractions.Repositories;
3+
using Shortify.NET.Application.Shared;
24
using Shortify.NET.Application.Shared.Models;
35
using Shortify.NET.Common.FunctionalTypes;
46
using Shortify.NET.Common.Messaging.Abstractions;
@@ -9,10 +11,14 @@ namespace Shortify.NET.Application.Url.Commands.UpdateUrl
911
{
1012
internal sealed class UpdateShortenedUrlCommandHandler(
1113
IShortenedUrlRepository shortenedUrlRepository,
14+
ICachingServices cachingServices,
1215
IUnitOfWork unitOfWork)
1316
: ICommandHandler<UpdateShortenedUrlCommand, ShortenedUrlDto>
1417
{
1518
private readonly IShortenedUrlRepository _shortenedUrlRepository = shortenedUrlRepository;
19+
20+
private readonly ICachingServices _cachingServices = cachingServices;
21+
1622
private readonly IUnitOfWork _unitOfWork = unitOfWork;
1723

1824
public async Task<Result<ShortenedUrlDto>> Handle(
@@ -27,7 +33,7 @@ public async Task<Result<ShortenedUrlDto>> Handle(
2733
_shortenedUrlRepository.Update(url);
2834
await _unitOfWork.SaveChangesAsync(cancellationToken);
2935

30-
return new ShortenedUrlDto(
36+
var response = new ShortenedUrlDto(
3137
Id: url.Id,
3238
UserId: url.UserId,
3339
OriginalUrl: url.OriginalUrl,
@@ -39,7 +45,23 @@ public async Task<Result<ShortenedUrlDto>> Handle(
3945
UpdatedOnUtc: url.UpdatedOnUtc,
4046
RowStatus: url.RowStatus
4147
);
48+
await SetCache(response, cancellationToken);
49+
50+
return response;
51+
}
52+
53+
private async Task SetCache(
54+
ShortenedUrlDto cacheItem,
55+
CancellationToken cancellationToken)
56+
{
57+
var cacheKey = $"{Constant.Cache.Prefixes.OriginalUrls}{cacheItem.Code}";
4258

59+
await _cachingServices
60+
.SetAsync(
61+
cacheKey,
62+
cacheItem,
63+
cancellationToken: cancellationToken,
64+
slidingExpiration: TimeSpan.FromDays(1));
4365
}
4466
}
4567
}

0 commit comments

Comments
 (0)