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 GetPullParamsWithContentAsync(string AppKey, string serviceProviderCode) { var data = await RedisHelper.Client.GetAsync(TrackPullContentKey( AppKey, serviceProviderCode)); var pullId = await _trackPullConfigRepository.FindIdAsync(AppKey, serviceProviderCode); data ??= await SetPullConfigAsync(pullId); return data; } public async Task 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(trackPullConfigDetail), ContentConfigs = _objectMapper.Map, List>( 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}"; }