添加推送配置缓存

This commit is contained in:
柳文翔 2024-03-15 16:09:27 +08:00
parent 91ad7eebb8
commit 1d80a8de4e
6 changed files with 153 additions and 115 deletions

View File

@ -4,13 +4,6 @@ namespace SJZY.Track.Application.Contract.Services;
public interface ITrackPushCacheService public interface ITrackPushCacheService
{ {
/// <summary>
/// 判断客户是否需要推送
/// </summary>
/// <param name="customId"></param>
/// <returns></returns>
Task<bool> IsPushAsync(int customId);
/// <summary> /// <summary>
/// 获取服务商拉取配置 /// 获取服务商拉取配置
/// </summary> /// </summary>
@ -30,9 +23,11 @@ public interface ITrackPushCacheService
int? trackContentId); int? trackContentId);
/// <summary> /// <summary>
/// 更新缓存 /// 设置推送配置缓存
/// </summary> /// </summary>
/// <param name="customId"></param> /// <param name="id"></param>
/// <param name="isPush"></param> /// <param name="subSystemId">子系统id</param>
Task UpdateAsync(int customId, bool isPush); /// <param name="customId">客户id</param>
/// <returns></returns>
Task<List<TrackPushContentConfigDto>> SetPushContentConfigsAsync(int? id = null, int? subSystemId = null, int? customId = null);
} }

View File

@ -1,4 +1,5 @@
using AutoMapper; using AutoMapper;
using SJZY.Track.Application.Contract.Dtos.ProvideServices;
using SJZY.Track.Application.Contract.Dtos.TrackPush; using SJZY.Track.Application.Contract.Dtos.TrackPush;
using SJZY.Track.Domain.Aggregates.TrackPullAggregate; using SJZY.Track.Domain.Aggregates.TrackPullAggregate;
using SJZY.Track.Domain.Aggregates.TrackPushAggregate; using SJZY.Track.Domain.Aggregates.TrackPushAggregate;
@ -14,5 +15,7 @@ public class TrackPushAutoMapperProfile : Profile
CreateMap<EditTrackPushConfigInput, TrackPushPullParamsConfigEntity>(MemberList.None).Ignore(dest => dest.Id) CreateMap<EditTrackPushConfigInput, TrackPushPullParamsConfigEntity>(MemberList.None).Ignore(dest => dest.Id)
.ForMember(dest => dest.IsPush, opt => opt.MapFrom(source => true)); .ForMember(dest => dest.IsPush, opt => opt.MapFrom(source => true));
CreateMap<EditTrackPushContentInput, TrackPushContentConfigEntity>(MemberList.None); CreateMap<EditTrackPushContentInput, TrackPushContentConfigEntity>(MemberList.None);
CreateMap<TrackPushContentConfigEntity, TrackPushContentConfigDto>(MemberList.None)
.ForMember(dest => dest.OrderType, opt => opt.MapFrom(source => source.OrderNoType));
} }
} }

View File

@ -4,10 +4,11 @@ using SJZY.Track.Application.Contract.Dtos.TrackPull;
using SJZY.Track.Application.Contract.Services; using SJZY.Track.Application.Contract.Services;
using SJZY.Track.Domain.Aggregates.TrackPullAggregate; using SJZY.Track.Domain.Aggregates.TrackPullAggregate;
using SJZY.Track.Domain.Dtos.TrackPull; using SJZY.Track.Domain.Dtos.TrackPull;
using Volo.Abp.DependencyInjection;
namespace SJZY.Track.Application.Services.TrackPull; namespace SJZY.Track.Application.Services.TrackPull;
public class TrackPullCacheService : ApplicationService, ITrackPullCacheService public class TrackPullCacheService : ApplicationService, ITrackPullCacheService, ITransientDependency
{ {
private readonly ITrackPullConfigRepository _trackPullConfigRepository; private readonly ITrackPullConfigRepository _trackPullConfigRepository;
@ -59,6 +60,7 @@ public class TrackPullCacheService : ApplicationService, ITrackPullCacheService
var trackPullConfigEntity = await _trackPullConfigRepository.GetFirstAsync(exp); var trackPullConfigEntity = await _trackPullConfigRepository.GetFirstAsync(exp);
if (trackPullConfigEntity == null) return data; if (trackPullConfigEntity == null) return data;
var trackPullConfigDetail = await _trackPullConfigRepository.GetDetailAsync(trackPullConfigEntity.Id); var trackPullConfigDetail = await _trackPullConfigRepository.GetDetailAsync(trackPullConfigEntity.Id);
if (trackPullConfigDetail is null) return data; if (trackPullConfigDetail is null) return data;
var trackPullContentList = await _trackPullConfigRepository.GetContentListCacheAsync(trackPullConfigEntity.Id); var trackPullContentList = await _trackPullConfigRepository.GetContentListCacheAsync(trackPullConfigEntity.Id);
@ -72,7 +74,7 @@ public class TrackPullCacheService : ApplicationService, ITrackPullCacheService
await RedisHelper.Client.SetAsync( await RedisHelper.Client.SetAsync(
TrackPullContentKey(trackPullConfigEntity.SubSystemId, trackPullConfigEntity.ServiceProviderId), data, TrackPullContentKey(trackPullConfigEntity.SubSystemId, trackPullConfigEntity.ServiceProviderId), data,
86400); (int)TimeSpan.FromDays(1).TotalSeconds);
return data; return data;
} }

View File

@ -212,16 +212,21 @@ public class TrackPullService : ApplicationService, ITransientDependency
ObjectMapper.Map(input, oldTrackPullContentEntity); ObjectMapper.Map(input, oldTrackPullContentEntity);
await _trackPullConfigRepository.UpdateContentAsync(oldTrackPullContentEntity); await _trackPullConfigRepository.UpdateContentAsync(oldTrackPullContentEntity);
await _trackPullCacheService.SetPullConfigAsync(input.TrackPullConfigId); await _trackPullCacheService.SetPullConfigAsync(oldTrackPullContentEntity.TrackPullConfigId);
break; break;
} }
case OptionTypeNoNull.删除: case OptionTypeNoNull.删除:
{
var oldTrackPullContentEntity = await _trackPullConfigRepository.GetContentByIdAsync(input.Id!.Value) ??
throw new BusinessException(message: _stringLocalizer["NotFindData"]);
await _trackPullConfigRepository.SoftDeleteContentAsync(x => x.Id == input.Id); await _trackPullConfigRepository.SoftDeleteContentAsync(x => x.Id == input.Id);
await _trackPullCacheService.SetPullConfigAsync(oldTrackPullContentEntity.TrackPullConfigId);
break; break;
}
default: default:
throw new BusinessException(message: _stringLocalizer[SjzyBasicLocalization.Validation_IsInEnum, throw new BusinessException(message: _stringLocalizer[SjzyBasicLocalization.Validation_IsInEnum,

View File

@ -1,41 +1,78 @@
using SJZY.Expand.ABP.Core; using Microsoft.Extensions.DependencyInjection;
using Nest;
using SJZY.Expand.ABP.Core;
using SJZY.Track.Application.Contract.Dtos.ProvideServices; using SJZY.Track.Application.Contract.Dtos.ProvideServices;
using SJZY.Track.Application.Contract.Services; using SJZY.Track.Application.Contract.Services;
using SJZY.Track.Domain.Aggregates.TrackPullAggregate;
using SJZY.Track.Domain.Aggregates.TrackPushAggregate; using SJZY.Track.Domain.Aggregates.TrackPushAggregate;
using SJZY.Track.Domain.Queries; using SJZY.Track.Domain.Queries;
using SJZY.UserCenter.SharedModel.Dtos.PHP.User;
using System.Linq.Expressions;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
namespace SJZY.Track.Application.Services.TrackPush; namespace SJZY.Track.Application.Services.TrackPush;
public class TrackPushCacheService : ITrackPushCacheService, ITransientDependency public class TrackPushCacheService : ApplicationService, ITrackPushCacheService, ITransientDependency
{ {
private readonly IBasicQuery<TrackPushConfigEntity> _trackPushConfigQuery; private readonly ITrackPushConfigRepository _trackPushConfigRepository;
private string RedisKey(int customId) => $"{GlobalConst.TrackPushKeyPrefix}:{customId}"; private string TrackPushContentKey(int subSystemId, int customId) => $"{GlobalConst.TrackPushKeyPrefix}:{subSystemId}:{customId}";
public TrackPushCacheService(IBasicQuery<TrackPushConfigEntity> trackPushConfigQuery) public TrackPushCacheService(ITrackPushConfigRepository trackPushConfigRepository)
{ {
_trackPushConfigQuery = trackPushConfigQuery; _trackPushConfigRepository = trackPushConfigRepository;
} }
public Task<bool> IsPushAsync(int customId) [NonAction]
public async Task<List<TrackPushContentConfigDto>> GetPushContentConfigsAsync(int subSystemId, int customId)
{ {
throw new NotImplementedException(); var data = await RedisHelper.Client.GetAsync<List<TrackPushContentConfigDto>>(TrackPushContentKey(subSystemId, customId));
data ??= await SetPushContentConfigsAsync(subSystemId: subSystemId, customId: customId);
return data;
} }
public Task<List<TrackPushContentConfigDto>> GetPushContentConfigsAsync(int subSystemId, int customId) [NonAction]
public async Task<TrackPushContentConfigDto?> InferNextPushContentOrNullAsync(int subSystemId, int customId, int? trackContentId)
{ {
throw new NotImplementedException(); var data = await GetPushContentConfigsAsync(subSystemId, customId);
if (trackContentId.HasValue)
{
if (data.Count > 1 && data.Any(x => x.TrackContentId == trackContentId))
{
int index = data.FindIndex(x => x.TrackContentId == trackContentId);
int nextIndex = (index + 1) % data.Count; // 计算下一个索引,处理最后一个元素的情况
return data[nextIndex];
}
else return null;
}
else return data.FirstOrDefault();
} }
public Task<TrackPushContentConfigDto?> InferNextPushContentOrNullAsync(int subSystemId, int customId, [NonAction]
int? trackContentId) public async Task<List<TrackPushContentConfigDto>> SetPushContentConfigsAsync(int? id = null, int? subSystemId = null, int? customId = null)
{ {
throw new NotImplementedException(); var data = new List<TrackPushContentConfigDto>();
} Expression<Func<TrackPushConfigEntity, bool>> exp = x => true;
if (id.HasValue) exp = x => x.Id == id;
else if (subSystemId.HasValue && customId.HasValue)
exp = x => x.SubSystemId == subSystemId && x.CustomId == customId;
public async Task UpdateAsync(int customId, bool isPush) var trackPushConfigEntity = await _trackPushConfigRepository.GetFirstAsync(exp);
{ if (trackPushConfigEntity == null) return data;
await RedisHelper.Client.SetAsync(RedisKey(customId), isPush);
var trackPushContentList = await _trackPushConfigRepository.GetContentListAsync(x => x.TrackPushConfigId == trackPushConfigEntity.Id);
if (trackPushContentList.Count == 0) return data;
trackPushContentList = trackPushContentList.OrderBy(x => x.Sort).ToList();
data = ObjectMapper.Map<List<TrackPushContentConfigEntity>, List<TrackPushContentConfigDto>>(trackPushContentList);
await RedisHelper.Client.SetAsync(TrackPushContentKey(trackPushConfigEntity.SubSystemId, trackPushConfigEntity.CustomId), data, (int)TimeSpan.FromDays(1).TotalSeconds);
return data;
} }
} }

View File

@ -94,8 +94,7 @@ public class TrackPushService : ApplicationService
if (input.Options != OptionTypeNoNull.删除) if (input.Options != OptionTypeNoNull.删除)
{ {
Expression<Func<TrackPushConfigEntity, bool>> isAnyExp = x => Expression<Func<TrackPushConfigEntity, bool>> isAnyExp = x =>
x.SubSystemId == input.SubSystemId && x.CustomId == input.CustomId && x.SubSystemId == input.SubSystemId && x.CustomId == input.CustomId;
x.ServiceProviderId == input.ServiceProviderId;
if (input.Options == OptionTypeNoNull.修改) isAnyExp = isAnyExp.And(x => x.Id != input.Id); if (input.Options == OptionTypeNoNull.修改) isAnyExp = isAnyExp.And(x => x.Id != input.Id);
if (await _trackPushConfigRepository.IsAnyAsync(isAnyExp)) if (await _trackPushConfigRepository.IsAnyAsync(isAnyExp))
@ -117,8 +116,6 @@ public class TrackPushService : ApplicationService
trackPushParamsEntity.PullOrPushConfigId = id; trackPushParamsEntity.PullOrPushConfigId = id;
await _trackPullConfigRepository.InsertParamsAsync(trackPushParamsEntity); await _trackPullConfigRepository.InsertParamsAsync(trackPushParamsEntity);
if (input.IsAutoPush!.Value) await _trackPushCacheService.IsPushAsync(input.CustomId!.Value);
ts.Complete(); ts.Complete();
} }
} }
@ -143,9 +140,6 @@ public class TrackPushService : ApplicationService
await _trackPushConfigRepository.UpdateAsync(oldTrackPushEntity); await _trackPushConfigRepository.UpdateAsync(oldTrackPushEntity);
await _trackPullConfigRepository.UpdateParamsAsync(oldTrackPushParamsEntity); await _trackPullConfigRepository.UpdateParamsAsync(oldTrackPushParamsEntity);
await _trackPushCacheService.UpdateAsync(oldCustomId, false);
if (input.IsAutoPush!.Value) await _trackPushCacheService.IsPushAsync(input.CustomId!.Value);
ts.Complete(); ts.Complete();
} }
} }
@ -162,7 +156,6 @@ public class TrackPushService : ApplicationService
await _trackPullConfigRepository.SoftDeleteParamsAsync(x => await _trackPullConfigRepository.SoftDeleteParamsAsync(x =>
x.PullOrPushConfigId == input.Id && x.IsPush); x.PullOrPushConfigId == input.Id && x.IsPush);
await _trackPushConfigRepository.SoftDeleteContentAsync(x => x.TrackPushConfigId == input.Id); await _trackPushConfigRepository.SoftDeleteContentAsync(x => x.TrackPushConfigId == input.Id);
await _trackPushCacheService.UpdateAsync(oldTrackPushEntity.CustomId, false);
ts.Complete(); ts.Complete();
} }
@ -223,6 +216,7 @@ public class TrackPushService : ApplicationService
entity.Sort = maxSort + 1; entity.Sort = maxSort + 1;
await _trackPushConfigRepository.InsertContentAsync(entity); await _trackPushConfigRepository.InsertContentAsync(entity);
await _trackPushCacheService.SetPushContentConfigsAsync(input.TrackPushConfigId);
break; break;
} }
@ -235,6 +229,7 @@ public class TrackPushService : ApplicationService
ObjectMapper.Map(input, entity); ObjectMapper.Map(input, entity);
await _trackPushConfigRepository.UpdateContentAsync(entity); await _trackPushConfigRepository.UpdateContentAsync(entity);
await _trackPushCacheService.SetPushContentConfigsAsync(entity.TrackPushConfigId);
break; break;
} }
@ -247,13 +242,14 @@ public class TrackPushService : ApplicationService
using (var ts = TransactionScopeHelper.Create()) using (var ts = TransactionScopeHelper.Create())
{ {
await _trackPushConfigRepository.SoftDeleteContentAsync(x => x.Id == input.Id); await _trackPushConfigRepository.SoftDeleteContentAsync(x => x.Id == input.Id);
await _trackPushConfigRepository.UpdateContentAsync(x => x.Sort == x.Sort - 1, await _trackPushConfigRepository.UpdateContentAsync(x => x.Sort == x.Sort - 1,
x => x.TrackPushConfigId == entity.TrackPushConfigId && x.Sort > entity.Sort); x => x.TrackPushConfigId == entity.TrackPushConfigId && x.Sort > entity.Sort);
ts.Complete(); ts.Complete();
} }
await _trackPushCacheService.SetPushContentConfigsAsync(entity.TrackPushConfigId);
break; break;
} }