diff --git a/PostFundManagement.Application/Abstractions/ServiceCacheBase.cs b/PostFundManagement.Application/Abstractions/ServiceCacheBase.cs new file mode 100644 index 0000000..7130cf4 --- /dev/null +++ b/PostFundManagement.Application/Abstractions/ServiceCacheBase.cs @@ -0,0 +1,9 @@ +namespace PostFundManagement.Application.Abstractions; + +public abstract class ServiceCacheBase +{ + protected readonly DistributedCacheEntryOptions cacheOptions = new() + { + AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(2) + }; +} diff --git a/PostFundManagement.Application/PostFundManagement.Application.csproj b/PostFundManagement.Application/PostFundManagement.Application.csproj index b6d027c..ed7a66f 100644 --- a/PostFundManagement.Application/PostFundManagement.Application.csproj +++ b/PostFundManagement.Application/PostFundManagement.Application.csproj @@ -26,6 +26,7 @@ + diff --git a/PostFundManagement.Application/Services/Communications/CommunicationsCacheService.cs b/PostFundManagement.Application/Services/Communications/CommunicationsCacheService.cs index 611b8b5..66920ee 100644 --- a/PostFundManagement.Application/Services/Communications/CommunicationsCacheService.cs +++ b/PostFundManagement.Application/Services/Communications/CommunicationsCacheService.cs @@ -1,11 +1,12 @@ -using PostFundManagement.Application.Abstractions.Services; +using PostFundManagement.Application.Abstractions; +using PostFundManagement.Application.Abstractions.Services; using PostFundManagement.Application.Services.Shared; using PostFundManagement.Domain; using PostFundManagement.Domain.Models; namespace PostFundManagement.Application.Services.Communications; -public sealed class CommunicationsCacheService(CommunicationsService service, HybridCache cache) : ICommunicationsService +public sealed class CommunicationsCacheService(CommunicationsService service, HybridCache cache) : ServiceCacheBase, ICommunicationsService { public async ValueTask> GetApplicantInviteAsync(Guid userId, CancellationToken cancellationToken = default) => await Result.Try((Func>>)(async () => diff --git a/PostFundManagement.Application/Services/Evidence/EvidenceCacheService.cs b/PostFundManagement.Application/Services/Evidence/EvidenceCacheService.cs index 90644e6..a9db1fa 100644 --- a/PostFundManagement.Application/Services/Evidence/EvidenceCacheService.cs +++ b/PostFundManagement.Application/Services/Evidence/EvidenceCacheService.cs @@ -1,11 +1,12 @@ -using PostFundManagement.Application.Abstractions.Services; +using PostFundManagement.Application.Abstractions; +using PostFundManagement.Application.Abstractions.Services; using PostFundManagement.Application.Services.Shared; using PostFundManagement.Domain; using PostFundManagement.Domain.Models; namespace PostFundManagement.Application.Services.Evidence; -public sealed class EvidenceCacheService(EvidenceService service, HybridCache cache) : IEvidenceService +public sealed class EvidenceCacheService(EvidenceService service, HybridCache cache) : ServiceCacheBase, IEvidenceService { public async ValueTask CreateEvidenceAsync(Guid userId, IFormCollection form, IFormFile file, CancellationToken cancellationToken = default) => await service.CreateEvidenceAsync(userId, form, file, cancellationToken); diff --git a/PostFundManagement.Application/Services/Shared/IdentityCacheService.cs b/PostFundManagement.Application/Services/Shared/IdentityCacheService.cs index d635950..dbf5ea7 100644 --- a/PostFundManagement.Application/Services/Shared/IdentityCacheService.cs +++ b/PostFundManagement.Application/Services/Shared/IdentityCacheService.cs @@ -1,10 +1,10 @@ -using Microsoft.Extensions.Caching.Distributed; +using PostFundManagement.Application.Abstractions; using PostFundManagement.Application.Abstractions.Services; using PostFundManagement.Domain.Errors; namespace PostFundManagement.Application.Services.Shared; -public sealed class IdentityCacheService(IdentityService service, IDistributedCache cache) : IIdentityService +public sealed class IdentityCacheService(IdentityService service, IDistributedCache cache) : ServiceCacheBase, IIdentityService { public async ValueTask UserIsAuthorisedAsync(Guid userId, CancellationToken cancellationToken = default) => await Result.Try((Func>)(async () => @@ -19,7 +19,7 @@ public sealed class IdentityCacheService(IdentityService service, IDistributedCa if (result.IsFailed) return result; - await cache.SetStringAsync(cacheKey, "true", cancellationToken); + await cache.SetStringAsync(cacheKey, "true", cacheOptions, cancellationToken); return Result.Ok(); } diff --git a/PostFundManagement.Application/Services/Users/UserCacheService.cs b/PostFundManagement.Application/Services/Users/UserCacheService.cs index 992bd29..fe38c85 100644 --- a/PostFundManagement.Application/Services/Users/UserCacheService.cs +++ b/PostFundManagement.Application/Services/Users/UserCacheService.cs @@ -1,4 +1,4 @@ -using Microsoft.Extensions.Caching.Distributed; +using PostFundManagement.Application.Abstractions; using PostFundManagement.Application.Abstractions.Services; using PostFundManagement.Application.Services.Shared; using PostFundManagement.Domain; @@ -6,7 +6,7 @@ using PostFundManagement.Domain.Models; namespace PostFundManagement.Application.Services.Users; -public sealed class UserCacheService(UserService service, IDistributedCache cache) : IUserService +public sealed class UserCacheService(UserService service, IDistributedCache cache) : ServiceCacheBase, IUserService { public async ValueTask> GetUserAsync(Guid userId, CancellationToken cancellationToken) => await Result.Try((Func>>)(async () => @@ -21,7 +21,7 @@ public sealed class UserCacheService(UserService service, IDistributedCache cach if (userResult.IsFailed) return userResult; - await cache.SetStringAsync(cacheKey, JsonSerializer.Serialize(userResult.Value)); + await cache.SetStringAsync(cacheKey, JsonSerializer.Serialize(userResult.Value), cacheOptions, cancellationToken); return userResult; } @@ -44,7 +44,7 @@ public sealed class UserCacheService(UserService service, IDistributedCache cach if (usersResult.IsFailed) return usersResult; - await cache.SetStringAsync(cacheKey, JsonSerializer.Serialize(usersResult.Value)); + await cache.SetStringAsync(cacheKey, JsonSerializer.Serialize(usersResult.Value), cacheOptions, cancellationToken); return usersResult; }