track/src/TrackCenter.Application/Services/TrackPull/TrackPullCacheService.cs
2025-08-04 17:46:29 +08:00

65 lines
2.4 KiB
C#

using Fake.ObjectMapping;
using TrackCenter.Application.Dtos.TrackPull;
using TrackCenter.Domain.Aggregates.TrackPullAggregate;
using TrackCenter.Domain.Dtos.TrackPull;
namespace TrackCenter.Application.Services.TrackPull;
public class TrackPullCacheService : ITransientDependency
{
private readonly IObjectMapper _objectMapper;
private readonly ITrackPullConfigRepository _trackPullConfigRepository;
public TrackPullCacheService(ITrackPullConfigRepository trackPullConfigRepository, IObjectMapper objectMapper)
{
_trackPullConfigRepository = trackPullConfigRepository;
_objectMapper = objectMapper;
}
public string TrackPullKey(string AppKey, string serviceProviderCode) =>
$"{GlobalConst.TrackPullKeyPrefix}:{AppKey}:{serviceProviderCode}";
public async Task<TrackPullParamsWithContentDto?> GetPullParamsWithContentAsync(string AppKey,
string serviceProviderCode)
{
var data = await RedisHelper.Client.GetAsync<TrackPullParamsWithContentDto>(TrackPullContentKey(
AppKey, serviceProviderCode));
var pullId = await _trackPullConfigRepository.FindIdAsync(AppKey, serviceProviderCode);
data ??= await SetPullConfigAsync(pullId);
return data;
}
public async Task<TrackPullParamsWithContentDto?> SetPullConfigAsync(int id)
{
var trackPullConfigDetail = await _trackPullConfigRepository.GetDetailAsync(id);
if (trackPullConfigDetail == null)
{
return null;
}
var trackPullContentList = await _trackPullConfigRepository.GetContentListCacheAsync(id);
if (trackPullContentList.Count == 0)
{
return null;
}
var data = new TrackPullParamsWithContentDto
{
ParamConfig =
_objectMapper.Map<GetTrackPullConfigDetailOutputVO, TrackPullParamConfigDto>(trackPullConfigDetail),
ContentConfigs = _objectMapper.Map<List<GetTrackPullContentListCacheVO>, List<TrackPullContentConfigDto>>(
trackPullContentList)
};
var key = TrackPullContentKey(trackPullConfigDetail.AppKey, trackPullConfigDetail.ServiceProviderCode);
await RedisHelper.Client.SetAsync(key, data, TimeSpan.FromHours(24));
return data;
}
public static string TrackPullContentKey(string AppKey, string serviceProviderCode) =>
$"{GlobalConst.TrackPullContentKeyPrefix}:{AppKey}:{serviceProviderCode}";
}