fix 增加redis缓存优化速度
This commit is contained in:
@@ -1,23 +1,28 @@
|
||||
using Dlhis.Basic.Device.Request;
|
||||
using System.Diagnostics;
|
||||
using Dlhis.Basic.Device.Request;
|
||||
using Dlhis.Basic.Device.Response;
|
||||
using Dlhis.Entity.Basic.Entity;
|
||||
using Dlhis.Entity.Device.Entities;
|
||||
using Dlhis.Entity.Device.Enums;
|
||||
using Dlhis.Entity.Sys.Entities;
|
||||
using Dlhis.Entity.Sys.Enums;
|
||||
using Kurisu.AspNetCore.Utils;
|
||||
using Kurisu.AspNetCore.Utils.Extensions;
|
||||
using Kurisu.Core.DataAccess.Dto;
|
||||
using Kurisu.SqlSugar.Services;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Dlhis.Basic.Device.Services.Implements;
|
||||
|
||||
public class DevicePermissionService : IDevicePermissionService
|
||||
{
|
||||
private readonly IDbContext _dbContext;
|
||||
private readonly RedisCache _redisCache;
|
||||
|
||||
public DevicePermissionService(IDbContext dbContext)
|
||||
public DevicePermissionService(IDbContext dbContext, RedisCache redisCache)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
_redisCache = redisCache;
|
||||
}
|
||||
|
||||
public async Task AssignAsync(DevicePermissionSaveReuqest reuqest)
|
||||
@@ -58,28 +63,46 @@ public class DevicePermissionService : IDevicePermissionService
|
||||
|
||||
public async Task GenerateDeviceTasksAsync(Func<Task> func, List<string> userNos)
|
||||
{
|
||||
var sw = Stopwatch.StartNew();
|
||||
//获取当前所有人的权限
|
||||
if (userNos.IsEmpty())
|
||||
{
|
||||
userNos = await GetPermissionUserNosAsync();
|
||||
|
||||
var beforeDic = new Dictionary<string, List<Guid>>();
|
||||
if (await _redisCache.KeyExistsAsync("devicePermissions"))
|
||||
{
|
||||
var cache = await _redisCache.StringGetAsync<Dictionary<string, List<Guid>>>("devicePermissions");
|
||||
if (cache?.Any() == true)
|
||||
{
|
||||
beforeDic = cache;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
beforeDic = await GetAssignDeviceCodesInternalAsync(userNos);
|
||||
}
|
||||
|
||||
var beforeDic = await GetAssignDeviceCodesInternalAsync(userNos);
|
||||
await Console.Out.WriteLineAsync($"获取当前所有人的权限耗时:{sw.ElapsedMilliseconds}");
|
||||
|
||||
//获取处理
|
||||
sw.Restart();
|
||||
//业务处理
|
||||
await func();
|
||||
await Console.Out.WriteLineAsync($"业务处理耗时:{sw.ElapsedMilliseconds}");
|
||||
|
||||
sw.Restart();
|
||||
//获取业务处理后所有人的权限
|
||||
var nextUserNos = await GetPermissionUserNosAsync();
|
||||
if (nextUserNos.Any())
|
||||
{
|
||||
userNos = userNos.Concat(nextUserNos).Distinct().ToList();
|
||||
}
|
||||
|
||||
var currentDic = await GetAssignDeviceCodesInternalAsync(userNos);
|
||||
await Console.Out.WriteLineAsync($"获取业务处理后所有人的权限耗时:{sw.ElapsedMilliseconds}");
|
||||
|
||||
sw.Restart();
|
||||
var deivces = await _dbContext.Queryable<DeviceEntity>().ToListAsync();
|
||||
var waitTasks = new List<DeviceTaskEntity>();
|
||||
foreach (var userNo in userNos)
|
||||
{
|
||||
|
||||
beforeDic.TryGetValue(userNo, out var beforeCodes);
|
||||
beforeCodes ??= new List<Guid>();
|
||||
|
||||
@@ -100,14 +123,6 @@ public class DevicePermissionService : IDevicePermissionService
|
||||
deletedItems = deletedItems.Where(x => deviceCodes.Contains(x)).ToList();
|
||||
addItems = addItems.Where(x => deviceCodes.Contains(x)).ToList();
|
||||
|
||||
var userInfo = await _dbContext.Queryable<GainfoEntity>()
|
||||
.Where(ga => ga.No == userNo)
|
||||
.Select(ga => new
|
||||
{
|
||||
ga.Code
|
||||
})
|
||||
.SingleAsync();
|
||||
|
||||
//下发任务创建
|
||||
var toDeleted = deletedItems.Select(x => new DeviceTaskEntity
|
||||
{
|
||||
@@ -116,7 +131,6 @@ public class DevicePermissionService : IDevicePermissionService
|
||||
Type = DeviceTaskEnumType.Delete,
|
||||
UserNo = userNo,
|
||||
Version = 0,
|
||||
UserCode = userInfo.Code,
|
||||
}).ToList();
|
||||
|
||||
var toAdd = addItems.Select(x => new DeviceTaskEntity
|
||||
@@ -126,11 +140,20 @@ public class DevicePermissionService : IDevicePermissionService
|
||||
Type = DeviceTaskEnumType.Add,
|
||||
UserNo = userNo,
|
||||
Version = 0,
|
||||
UserCode = userInfo.Code,
|
||||
}).ToList();
|
||||
|
||||
var tasks = toDeleted.Concat(toAdd).ToList();
|
||||
await _dbContext.InsertAsync(tasks);
|
||||
waitTasks.AddRange(toDeleted.Concat(toAdd).ToList());
|
||||
}
|
||||
|
||||
await Console.Out.WriteLineAsync($"比对任务生成耗时:{sw.ElapsedMilliseconds}");
|
||||
|
||||
if (waitTasks.Any())
|
||||
{
|
||||
sw.Restart();
|
||||
await _redisCache.StringSetAsync("devicePermissions", JsonConvert.SerializeObject(currentDic));
|
||||
await _dbContext.InsertAsync(waitTasks);
|
||||
|
||||
await Console.Out.WriteLineAsync($"任务写入耗时:{sw.ElapsedMilliseconds}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -161,7 +184,6 @@ public class DevicePermissionService : IDevicePermissionService
|
||||
Type = taskType,
|
||||
UserNo = userNo,
|
||||
Version = 0,
|
||||
UserCode = userInfo.Code,
|
||||
}).ToList();
|
||||
|
||||
await _dbContext.InsertAsync(tasks);
|
||||
@@ -358,10 +380,11 @@ public class DevicePermissionService : IDevicePermissionService
|
||||
|
||||
private async Task<Dictionary<string, List<Guid>>> GetAssignDeviceCodesInternalAsync(List<string> userNos)
|
||||
{
|
||||
var sw = Stopwatch.StartNew();
|
||||
var gainfos = await _dbContext.Queryable<GainfoEntity>().Where(x => userNos.Contains(x.No)).ToListAsync();
|
||||
if (!gainfos.Any())
|
||||
{
|
||||
return new Dictionary<string, List<Guid>>();
|
||||
return new Dictionary<string, List<Guid>>(0);
|
||||
}
|
||||
|
||||
var gainfoGroupRelationships = await _dbContext.Queryable<GainfoGroupRelationshipEntity>().ToListAsync();
|
||||
@@ -369,7 +392,7 @@ public class DevicePermissionService : IDevicePermissionService
|
||||
var deviceGroups = await _dbContext.Queryable<DeviceGroupEntity>().ToListAsync();
|
||||
var deviceRelationships = await _dbContext.Queryable<DeviceGroupRelationshipEntity>().ToListAsync();
|
||||
|
||||
var result = new Dictionary<string, List<Guid>>();
|
||||
var result = new Dictionary<string, List<Guid>>(gainfos.Count);
|
||||
|
||||
foreach (var gainfo in gainfos)
|
||||
{
|
||||
@@ -379,6 +402,7 @@ public class DevicePermissionService : IDevicePermissionService
|
||||
result.Add(userNo, codes);
|
||||
}
|
||||
|
||||
await Console.Out.WriteLineAsync($"获取所有人员权限耗时:{sw.ElapsedMilliseconds}");
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user