track/src/SJZY.Track.Application/Services/TrackPull/TrackPullCacheService.cs
xiaolipro d12ef6d350 opt:异常友好提示
change:调整redis缓存过期时间
2024-09-27 16:59:14 +08:00

68 lines
2.6 KiB
C#

using SJZY.Expand.ABP.Core;
using SJZY.Track.Application.Contract.Dtos.TrackPull;
using SJZY.Track.Application.Contract.Services;
using SJZY.Track.Domain.Aggregates.TrackPullAggregate;
using SJZY.Track.Domain.Dtos.TrackPull;
using Volo.Abp.DependencyInjection;
using Volo.Abp.ObjectMapping;
namespace SJZY.Track.Application.Services.TrackPull;
public class TrackPullCacheService : ITransientDependency, ITrackPullCacheService
{
private readonly IObjectMapper _objectMapper;
private readonly ITrackPullConfigRepository _trackPullConfigRepository;
public TrackPullCacheService(ITrackPullConfigRepository trackPullConfigRepository, IObjectMapper objectMapper)
{
_trackPullConfigRepository = trackPullConfigRepository;
_objectMapper = objectMapper;
}
public string TrackPullKey(int subSystemId, string serviceProviderCode) =>
$"{GlobalConst.TrackPullKeyPrefix}:{subSystemId}:{serviceProviderCode}";
public async Task<TrackPullParamsWithContentDto?> GetPullParamsWithContentAsync(int subSystemId,
string serviceProviderCode)
{
var data = await RedisHelper.Client.GetAsync<TrackPullParamsWithContentDto>(TrackPullContentKey(
subSystemId, serviceProviderCode));
var pullId = await _trackPullConfigRepository.FindIdAsync(subSystemId, 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.SubSystemId, trackPullConfigDetail.ServiceProviderCode);
await RedisHelper.Client.SetAsync(key, data, TimeSpan.FromHours(24));
return data;
}
public string TrackPullContentKey(int subSystemId, string serviceProviderCode) =>
$"{GlobalConst.TrackPullContentKeyPrefix}:{subSystemId}:{serviceProviderCode}";
}