From 66d5a5c4d59dee0eaf4175e32eb109f864841226 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B9=8F=E5=86=9B?= <2357729423@qq.com> Date: Sun, 7 Apr 2024 21:21:21 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9A=E3=80=90=E3=80=90=E8=BD=A8?= =?UTF-8?q?=E8=BF=B9=E8=AE=A2=E5=8D=95=E3=80=91"=E6=98=AF=E5=90=A6?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E6=8E=A8=E9=80=81"=E4=B8=BA=E5=90=A6?= =?UTF-8?q?=EF=BC=8C=E6=96=B0=E5=A2=9E=E8=BD=A8=E8=BF=B9=E6=98=8E=E7=BB=86?= =?UTF-8?q?=E5=90=8E=EF=BC=8C=E5=BC=82=E5=B8=B8=E8=87=AA=E5=8A=A8=E6=8E=A8?= =?UTF-8?q?=E9=80=81=E8=BD=A8=E8=BF=B9=E6=98=8E=E7=BB=86=E3=80=91https://w?= =?UTF-8?q?ww.tapd.cn/60863849/bugtrace/bugs/view=3Fbug=5Fid=3D11608638490?= =?UTF-8?q?01032644?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/ITrackPushCacheService.cs | 18 +++-- .../TrackPush/TrackPushCacheService.cs | 50 ++++++++------ .../Services/TrackPush/TrackPushService.cs | 65 +++++++++++-------- 3 files changed, 76 insertions(+), 57 deletions(-) diff --git a/src/SJZY.Track.Application.Contract/Services/ITrackPushCacheService.cs b/src/SJZY.Track.Application.Contract/Services/ITrackPushCacheService.cs index d04c0fa2..8928ae11 100644 --- a/src/SJZY.Track.Application.Contract/Services/ITrackPushCacheService.cs +++ b/src/SJZY.Track.Application.Contract/Services/ITrackPushCacheService.cs @@ -12,6 +12,14 @@ public interface ITrackPushCacheService /// Task?> GetPushContentConfigsAsync(int subSystemId, int customId); + /// + /// 设置服务商拉取配置缓存 + /// + /// + /// + /// + Task?> SetPushContentConfigsAsync(int subSystemId, int customId); + /// /// 获取服务商拉取配置 /// @@ -33,14 +41,4 @@ public interface ITrackPushCacheService Task IsValidPushAsync(int subSystemId, int customId, string? latestPushTrackCode, string? trackCode); - - /// - /// 设置推送配置缓存 - /// - /// - /// 子系统id - /// 客户id - /// - Task?> SetPushContentConfigsAsync(int? id = null, int? subSystemId = null, - int? customId = null); } \ No newline at end of file diff --git a/src/SJZY.Track.Application/Services/TrackPush/TrackPushCacheService.cs b/src/SJZY.Track.Application/Services/TrackPush/TrackPushCacheService.cs index 88d6b079..bf365f28 100644 --- a/src/SJZY.Track.Application/Services/TrackPush/TrackPushCacheService.cs +++ b/src/SJZY.Track.Application/Services/TrackPush/TrackPushCacheService.cs @@ -1,5 +1,4 @@ -using System.Linq.Expressions; -using SJZY.Expand.ABP.Core; +using SJZY.Expand.ABP.Core; using SJZY.Track.Application.Contract.Dtos.TrackPush; using SJZY.Track.Application.Contract.Services; using SJZY.Track.Domain.Aggregates.TrackPushAggregate; @@ -22,11 +21,23 @@ public class TrackPushCacheService : ITrackPushCacheService, ITransientDependenc public async Task?> GetPushContentConfigsAsync(int subSystemId, int customId) { - var data = await RedisHelper.Client.GetAsync>( - TrackPushContentKey(subSystemId, customId)); + var exists = await RedisHelper.Client.ExistsAsync(TrackPushContentKey(subSystemId, customId)); + if (exists) + { + return await RedisHelper.Client.GetAsync>( + TrackPushContentKey(subSystemId, customId)); + } - data ??= await SetPushContentConfigsAsync(subSystemId: subSystemId, customId: customId); + return await SetPushContentConfigsAsync(subSystemId, customId); + } + public async Task?> SetPushContentConfigsAsync(int subSystemId, int customId) + { + // maybe is null + var data = await DoGetPushContentConfigsAsync(subSystemId, customId); + // ttl 3days + await RedisHelper.Client.SetAsync(TrackPushContentKey(subSystemId, customId), + data, 3 * 24 * 60 * 60); return data; } @@ -58,23 +69,23 @@ public class TrackPushCacheService : ITrackPushCacheService, ITransientDependenc return matched != null && matched.TrackCode == trackCode && matched.IsAutoPush ? matched : default; } - public async Task?> SetPushContentConfigsAsync(int? id = null, - int? subSystemId = null, - int? customId = null) + private async Task?> DoGetPushContentConfigsAsync(int subSystemId, int customId) { - Expression> 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; - - var trackPushConfig = await _trackPushConfigRepository.GetFirstAsync(exp, x => new + 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) { - x.Id, x.IsAutoPush, x.IsAllPush, x.IsCanDelete, x.IsCanUpdate, x.SubSystemId, x.CustomId - }); - if (trackPushConfig == null) return null; + return null; + } var trackPushContentList = await _trackPushConfigRepository.GetContentListAsync(trackPushConfig.Id); - if (trackPushContentList.Count == 0) return null; + if (trackPushContentList.Count == 0) + { + return null; + } // tips:hot path,auto mapper性能不如原生 var res = trackPushContentList.Select(item => new TrackPushContentConfigDto @@ -95,9 +106,6 @@ public class TrackPushCacheService : ITrackPushCacheService, ITransientDependenc ExpandFieldsValue = item.ExpandFieldsValue, }).ToList(); - await RedisHelper.Client.SetAsync(TrackPushContentKey(trackPushConfig.SubSystemId, trackPushConfig.CustomId), - res); - return res; } } \ No newline at end of file diff --git a/src/SJZY.Track.Application/Services/TrackPush/TrackPushService.cs b/src/SJZY.Track.Application/Services/TrackPush/TrackPushService.cs index 5225e8db..f7d69e1d 100644 --- a/src/SJZY.Track.Application/Services/TrackPush/TrackPushService.cs +++ b/src/SJZY.Track.Application/Services/TrackPush/TrackPushService.cs @@ -95,12 +95,17 @@ public class TrackPushService : ApplicationService throw new BusinessException(message: _stringLocalizer["IsTrackPushConfigAny"]); } + using var ts = TransactionScopeHelper.Create(); + switch (input.Options) { case OptionTypeNoNull.新增: { var trackPushEntity = ObjectMapper.Map(input); + await _trackPushConfigRepository.InsertReturnIdentityAsync(trackPushEntity); + await _trackPushCacheService.SetPushContentConfigsAsync(trackPushEntity.SubSystemId, + trackPushEntity.CustomId); } break; @@ -111,21 +116,19 @@ public class TrackPushService : ApplicationService ObjectMapper.Map(input, oldTrackPushEntity); await _trackPushConfigRepository.UpdateAsync(oldTrackPushEntity); - - if (input.IsAutoPush!.Value) await _trackPushCacheService.SetPushContentConfigsAsync(input.Id); + await _trackPushCacheService.SetPushContentConfigsAsync(oldTrackPushEntity.SubSystemId, + oldTrackPushEntity.CustomId); } break; case OptionTypeNoNull.删除: { - _ = await _trackPushConfigRepository.GetFirstAsync(x => x.Id == input.Id) ?? - throw new BusinessException(message: _stringLocalizer["NotFindData"]); + var entity = await _trackPushConfigRepository.GetFirstAsync(x => x.Id == input.Id) ?? + throw new BusinessException(message: _stringLocalizer["NotFindData"]); - using var ts = TransactionScopeHelper.Create(); await _trackPushConfigRepository.SoftDeleteAsync(x => x.Id == input.Id); await _trackPushConfigRepository.SoftDeleteContentAsync(x => x.TrackPushConfigId == input.Id); - - ts.Complete(); + await _trackPushCacheService.SetPushContentConfigsAsync(entity.SubSystemId, entity.CustomId); } break; @@ -134,6 +137,7 @@ public class TrackPushService : ApplicationService nameof(EditTrackPushConfigInput.Options)]); } + ts.Complete(); return true; } @@ -172,6 +176,10 @@ public class TrackPushService : ApplicationService throw new BusinessException(message: _stringLocalizer["IsTrackPushEnContentValueAny"]); } + var trackPushConfigId = input.TrackPushConfigId; + + using var ts = TransactionScopeHelper.Create(); + switch (input.Options) { case OptionTypeNoNull.新增: @@ -181,10 +189,7 @@ public class TrackPushService : ApplicationService var entity = ObjectMapper.Map(input); entity.Sort = maxSort + 1; - await _trackPushConfigRepository.InsertContentAsync(entity); - await _trackPushCacheService.SetPushContentConfigsAsync(input.TrackPushConfigId); - break; } @@ -194,10 +199,7 @@ public class TrackPushService : ApplicationService throw new BusinessException(message: _stringLocalizer["NotFindData"]); ObjectMapper.Map(input, entity); - await _trackPushConfigRepository.UpdateContentAsync(entity); - await _trackPushCacheService.SetPushContentConfigsAsync(entity.TrackPushConfigId); - break; } @@ -205,18 +207,10 @@ public class TrackPushService : ApplicationService { var entity = await _trackPushConfigRepository.GetContentByIdAsync(input.Id!.Value) ?? throw new BusinessException(message: _stringLocalizer["NotFindData"]); - - using (var ts = TransactionScopeHelper.Create()) - { - await _trackPushConfigRepository.SoftDeleteContentAsync(x => x.Id == input.Id); - await _trackPushConfigRepository.UpdateContentAsync(x => x.Sort == x.Sort - 1, - x => x.TrackPushConfigId == entity.TrackPushConfigId && x.Sort > entity.Sort); - - ts.Complete(); - } - - await _trackPushCacheService.SetPushContentConfigsAsync(entity.TrackPushConfigId); - + trackPushConfigId = entity.TrackPushConfigId; + await _trackPushConfigRepository.SoftDeleteContentAsync(x => x.Id == input.Id); + await _trackPushConfigRepository.UpdateContentAsync(x => x.Sort == x.Sort - 1, + x => x.TrackPushConfigId == entity.TrackPushConfigId && x.Sort > entity.Sort); break; } @@ -225,6 +219,14 @@ public class TrackPushService : ApplicationService nameof(EditTrackPushContentInput.Options)]); } + var pushConfig = await _trackPushConfigRepository.GetFirstAsync(x => x.Id == trackPushConfigId); + if (pushConfig != null) + { + await _trackPushCacheService.SetPushContentConfigsAsync(pushConfig.SubSystemId, pushConfig.CustomId); + } + + ts.Complete(); + return true; } @@ -247,8 +249,19 @@ public class TrackPushService : ApplicationService if (trackPushContentEntities.Count > 0) { + using var ts = TransactionScopeHelper.Create(); + await _trackPushConfigRepository.UpdateContentRangeAsync(trackPushContentEntities); - await _trackPushCacheService.SetPushContentConfigsAsync(trackPushContentEntities[0].TrackPushConfigId); + + var pushConfig = + await _trackPushConfigRepository.GetFirstAsync(x => + x.Id == trackPushContentEntities[0].TrackPushConfigId); + if (pushConfig != null) + { + await _trackPushCacheService.SetPushContentConfigsAsync(pushConfig.SubSystemId, pushConfig.CustomId); + } + + ts.Complete(); } return true;