fix 增加redis缓存优化速度
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using Dlhis.Basic.Device.Request;
|
||||
using System.Diagnostics;
|
||||
using Dlhis.Basic.Device.Request;
|
||||
using Dlhis.Basic.Device.Response;
|
||||
using Dlhis.Basic.Device.Services;
|
||||
using Dlhis.Basic.Packs;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ using Dlhis.Basic.Summary.Response;
|
||||
using Dlhis.Entity.Basic.Entity;
|
||||
using Dlhis.Entity.Basic.Enums;
|
||||
using Dlhis.Entity.Device.Entities;
|
||||
using Dlhis.Entity.Device.Enums;
|
||||
using Kurisu.SqlSugar.Services;
|
||||
|
||||
namespace Dlhis.Basic.Summary.Services.Implements;
|
||||
@@ -27,7 +28,7 @@ public class SummaryService : ISummaryService
|
||||
var before30Day = DateTime.Today.AddDays(-29);
|
||||
var before7Day = DateTime.Today.AddDays(-6);
|
||||
|
||||
var devices = await _dbContext.Queryable<DeviceEntity>().ToListAsync();
|
||||
var devices = await _dbContext.Queryable<DeviceEntity>().Where(x => x.Type == DeviceTypeEnumType.Attendance || (x.Type == DeviceTypeEnumType.Door && x.AsAttendance)).ToListAsync();
|
||||
var deviceUniqueNos = devices.Select(x => x.UniqueNo).ToList();
|
||||
|
||||
var deviceRegistries = await _dbContext.Queryable<DeviceRegistryEntity>()
|
||||
|
||||
@@ -49,7 +49,6 @@ public class CreateGetAttendanceJob
|
||||
TenantId = x.TenantId,
|
||||
Version = 0,
|
||||
UserNo = string.Empty,
|
||||
UserCode = string.Empty,
|
||||
Type = DeviceTaskEnumType.UploadRecord,
|
||||
|
||||
CreatedBy = 1,
|
||||
|
||||
@@ -43,7 +43,6 @@ namespace Dlhis.ScheduledTask.TaskAssignment.Job
|
||||
TenantId = x.TenantId,
|
||||
Version = 0,
|
||||
UserNo = string.Empty,
|
||||
UserCode = string.Empty,
|
||||
Type = DeviceTaskEnumType.SyncDateTime,
|
||||
|
||||
CreatedBy = 1,
|
||||
|
||||
@@ -14,6 +14,7 @@ using SqlSugar;
|
||||
using Kurisu.SqlSugar.Extensions;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Kurisu.Core.CustomClass;
|
||||
using Kurisu.AspNetCore;
|
||||
|
||||
namespace Dlhis.ScheduledTask.TaskAssignment.Services.Implements;
|
||||
|
||||
@@ -52,7 +53,7 @@ public class AssignmentService : IAssignmentService
|
||||
deivceTamplate.Brand,
|
||||
FuncStr = deivce.Funcs.ToString(),
|
||||
deviceTask.UserNo,
|
||||
deviceTask.UserCode,
|
||||
UserCode = ga.Code,
|
||||
UserName = ga.Name,
|
||||
ga.DepartmentNo,
|
||||
DepartmentName = department.Name,
|
||||
@@ -282,25 +283,21 @@ public class AssignmentService : IAssignmentService
|
||||
{
|
||||
var task = await _dbContext.Queryable<DeviceTaskEntity>().Where(x => x.Id == taskId).FirstAsync();
|
||||
task.ThrowIfNull("该任务不存在");
|
||||
|
||||
(task.Status != DeviceTaskStatusEnumType.Process).ThrowIfTrue($"设备任务下发状态需为{DeviceTaskStatusEnumType.Process.GetDescription()}");
|
||||
|
||||
return Scoped.Request.Value.Create((sp) =>
|
||||
{
|
||||
var faceUrlOptions = sp.GetService<IOptions<FaceUrlOptions>>().Value;
|
||||
var dataProtectionProvider = sp.GetService<IDataProtectionProvider>();
|
||||
var faceUrlOptions = App.GetServiceProvider(true).GetService<IOptions<FaceUrlOptions>>().Value;
|
||||
var dataProtectionProvider = App.GetServiceProvider(true).GetService<IDataProtectionProvider>();
|
||||
|
||||
var timeLimitedDataProtector = dataProtectionProvider
|
||||
.CreateProtector("down:face")
|
||||
.ToTimeLimitedDataProtector();
|
||||
var timeLimitedDataProtector = dataProtectionProvider
|
||||
.CreateProtector("down:face")
|
||||
.ToTimeLimitedDataProtector();
|
||||
|
||||
var userCode = task.UserCode;
|
||||
var userCode = await _dbContext.Queryable<GainfoEntity>().Where(x => x.No == task.UserNo).Select(x => x.Code).SingleAsync();
|
||||
|
||||
var key = $"face:{userCode}";
|
||||
var token = timeLimitedDataProtector.Protect(key, TimeSpan.FromSeconds(faceUrlOptions.Limit));
|
||||
var key = $"face:{userCode}";
|
||||
var token = timeLimitedDataProtector.Protect(key, TimeSpan.FromSeconds(faceUrlOptions.Limit));
|
||||
|
||||
return faceUrlOptions.Url + token;
|
||||
});
|
||||
return faceUrlOptions.Url + token;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,14 +1,10 @@
|
||||
using Dlhis.Entity.Basic.Entity;
|
||||
using Dlhis.Entity.Device.Entities;
|
||||
using Dlhis.Entity.Device.Enums;
|
||||
using Dlhis.ScheduledTask.TaskAssignment.Request;
|
||||
using Dlhis.ScheduledTask.TaskAssignment.Response;
|
||||
using Kurisu.AspNetCore.Utils.Extensions;
|
||||
using Kurisu.Core.DataAccess.Dto;
|
||||
using Kurisu.SqlSugar.Extensions;
|
||||
using Kurisu.SqlSugar.Services;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Dlhis.ScheduledTask.TaskAssignment.Services.Implements;
|
||||
|
||||
@@ -33,14 +29,13 @@ public class DeviceTaskAssignmentService : IDeviceTaskAssignmentService
|
||||
Status = deviceTask.Status,
|
||||
UserNo = deviceTask.UserNo,
|
||||
UserName = ga.Name,
|
||||
UserCode = deviceTask.UserCode,
|
||||
UserCode = ga.Code,
|
||||
Type = deviceTask.Type,
|
||||
LastUpdateTime = deviceTask.ModifiedTime
|
||||
})
|
||||
.ToPageListAsync(request);
|
||||
return page;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user