diff --git a/YD_AllHeartRates.Api/Mqtt/MqttBackgroundService.cs b/YD_AllHeartRates.Api/Mqtt/MqttBackgroundService.cs index 20867da..d9a8a24 100644 --- a/YD_AllHeartRates.Api/Mqtt/MqttBackgroundService.cs +++ b/YD_AllHeartRates.Api/Mqtt/MqttBackgroundService.cs @@ -34,8 +34,6 @@ namespace YD_AllHeartRates.Api.Mqtt private readonly ICaching _caching; private readonly IServiceScopeFactory _scopeFactory; - private readonly List _studentList = new(); - private readonly List _devices = new(); private readonly List _pendingHeartRates = new(); private DateTime _lastHeartRateSaveTime = DateTime.Now; @@ -60,28 +58,6 @@ namespace YD_AllHeartRates.Api.Mqtt _smartSportsContext = smartSportsContext; _caching = caching; _scopeFactory = scopeFactory; - - _studentList = (from d in _smartSportsContext.Device - join s in _smartSportsContext.Student on d.StudentNo equals s.StudentNo - join c in _smartSportsContext.Class on s.ClassId equals c.Id - where s.StudentStatus == 1 - select new StudentDto - { - SchoolCode = s.SchoolCode, - StudentNo = s.StudentNo, - StudentName = s.StudentName, - Sex = s.Sex, - Age = s.Age, - HeartRateId = d.Code, - JumpRopeId = d.Code, - ClassId = s.ClassId, - ClassName = s.ClassName, - GradeId = c.GradeId, - GradeName = c.GradeName ?? "", - DeviceType = d.DeviceType, - }).ToList(); - - _devices = _smartSportsContext.Device.ToList(); } protected override async Task ExecuteAsync(CancellationToken stoppingToken) @@ -110,13 +86,47 @@ namespace YD_AllHeartRates.Api.Mqtt await foreach (var batch in ReadBatchesAsync(stoppingToken)) { + List studentList; + List devices; + + studentList = _caching.Get>(AppSettings.StudentListCacheKey); + devices = _caching.Get>(AppSettings.DeviceListCacheKey); + + if (studentList == null || devices == null) + { + studentList = (from d in _smartSportsContext.Device + join s in _smartSportsContext.Student on d.StudentNo equals s.StudentNo + join c in _smartSportsContext.Class on s.ClassId equals c.Id + where s.StudentStatus == 1 + select new StudentDto + { + SchoolCode = s.SchoolCode, + StudentNo = s.StudentNo, + StudentName = s.StudentName, + Sex = s.Sex, + Age = s.Age, + HeartRateId = d.Code, + JumpRopeId = d.Code, + ClassId = s.ClassId, + ClassName = s.ClassName, + GradeId = c.GradeId, + GradeName = c.GradeName ?? "", + DeviceType = d.DeviceType, + }).ToList(); + + devices = _smartSportsContext.Device.ToList(); + + _caching.AddObject(AppSettings.StudentListCacheKey, studentList, 28800); + _caching.AddObject(AppSettings.DeviceListCacheKey, devices, 28800); + } + var heartRateEntities = new ConcurrentBag(); var jumpRopeEntities = new ConcurrentBag(); - var deviceHMap = _devices.Where(x => x.DeviceType == 1).ToDictionary(x => x.Code, x => x.StudentNo); - var deviceJMap = _devices.Where(x => x.DeviceType == 2).ToDictionary(x => x.Code, x => x.StudentNo); + var deviceHMap = devices.Where(x => x.DeviceType == 1).ToDictionary(x => x.Code, x => x.StudentNo); + var deviceJMap = devices.Where(x => x.DeviceType == 2).ToDictionary(x => x.Code, x => x.StudentNo); - var studentMap = _studentList.GroupBy(x => x.StudentNo).ToDictionary(g => g.Key, g => g.First()); + var studentMap = studentList.GroupBy(x => x.StudentNo).ToDictionary(g => g.Key, g => g.First()); Parallel.ForEach(batch, new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount }, msg => { diff --git a/YD_AllHeartRates.Api/Services/Impl/DeviceService.cs b/YD_AllHeartRates.Api/Services/Impl/DeviceService.cs index 9d7946f..88ef837 100644 --- a/YD_AllHeartRates.Api/Services/Impl/DeviceService.cs +++ b/YD_AllHeartRates.Api/Services/Impl/DeviceService.cs @@ -12,6 +12,7 @@ using YD_AllHeartRates.Api.Utilities; using YD_AllHeartRates.Commons.Dto; using YD_AllHeartRates.Commons.Dto.Device; using YD_AllHeartRates.Commons.Dto.LargeScreen; +using YD_AllHeartRates.Commons.MemoryCaches; namespace YD_AllHeartRates.Api.Services.Impl { @@ -24,15 +25,17 @@ namespace YD_AllHeartRates.Api.Services.Impl public UserContext _userContext; private readonly LoginContext _loginContext; private string schoolCode; + private readonly ICaching _caching; /// /// 构造 /// - public DeviceService(SmartSportsContext sportsContext, UserContext userContext, LoginContext loginContext) + public DeviceService(SmartSportsContext sportsContext, UserContext userContext, LoginContext loginContext, ICaching caching) { _sportsContext = sportsContext; _userContext = userContext; _loginContext = loginContext; + _caching = caching; schoolCode = _loginContext.SchoolCode; } @@ -169,7 +172,14 @@ namespace YD_AllHeartRates.Api.Services.Impl }; await _sportsContext.AddAsync(entity); - return await _sportsContext.SaveChangesAsync() > 0; + var res = await _sportsContext.SaveChangesAsync() > 0; + + if (res) + { + _caching.Remove(AppSettings.DeviceListCacheKey); + return true; + } + return false; } /// @@ -198,7 +208,14 @@ namespace YD_AllHeartRates.Api.Services.Impl device.Name = dto.DeviceType == 1 ? "心率设备" : "跳绳设备"; _sportsContext.Update(device); - return await _sportsContext.SaveChangesAsync() > 0; + var res = await _sportsContext.SaveChangesAsync() > 0; + + if (res) + { + _caching.Remove(AppSettings.DeviceListCacheKey); + return true; + } + return false; } /// @@ -214,7 +231,14 @@ namespace YD_AllHeartRates.Api.Services.Impl throw new Exception("更新的设备不存在"); _sportsContext.Device.Remove(device); - return await _sportsContext.SaveChangesAsync() > 0; + var res = await _sportsContext.SaveChangesAsync() > 0; + + if (res) + { + _caching.Remove(AppSettings.DeviceListCacheKey); + return true; + } + return false; } /// diff --git a/YD_AllHeartRates.Api/Utilities/AppSettings.cs b/YD_AllHeartRates.Api/Utilities/AppSettings.cs index b026931..b4f1cdb 100644 --- a/YD_AllHeartRates.Api/Utilities/AppSettings.cs +++ b/YD_AllHeartRates.Api/Utilities/AppSettings.cs @@ -14,6 +14,8 @@ public static string[] CorsUrls { get; set; } public static MqttConfig Mqtt { get; set; } + public static string StudentListCacheKey = "student_list"; + public static string DeviceListCacheKey = "device_list"; public static void Init(IConfiguration configuration) { Logging = configuration.GetSection("Logging").Get(); diff --git a/YD_AllHeartRates.Api/XS.cs b/YD_AllHeartRates.Api/XS.cs index 6668b7d..e72cc86 100644 --- a/YD_AllHeartRates.Api/XS.cs +++ b/YD_AllHeartRates.Api/XS.cs @@ -5,6 +5,7 @@ /* 「」 + diff --git a/YD_AllHeartRates.Api/obj/Debug/net6.0/YD_AllHeartRates.Api.AssemblyInfo.cs b/YD_AllHeartRates.Api/obj/Debug/net6.0/YD_AllHeartRates.Api.AssemblyInfo.cs index 9458177..7984990 100644 --- a/YD_AllHeartRates.Api/obj/Debug/net6.0/YD_AllHeartRates.Api.AssemblyInfo.cs +++ b/YD_AllHeartRates.Api/obj/Debug/net6.0/YD_AllHeartRates.Api.AssemblyInfo.cs @@ -14,7 +14,7 @@ using System.Reflection; [assembly: System.Reflection.AssemblyCompanyAttribute("YD_AllHeartRates.Api")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] -[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+c01227dcc153e3533d4428f5edc7c1f1bf5bf552")] +[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+7b9f68a3083ca4505691f4f83599da73c25dca89")] [assembly: System.Reflection.AssemblyProductAttribute("YD_AllHeartRates.Api")] [assembly: System.Reflection.AssemblyTitleAttribute("YD_AllHeartRates.Api")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] diff --git a/YD_AllHeartRates.Api/obj/Debug/net6.0/YD_AllHeartRates.Api.AssemblyInfoInputs.cache b/YD_AllHeartRates.Api/obj/Debug/net6.0/YD_AllHeartRates.Api.AssemblyInfoInputs.cache index c3e37e0..d08631d 100644 --- a/YD_AllHeartRates.Api/obj/Debug/net6.0/YD_AllHeartRates.Api.AssemblyInfoInputs.cache +++ b/YD_AllHeartRates.Api/obj/Debug/net6.0/YD_AllHeartRates.Api.AssemblyInfoInputs.cache @@ -1 +1 @@ -6784db23a965b9ae2a3dfdaab50ae94c643a7dee1fcca8b9d9cb873fd2636867 +3e3817ef6a7d7531bf57358bb5d2abdf866abc385932c9d17d07e9797a04f620 diff --git a/YD_AllHeartRates.Api/obj/Debug/net6.0/YD_AllHeartRates.Api.csproj.AssemblyReference.cache b/YD_AllHeartRates.Api/obj/Debug/net6.0/YD_AllHeartRates.Api.csproj.AssemblyReference.cache index 2bbad4a..f77fa8b 100644 Binary files a/YD_AllHeartRates.Api/obj/Debug/net6.0/YD_AllHeartRates.Api.csproj.AssemblyReference.cache and b/YD_AllHeartRates.Api/obj/Debug/net6.0/YD_AllHeartRates.Api.csproj.AssemblyReference.cache differ diff --git a/YD_AllHeartRates.Api/obj/Debug/net6.0/YD_AllHeartRates.Api.dll b/YD_AllHeartRates.Api/obj/Debug/net6.0/YD_AllHeartRates.Api.dll index 2341820..c60e559 100644 Binary files a/YD_AllHeartRates.Api/obj/Debug/net6.0/YD_AllHeartRates.Api.dll and b/YD_AllHeartRates.Api/obj/Debug/net6.0/YD_AllHeartRates.Api.dll differ diff --git a/YD_AllHeartRates.Api/obj/Debug/net6.0/YD_AllHeartRates.Api.pdb b/YD_AllHeartRates.Api/obj/Debug/net6.0/YD_AllHeartRates.Api.pdb index 22f2f5e..2be4050 100644 Binary files a/YD_AllHeartRates.Api/obj/Debug/net6.0/YD_AllHeartRates.Api.pdb and b/YD_AllHeartRates.Api/obj/Debug/net6.0/YD_AllHeartRates.Api.pdb differ diff --git a/YD_AllHeartRates.Api/obj/Debug/net6.0/YD_AllHeartRates.Api.xml b/YD_AllHeartRates.Api/obj/Debug/net6.0/YD_AllHeartRates.Api.xml index e515709..c8469a4 100644 --- a/YD_AllHeartRates.Api/obj/Debug/net6.0/YD_AllHeartRates.Api.xml +++ b/YD_AllHeartRates.Api/obj/Debug/net6.0/YD_AllHeartRates.Api.xml @@ -436,7 +436,7 @@ 服务实现 - + 构造 diff --git a/YD_AllHeartRates.Api/obj/Debug/net6.0/apphost.exe b/YD_AllHeartRates.Api/obj/Debug/net6.0/apphost.exe index a3e9d8f..2374636 100644 Binary files a/YD_AllHeartRates.Api/obj/Debug/net6.0/apphost.exe and b/YD_AllHeartRates.Api/obj/Debug/net6.0/apphost.exe differ diff --git a/YD_AllHeartRates.Api/obj/Debug/net6.0/ref/YD_AllHeartRates.Api.dll b/YD_AllHeartRates.Api/obj/Debug/net6.0/ref/YD_AllHeartRates.Api.dll index c3d6ddd..b55c6dc 100644 Binary files a/YD_AllHeartRates.Api/obj/Debug/net6.0/ref/YD_AllHeartRates.Api.dll and b/YD_AllHeartRates.Api/obj/Debug/net6.0/ref/YD_AllHeartRates.Api.dll differ diff --git a/YD_AllHeartRates.Api/obj/Debug/net6.0/refint/YD_AllHeartRates.Api.dll b/YD_AllHeartRates.Api/obj/Debug/net6.0/refint/YD_AllHeartRates.Api.dll index c3d6ddd..b55c6dc 100644 Binary files a/YD_AllHeartRates.Api/obj/Debug/net6.0/refint/YD_AllHeartRates.Api.dll and b/YD_AllHeartRates.Api/obj/Debug/net6.0/refint/YD_AllHeartRates.Api.dll differ diff --git a/YD_AllHeartRates.Commons/bin/Debug/net6.0/YD_AllHeartRates.Commons.dll b/YD_AllHeartRates.Commons/bin/Debug/net6.0/YD_AllHeartRates.Commons.dll index 61c0381..6dfe1af 100644 Binary files a/YD_AllHeartRates.Commons/bin/Debug/net6.0/YD_AllHeartRates.Commons.dll and b/YD_AllHeartRates.Commons/bin/Debug/net6.0/YD_AllHeartRates.Commons.dll differ diff --git a/YD_AllHeartRates.Commons/bin/Debug/net6.0/YD_AllHeartRates.Commons.pdb b/YD_AllHeartRates.Commons/bin/Debug/net6.0/YD_AllHeartRates.Commons.pdb index dbb6281..f45bb85 100644 Binary files a/YD_AllHeartRates.Commons/bin/Debug/net6.0/YD_AllHeartRates.Commons.pdb and b/YD_AllHeartRates.Commons/bin/Debug/net6.0/YD_AllHeartRates.Commons.pdb differ diff --git a/YD_AllHeartRates.Commons/obj/Debug/net6.0/YD_AllHeartRates.Commons.AssemblyInfo.cs b/YD_AllHeartRates.Commons/obj/Debug/net6.0/YD_AllHeartRates.Commons.AssemblyInfo.cs index 026b282..6a01eb6 100644 --- a/YD_AllHeartRates.Commons/obj/Debug/net6.0/YD_AllHeartRates.Commons.AssemblyInfo.cs +++ b/YD_AllHeartRates.Commons/obj/Debug/net6.0/YD_AllHeartRates.Commons.AssemblyInfo.cs @@ -14,7 +14,7 @@ using System.Reflection; [assembly: System.Reflection.AssemblyCompanyAttribute("YD_AllHeartRates.Commons")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] -[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+c01227dcc153e3533d4428f5edc7c1f1bf5bf552")] +[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+7b9f68a3083ca4505691f4f83599da73c25dca89")] [assembly: System.Reflection.AssemblyProductAttribute("YD_AllHeartRates.Commons")] [assembly: System.Reflection.AssemblyTitleAttribute("YD_AllHeartRates.Commons")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] diff --git a/YD_AllHeartRates.Commons/obj/Debug/net6.0/YD_AllHeartRates.Commons.AssemblyInfoInputs.cache b/YD_AllHeartRates.Commons/obj/Debug/net6.0/YD_AllHeartRates.Commons.AssemblyInfoInputs.cache index 10bdd2c..8864dc7 100644 --- a/YD_AllHeartRates.Commons/obj/Debug/net6.0/YD_AllHeartRates.Commons.AssemblyInfoInputs.cache +++ b/YD_AllHeartRates.Commons/obj/Debug/net6.0/YD_AllHeartRates.Commons.AssemblyInfoInputs.cache @@ -1 +1 @@ -463029ebc318895442edb6fb3984092082e0f3a1f95e6c3cf59e09e021c6eec2 +a15d75618d9a5f604592ebd59e64d2979ec0365ce6479e8233cedcc51f5c89ef diff --git a/YD_AllHeartRates.Commons/obj/Debug/net6.0/YD_AllHeartRates.Commons.dll b/YD_AllHeartRates.Commons/obj/Debug/net6.0/YD_AllHeartRates.Commons.dll index 61c0381..6dfe1af 100644 Binary files a/YD_AllHeartRates.Commons/obj/Debug/net6.0/YD_AllHeartRates.Commons.dll and b/YD_AllHeartRates.Commons/obj/Debug/net6.0/YD_AllHeartRates.Commons.dll differ diff --git a/YD_AllHeartRates.Commons/obj/Debug/net6.0/YD_AllHeartRates.Commons.pdb b/YD_AllHeartRates.Commons/obj/Debug/net6.0/YD_AllHeartRates.Commons.pdb index dbb6281..f45bb85 100644 Binary files a/YD_AllHeartRates.Commons/obj/Debug/net6.0/YD_AllHeartRates.Commons.pdb and b/YD_AllHeartRates.Commons/obj/Debug/net6.0/YD_AllHeartRates.Commons.pdb differ diff --git a/YD_AllHeartRates.Commons/obj/Debug/net6.0/ref/YD_AllHeartRates.Commons.dll b/YD_AllHeartRates.Commons/obj/Debug/net6.0/ref/YD_AllHeartRates.Commons.dll index 18bcd17..600e518 100644 Binary files a/YD_AllHeartRates.Commons/obj/Debug/net6.0/ref/YD_AllHeartRates.Commons.dll and b/YD_AllHeartRates.Commons/obj/Debug/net6.0/ref/YD_AllHeartRates.Commons.dll differ diff --git a/YD_AllHeartRates.Commons/obj/Debug/net6.0/refint/YD_AllHeartRates.Commons.dll b/YD_AllHeartRates.Commons/obj/Debug/net6.0/refint/YD_AllHeartRates.Commons.dll index 18bcd17..600e518 100644 Binary files a/YD_AllHeartRates.Commons/obj/Debug/net6.0/refint/YD_AllHeartRates.Commons.dll and b/YD_AllHeartRates.Commons/obj/Debug/net6.0/refint/YD_AllHeartRates.Commons.dll differ