track/src/SJZY.Track.Application/Services/TrackPush/TrackPushCacheService.cs

113 lines
4.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using SJZY.Expand.ABP.Core;
using SJZY.Track.Application.Contract.Dtos.TrackPush;
using SJZY.Track.Application.Contract.Services;
using SJZY.Track.Domain.Aggregates.TrackPushAggregate;
using SJZY.Track.Domain.Consts;
using Volo.Abp.DependencyInjection;
namespace SJZY.Track.Application.Services.TrackPush;
public class TrackPushCacheService : ITrackPushCacheService, ITransientDependency
{
private readonly ITrackPushConfigRepository _trackPushConfigRepository;
private string TrackPushContentKey(int subSystemId, int customId) =>
$"{GlobalConst.TrackPushKeyPrefix}:{subSystemId}:{customId}";
public TrackPushCacheService(ITrackPushConfigRepository trackPushConfigRepository)
{
_trackPushConfigRepository = trackPushConfigRepository;
}
public async Task<List<TrackPushContentConfigDto>?> GetPushContentConfigsAsync(int subSystemId, int customId)
{
var exists = await RedisHelper.Client.ExistsAsync(TrackPushContentKey(subSystemId, customId));
if (exists)
{
return await RedisHelper.Client.GetAsync<List<TrackPushContentConfigDto>>(
TrackPushContentKey(subSystemId, customId));
}
return await SetPushContentConfigsAsync(subSystemId, customId);
}
public async Task<List<TrackPushContentConfigDto>?> SetPushContentConfigsAsync(int subSystemId, int customId)
{
// maybe is null
var data = await DoGetPushContentConfigsAsync(subSystemId, customId);
var ttl = 3 * 24 * 60 * 60;
if (data == null) ttl = 5;
// ttl 3days
await RedisHelper.Client.SetAsync(TrackPushContentKey(subSystemId, customId),
data, ttl);
return data;
}
public async Task<TrackPushContentConfigDto?> GetPushContentConfigAsync(int subSystemId, int customId,
string trackConde)
{
var configs = await GetPushContentConfigsAsync(subSystemId, customId);
return configs?.FirstOrDefault(x => x.TrackCode == trackConde);
}
public async Task<TrackPushContentConfigDto?> IsValidPushAsync(int subSystemId, int customId,
string? latestPushTrackCode,
string? trackCode)
{
var cache = await GetPushContentConfigsAsync(subSystemId, customId);
if (cache == null || cache.Count < 1) return default;
var idx = cache.FindIndex(x => x.TrackCode == latestPushTrackCode);
if (idx + 1 >= cache.Count) return default; // 推完了
// find next
var matched = cache[idx + 1].TrackCode == trackCode
? cache[idx + 1]
:
// find swap
cache.FirstOrDefault(x => x.TrackCode == cache[idx + 1].SwapTrackCode);
return matched != null && matched.TrackCode == trackCode && matched.IsAutoPush ? matched : default;
}
private async Task<List<TrackPushContentConfigDto>?> DoGetPushContentConfigsAsync(int subSystemId, int customId)
{
var trackPushConfig = await _trackPushConfigRepository.GetFirstAsync(
x => x.SubSystemId == subSystemId && x.CustomId == customId, x => new
{
x.Id, x.IsAutoPush, x.IsAllPush, x.IsCanDelete, x.IsCanUpdate, x.SubSystemId, x.CustomId
});
if (trackPushConfig == null)
{
return null;
}
var trackPushContentList = await _trackPushConfigRepository.GetContentListAsync(trackPushConfig.Id);
if (trackPushContentList.Count == 0)
{
return null;
}
// tips:hot path,auto mapper性能不如原生
var res = trackPushContentList.Select(item => new TrackPushContentConfigDto
{
IsAutoPush = trackPushConfig.IsAutoPush,
IsCanUpdate = trackPushConfig.IsCanUpdate,
IsAllPush = trackPushConfig.IsAllPush,
IsCanDelete = trackPushConfig.IsCanDelete,
ServiceProviderCode = item.ServiceProviderCode,
OrderType = item.OrderType,
TrackCode = item.TrackCode,
SwapTrackCode = item.SwapTrackCode,
TrackStatusValue = item.TrackStatusValue,
TrackContentCodeValue = item.TrackContentCodeValue,
TrackContentValue = item.TrackContentValue,
TrackEnContentValue = item.TrackEnContentValue,
TrackTimeZone = item.TrackTimeZone,
ExpandFieldsValue = item.ExpandFieldsValue,
}).ToList();
return res;
}
}