Refactored cache ttl handling
This commit is contained in:
@@ -0,0 +1,9 @@
|
|||||||
|
namespace PostFundManagement.Application.Abstractions;
|
||||||
|
|
||||||
|
public abstract class ServiceCacheBase
|
||||||
|
{
|
||||||
|
protected readonly DistributedCacheEntryOptions cacheOptions = new()
|
||||||
|
{
|
||||||
|
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(2)
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -26,6 +26,7 @@
|
|||||||
|
|
||||||
<!-- Shared Usings -->
|
<!-- Shared Usings -->
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Using Include="Microsoft.Extensions.Caching.Distributed" />
|
||||||
<Using Include="Microsoft.Extensions.Caching.Hybrid" />
|
<Using Include="Microsoft.Extensions.Caching.Hybrid" />
|
||||||
<Using Include="System.IdentityModel.Tokens.Jwt" />
|
<Using Include="System.IdentityModel.Tokens.Jwt" />
|
||||||
<Using Include="Asp.Versioning" />
|
<Using Include="Asp.Versioning" />
|
||||||
|
|||||||
+3
-2
@@ -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.Application.Services.Shared;
|
||||||
using PostFundManagement.Domain;
|
using PostFundManagement.Domain;
|
||||||
using PostFundManagement.Domain.Models;
|
using PostFundManagement.Domain.Models;
|
||||||
|
|
||||||
namespace PostFundManagement.Application.Services.Communications;
|
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<Result<Invite>> GetApplicantInviteAsync(Guid userId, CancellationToken cancellationToken = default) =>
|
public async ValueTask<Result<Invite>> GetApplicantInviteAsync(Guid userId, CancellationToken cancellationToken = default) =>
|
||||||
await Result.Try((Func<ValueTask<Result<Invite>>>)(async () =>
|
await Result.Try((Func<ValueTask<Result<Invite>>>)(async () =>
|
||||||
|
|||||||
@@ -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.Application.Services.Shared;
|
||||||
using PostFundManagement.Domain;
|
using PostFundManagement.Domain;
|
||||||
using PostFundManagement.Domain.Models;
|
using PostFundManagement.Domain.Models;
|
||||||
|
|
||||||
namespace PostFundManagement.Application.Services.Evidence;
|
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<Result> CreateEvidenceAsync(Guid userId, IFormCollection form, IFormFile file, CancellationToken cancellationToken = default) =>
|
public async ValueTask<Result> CreateEvidenceAsync(Guid userId, IFormCollection form, IFormFile file, CancellationToken cancellationToken = default) =>
|
||||||
await service.CreateEvidenceAsync(userId, form, file, cancellationToken);
|
await service.CreateEvidenceAsync(userId, form, file, cancellationToken);
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
using Microsoft.Extensions.Caching.Distributed;
|
using PostFundManagement.Application.Abstractions;
|
||||||
using PostFundManagement.Application.Abstractions.Services;
|
using PostFundManagement.Application.Abstractions.Services;
|
||||||
using PostFundManagement.Domain.Errors;
|
using PostFundManagement.Domain.Errors;
|
||||||
|
|
||||||
namespace PostFundManagement.Application.Services.Shared;
|
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<Result> UserIsAuthorisedAsync(Guid userId, CancellationToken cancellationToken = default) =>
|
public async ValueTask<Result> UserIsAuthorisedAsync(Guid userId, CancellationToken cancellationToken = default) =>
|
||||||
await Result.Try((Func<ValueTask<Result>>)(async () =>
|
await Result.Try((Func<ValueTask<Result>>)(async () =>
|
||||||
@@ -19,7 +19,7 @@ public sealed class IdentityCacheService(IdentityService service, IDistributedCa
|
|||||||
|
|
||||||
if (result.IsFailed) return result;
|
if (result.IsFailed) return result;
|
||||||
|
|
||||||
await cache.SetStringAsync(cacheKey, "true", cancellationToken);
|
await cache.SetStringAsync(cacheKey, "true", cacheOptions, cancellationToken);
|
||||||
|
|
||||||
return Result.Ok();
|
return Result.Ok();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
using Microsoft.Extensions.Caching.Distributed;
|
using PostFundManagement.Application.Abstractions;
|
||||||
using PostFundManagement.Application.Abstractions.Services;
|
using PostFundManagement.Application.Abstractions.Services;
|
||||||
using PostFundManagement.Application.Services.Shared;
|
using PostFundManagement.Application.Services.Shared;
|
||||||
using PostFundManagement.Domain;
|
using PostFundManagement.Domain;
|
||||||
@@ -6,7 +6,7 @@ using PostFundManagement.Domain.Models;
|
|||||||
|
|
||||||
namespace PostFundManagement.Application.Services.Users;
|
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<Result<User>> GetUserAsync(Guid userId, CancellationToken cancellationToken) =>
|
public async ValueTask<Result<User>> GetUserAsync(Guid userId, CancellationToken cancellationToken) =>
|
||||||
await Result.Try((Func<ValueTask<Result<User>>>)(async () =>
|
await Result.Try((Func<ValueTask<Result<User>>>)(async () =>
|
||||||
@@ -21,7 +21,7 @@ public sealed class UserCacheService(UserService service, IDistributedCache cach
|
|||||||
|
|
||||||
if (userResult.IsFailed) return userResult;
|
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;
|
return userResult;
|
||||||
}
|
}
|
||||||
@@ -44,7 +44,7 @@ public sealed class UserCacheService(UserService service, IDistributedCache cach
|
|||||||
|
|
||||||
if (usersResult.IsFailed) return usersResult;
|
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;
|
return usersResult;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user