62 lines
2.6 KiB
C#
62 lines
2.6 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.Models;
|
|
|
|
namespace PostFundManagement.Application.Services.Users;
|
|
|
|
public sealed class UserCacheService(UserService service, IDistributedCache cache) : ServiceCacheBase, IUserService
|
|
{
|
|
public async ValueTask<Result<User>> GetUserAsync(Guid userId, CancellationToken cancellationToken) =>
|
|
await Result.Try((Func<ValueTask<Result<User>>>)(async () =>
|
|
{
|
|
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 ValueTask<Result<User[]>> GetUsersAsync(Pagination pagination, UserFilterRequest request, CancellationToken cancellationToken = default) =>
|
|
await Result.Try((Func<ValueTask<Result<User[]>>>)(async () =>
|
|
{
|
|
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[]>());
|
|
}));
|
|
|
|
public async ValueTask<Result> SyncUserAsync(Guid userId, string email, CancellationToken cancellationToken = default) =>
|
|
await service.SyncUserAsync(userId, email, cancellationToken);
|
|
|
|
public async ValueTask<Result> UpdateUserStatusAsync(Guid userId, ActivityStatus status, CancellationToken cancellationToken = default) =>
|
|
await service.UpdateUserStatusAsync(userId, status, cancellationToken);
|
|
}
|