缓存
This commit is contained in:
parent
7b9f68a308
commit
55e1246e83
@ -34,8 +34,6 @@ namespace YD_AllHeartRates.Api.Mqtt
|
|||||||
private readonly ICaching _caching;
|
private readonly ICaching _caching;
|
||||||
private readonly IServiceScopeFactory _scopeFactory;
|
private readonly IServiceScopeFactory _scopeFactory;
|
||||||
|
|
||||||
private readonly List<StudentDto> _studentList = new();
|
|
||||||
private readonly List<S_Device> _devices = new();
|
|
||||||
private readonly List<HeartRateData> _pendingHeartRates = new();
|
private readonly List<HeartRateData> _pendingHeartRates = new();
|
||||||
|
|
||||||
private DateTime _lastHeartRateSaveTime = DateTime.Now;
|
private DateTime _lastHeartRateSaveTime = DateTime.Now;
|
||||||
@ -60,28 +58,6 @@ namespace YD_AllHeartRates.Api.Mqtt
|
|||||||
_smartSportsContext = smartSportsContext;
|
_smartSportsContext = smartSportsContext;
|
||||||
_caching = caching;
|
_caching = caching;
|
||||||
_scopeFactory = scopeFactory;
|
_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)
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
@ -110,13 +86,47 @@ namespace YD_AllHeartRates.Api.Mqtt
|
|||||||
|
|
||||||
await foreach (var batch in ReadBatchesAsync(stoppingToken))
|
await foreach (var batch in ReadBatchesAsync(stoppingToken))
|
||||||
{
|
{
|
||||||
|
List<StudentDto> studentList;
|
||||||
|
List<S_Device> devices;
|
||||||
|
|
||||||
|
studentList = _caching.Get<List<StudentDto>>(AppSettings.StudentListCacheKey);
|
||||||
|
devices = _caching.Get<List<S_Device>>(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<HeartRateData>();
|
var heartRateEntities = new ConcurrentBag<HeartRateData>();
|
||||||
var jumpRopeEntities = new ConcurrentBag<JumpRopeData>();
|
var jumpRopeEntities = new ConcurrentBag<JumpRopeData>();
|
||||||
|
|
||||||
var deviceHMap = _devices.Where(x => x.DeviceType == 1).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 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 =>
|
Parallel.ForEach(batch, new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount }, msg =>
|
||||||
{
|
{
|
||||||
|
@ -12,6 +12,7 @@ using YD_AllHeartRates.Api.Utilities;
|
|||||||
using YD_AllHeartRates.Commons.Dto;
|
using YD_AllHeartRates.Commons.Dto;
|
||||||
using YD_AllHeartRates.Commons.Dto.Device;
|
using YD_AllHeartRates.Commons.Dto.Device;
|
||||||
using YD_AllHeartRates.Commons.Dto.LargeScreen;
|
using YD_AllHeartRates.Commons.Dto.LargeScreen;
|
||||||
|
using YD_AllHeartRates.Commons.MemoryCaches;
|
||||||
|
|
||||||
namespace YD_AllHeartRates.Api.Services.Impl
|
namespace YD_AllHeartRates.Api.Services.Impl
|
||||||
{
|
{
|
||||||
@ -24,15 +25,17 @@ namespace YD_AllHeartRates.Api.Services.Impl
|
|||||||
public UserContext _userContext;
|
public UserContext _userContext;
|
||||||
private readonly LoginContext _loginContext;
|
private readonly LoginContext _loginContext;
|
||||||
private string schoolCode;
|
private string schoolCode;
|
||||||
|
private readonly ICaching _caching;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 构造
|
/// 构造
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public DeviceService(SmartSportsContext sportsContext, UserContext userContext, LoginContext loginContext)
|
public DeviceService(SmartSportsContext sportsContext, UserContext userContext, LoginContext loginContext, ICaching caching)
|
||||||
{
|
{
|
||||||
_sportsContext = sportsContext;
|
_sportsContext = sportsContext;
|
||||||
_userContext = userContext;
|
_userContext = userContext;
|
||||||
_loginContext = loginContext;
|
_loginContext = loginContext;
|
||||||
|
_caching = caching;
|
||||||
|
|
||||||
schoolCode = _loginContext.SchoolCode;
|
schoolCode = _loginContext.SchoolCode;
|
||||||
}
|
}
|
||||||
@ -169,7 +172,14 @@ namespace YD_AllHeartRates.Api.Services.Impl
|
|||||||
};
|
};
|
||||||
|
|
||||||
await _sportsContext.AddAsync(entity);
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -198,7 +208,14 @@ namespace YD_AllHeartRates.Api.Services.Impl
|
|||||||
device.Name = dto.DeviceType == 1 ? "心率设备" : "跳绳设备";
|
device.Name = dto.DeviceType == 1 ? "心率设备" : "跳绳设备";
|
||||||
|
|
||||||
_sportsContext.Update(device);
|
_sportsContext.Update(device);
|
||||||
return await _sportsContext.SaveChangesAsync() > 0;
|
var res = await _sportsContext.SaveChangesAsync() > 0;
|
||||||
|
|
||||||
|
if (res)
|
||||||
|
{
|
||||||
|
_caching.Remove(AppSettings.DeviceListCacheKey);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -214,7 +231,14 @@ namespace YD_AllHeartRates.Api.Services.Impl
|
|||||||
throw new Exception("更新的设备不存在");
|
throw new Exception("更新的设备不存在");
|
||||||
|
|
||||||
_sportsContext.Device.Remove(device);
|
_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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -14,6 +14,8 @@
|
|||||||
public static string[] CorsUrls { get; set; }
|
public static string[] CorsUrls { get; set; }
|
||||||
public static MqttConfig Mqtt { 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)
|
public static void Init(IConfiguration configuration)
|
||||||
{
|
{
|
||||||
Logging = configuration.GetSection("Logging").Get<LoggingConfig>();
|
Logging = configuration.GetSection("Logging").Get<LoggingConfig>();
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,7 @@ using System.Reflection;
|
|||||||
[assembly: System.Reflection.AssemblyCompanyAttribute("YD_AllHeartRates.Api")]
|
[assembly: System.Reflection.AssemblyCompanyAttribute("YD_AllHeartRates.Api")]
|
||||||
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
|
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
|
||||||
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
|
[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.AssemblyProductAttribute("YD_AllHeartRates.Api")]
|
||||||
[assembly: System.Reflection.AssemblyTitleAttribute("YD_AllHeartRates.Api")]
|
[assembly: System.Reflection.AssemblyTitleAttribute("YD_AllHeartRates.Api")]
|
||||||
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
|
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
|
||||||
|
@ -1 +1 @@
|
|||||||
6784db23a965b9ae2a3dfdaab50ae94c643a7dee1fcca8b9d9cb873fd2636867
|
3e3817ef6a7d7531bf57358bb5d2abdf866abc385932c9d17d07e9797a04f620
|
||||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -436,7 +436,7 @@
|
|||||||
服务实现
|
服务实现
|
||||||
</summary>
|
</summary>
|
||||||
</member>
|
</member>
|
||||||
<member name="M:YD_AllHeartRates.Api.Services.Impl.DeviceService.#ctor(YD_AllHeartRates.Api.Context.SmartSportsContext,YD_AllHeartRates.Api.Context.UserContext,YD_AllHeartRates.Api.Utilities.LoginContext)">
|
<member name="M:YD_AllHeartRates.Api.Services.Impl.DeviceService.#ctor(YD_AllHeartRates.Api.Context.SmartSportsContext,YD_AllHeartRates.Api.Context.UserContext,YD_AllHeartRates.Api.Utilities.LoginContext,YD_AllHeartRates.Commons.MemoryCaches.ICaching)">
|
||||||
<summary>
|
<summary>
|
||||||
构造
|
构造
|
||||||
</summary>
|
</summary>
|
||||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -14,7 +14,7 @@ using System.Reflection;
|
|||||||
[assembly: System.Reflection.AssemblyCompanyAttribute("YD_AllHeartRates.Commons")]
|
[assembly: System.Reflection.AssemblyCompanyAttribute("YD_AllHeartRates.Commons")]
|
||||||
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
|
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
|
||||||
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
|
[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.AssemblyProductAttribute("YD_AllHeartRates.Commons")]
|
||||||
[assembly: System.Reflection.AssemblyTitleAttribute("YD_AllHeartRates.Commons")]
|
[assembly: System.Reflection.AssemblyTitleAttribute("YD_AllHeartRates.Commons")]
|
||||||
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
|
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
|
||||||
|
@ -1 +1 @@
|
|||||||
463029ebc318895442edb6fb3984092082e0f3a1f95e6c3cf59e09e021c6eec2
|
a15d75618d9a5f604592ebd59e64d2979ec0365ce6479e8233cedcc51f5c89ef
|
||||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user