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 ;
24using Shortify . NET . Application . Shared . Models ;
35using Shortify . NET . Common . FunctionalTypes ;
46using 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