73 lines
3.1 KiB
C#
73 lines
3.1 KiB
C#
using PostFundManagement.Application.Abstractions;
|
|
using PostFundManagement.Application.Abstractions.Services;
|
|
using PostFundManagement.Application.Extensions;
|
|
using PostFundManagement.Application.Services.Shared;
|
|
using PostFundManagement.Domain;
|
|
using PostFundManagement.Domain.Attributes;
|
|
using PostFundManagement.Domain.Models;
|
|
using static PostFundManagement.Domain.Extensions.Constants;
|
|
|
|
namespace PostFundManagement.Application.Services.Users;
|
|
|
|
public sealed class UserCacheService(UserService service, IDistributedCache cache,
|
|
IFeatureManager featureManager) : ServiceCacheBase, IUserService
|
|
{
|
|
public async Task<Result<User>> GetUserAsync(Guid userId, CancellationToken cancellationToken) =>
|
|
await Result.Try((Func<Task<Result<User>>>)(async () =>
|
|
{
|
|
if (!await featureManager.IsEnabledAsync(ServiceCachingFeature))
|
|
return await service.GetUserAsync(userId, cancellationToken);
|
|
|
|
var cacheKey = HashService.ToMd5Hash($"getuser-{userId}").Value;
|
|
|
|
var cached = await cache.GetStringAsync(cacheKey, cancellationToken);
|
|
|
|
if(string.IsNullOrWhiteSpace(cached))
|
|
{
|
|
var userResult = await service.GetUserAsync(userId, cancellationToken);
|
|
|
|
if (userResult.IsFailed) return userResult;
|
|
|
|
await cache.SetStringAsync(cacheKey, userResult.Value.ToJson(), cacheOptions, cancellationToken);
|
|
|
|
return userResult;
|
|
}
|
|
|
|
return Result.Ok(cached.FromJson<User>());
|
|
}));
|
|
|
|
public async Task<Result<User[]>> GetUsersAsync(Pagination pagination, UserFilterRequest request, CancellationToken cancellationToken = default) =>
|
|
await Result.Try((Func<Task<Result<User[]>>>)(async () =>
|
|
{
|
|
if (!await featureManager.IsEnabledAsync(ServiceCachingFeature))
|
|
return await service.GetUsersAsync(pagination, request, cancellationToken);
|
|
|
|
var cacheKey = HashService.ToMd5Hash($"getusers-{pagination.Page}-{pagination.PageSize}-" +
|
|
$"{request.Email}-{request.Status}-{request.SortBy}-{request.SortDescending}").Value;
|
|
|
|
var cached = await cache.GetStringAsync(cacheKey, cancellationToken);
|
|
|
|
if (string.IsNullOrWhiteSpace(cached))
|
|
{
|
|
var usersResult = await service.GetUsersAsync(pagination, request, cancellationToken);
|
|
|
|
if (usersResult.IsFailed) return usersResult;
|
|
|
|
await cache.SetStringAsync(cacheKey, usersResult.Value.ToJson(),
|
|
cacheOptions, cancellationToken);
|
|
|
|
return usersResult;
|
|
}
|
|
|
|
return Result.Ok(cached.FromJson<User[]>());
|
|
}));
|
|
|
|
[Audit]
|
|
public async Task<Result> SyncUserAsync(Guid userId, string email, CancellationToken cancellationToken = default) =>
|
|
await service.SyncUserAsync(userId, email, cancellationToken);
|
|
|
|
[Audit]
|
|
public async Task<Result> UpdateUserStatusAsync(Guid userId, ActivityStatus status, CancellationToken cancellationToken = default) =>
|
|
await service.UpdateUserStatusAsync(userId, status, cancellationToken);
|
|
}
|