fix 优化获取开始id

This commit is contained in:
2025-09-01 15:27:03 +08:00
parent b9b4cf5de3
commit 0678f25f6f
2 changed files with 23 additions and 12 deletions

View File

@@ -19,5 +19,13 @@ public class Startup : DefaultStartup
{ {
base.ConfigureServices(services); base.ConfigureServices(services);
services.AddRedis(); services.AddRedis();
services.AddMemoryCache();
} }
// public override void Configure(IApplicationBuilder app, IWebHostEnvironment env)
// {
// base.Configure(app, env);
// var token = new JipTokenHandler().GetTokenAsync(app.ApplicationServices).GetAwaiter().GetResult();
// Console.WriteLine("token: " + token);
// }
} }

View File

@@ -13,6 +13,7 @@ using Kurisu.AspNetCore.Utils;
using Dlhis.ScheduledTask.TaskAssignment.Dto; using Dlhis.ScheduledTask.TaskAssignment.Dto;
using Kurisu.AspNetCore.CustomClass; using Kurisu.AspNetCore.CustomClass;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services; using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
using Microsoft.Extensions.Caching.Memory;
namespace Dlhis.ScheduledTask.TaskAssignment.Services.Implements; namespace Dlhis.ScheduledTask.TaskAssignment.Services.Implements;
@@ -20,16 +21,19 @@ public class AssignmentService : IAssignmentService
{ {
private readonly IDbContext _dbContext; private readonly IDbContext _dbContext;
private readonly IDataProtectionProvider _dataProtectionProvider; private readonly IDataProtectionProvider _dataProtectionProvider;
private readonly IMemoryCache _memoryCache;
private readonly ILogger<AssignmentService> _logger; private readonly ILogger<AssignmentService> _logger;
private readonly FaceUrlOptions _faceUrlOptions; private readonly FaceUrlOptions _faceUrlOptions;
public AssignmentService(IDbContext dbContext, public AssignmentService(IDbContext dbContext,
IDataProtectionProvider dataProtectionProvider, IDataProtectionProvider dataProtectionProvider,
IOptions<FaceUrlOptions> options, IOptions<FaceUrlOptions> options,
IMemoryCache memoryCache,
ILogger<AssignmentService> logger) ILogger<AssignmentService> logger)
{ {
_dbContext = dbContext; _dbContext = dbContext;
_dataProtectionProvider = dataProtectionProvider; _dataProtectionProvider = dataProtectionProvider;
_memoryCache = memoryCache;
_logger = logger; _logger = logger;
_faceUrlOptions = options.Value; _faceUrlOptions = options.Value;
} }
@@ -241,11 +245,7 @@ public class AssignmentService : IAssignmentService
private async Task<List<TaskDto>> GetDataAsync(DeviceTaskGetRequest request) private async Task<List<TaskDto>> GetDataAsync(DeviceTaskGetRequest request)
{ {
var startId = await GetStartIdAsync(request.DeviceUniqueNo); var startId = await GetStartIdAsync();
if (!startId.HasValue)
{
return new List<TaskDto>();
}
var data = await CommonHelper.RetryAsync(async () => var data = await CommonHelper.RetryAsync(async () =>
{ {
@@ -329,22 +329,25 @@ public class AssignmentService : IAssignmentService
} }
private async Task<long?> GetStartIdAsync(string deviceUniqueNo) private async Task<long> GetStartIdAsync()
{ {
return 74782371; const string cacheKey = "DeviceTaskStartId";
if (_memoryCache.TryGetValue<long>(cacheKey, out var cachedStartId))
{
return cachedStartId;
}
var limitTime = DateTime.Now.AddDays(-3); var limitTime = DateTime.Now.AddDays(-3);
var limitData = await _dbContext.Queryable<DeviceTaskEntity>() var limitData = await _dbContext.Queryable<DeviceTaskEntity>()
.Where(x => x.ModifiedTime >= limitTime) .Where(x => x.ModifiedTime >= limitTime)
.Where(x => x.DeviceUniqueNo == deviceUniqueNo)
.OrderBy(x => x.ModifiedTime) .OrderBy(x => x.ModifiedTime)
.Select(x => new { x.Id }) .Select(x => x.Id)
.FirstAsync(); .FirstAsync();
var startId = limitData?.Id; _memoryCache.Set(cacheKey, limitData, TimeSpan.FromMinutes(30));
return startId; return limitData;
} }