缓存修改

IRC_NewDev
he 2024-09-02 17:30:56 +08:00
parent 5d946d820e
commit 4d2589c112
2 changed files with 45 additions and 23 deletions

View File

@ -31,6 +31,29 @@ namespace IRaCIS.Core.Application.Helper
public static string UserAutoLoginOut(Guid userId) => $"UserAutoLoginOut:{userId}"; public static string UserAutoLoginOut(Guid userId) => $"UserAutoLoginOut:{userId}";
// 你可以为其他实体和模块定义更多的键 // 你可以为其他实体和模块定义更多的键
/// <summary>
/// 跳过阅片
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
public static string SkipReadingCacheKey(Guid userId) => $"{userId}SkipReadingCache";
/// <summary>
/// 开始阅片时间
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
public static string StartReadingTimeKey(Guid userId) => $"{userId}StartReadingTime";
/// <summary>
/// 开始休息时间
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
public static string StartRestTime(Guid userId) => $"{userId}StartRestTime";
} }
public static class CacheHelper public static class CacheHelper

View File

@ -25,6 +25,8 @@ using Microsoft.Extensions.Options;
using System.Linq; using System.Linq;
using NPOI.SS.Formula.Functions; using NPOI.SS.Formula.Functions;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory; using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
using IRaCIS.Core.Application.Helper;
using ZiggyCreatures.Caching.Fusion;
namespace IRaCIS.Application.Services namespace IRaCIS.Application.Services
{ {
@ -54,7 +56,6 @@ namespace IRaCIS.Application.Services
IRepository<OrganInfo> _organInfoRepository, IRepository<OrganInfo> _organInfoRepository,
IRepository<TrialDocument> _trialDocumentRepository, IRepository<TrialDocument> _trialDocumentRepository,
IRepository<User> _userRepository, IRepository<User> _userRepository,
IEasyCachingProvider _provider,
ILuganoCalculateService _luganoCalculateService, ILuganoCalculateService _luganoCalculateService,
IRepository<ReadingCustomTag> _readingCustomTagRepository, IRepository<ReadingCustomTag> _readingCustomTagRepository,
IRepository<ReadingTaskQuestionMark> _readingTaskQuestionMarkRepository, IRepository<ReadingTaskQuestionMark> _readingTaskQuestionMarkRepository,
@ -2656,8 +2657,8 @@ namespace IRaCIS.Application.Services
[HttpPost] [HttpPost]
public async Task<bool> ClearSkipReadingCache() public async Task<bool> ClearSkipReadingCache()
{ {
var clearSkipReadingCacheKey = _userInfo.Id.ToString() + "SkipReadingCache";
_provider.Remove(clearSkipReadingCacheKey); await _fusionCache.RemoveAsync(CacheKeys.SkipReadingCacheKey(_userInfo.Id));
return true; return true;
} }
@ -2670,20 +2671,18 @@ namespace IRaCIS.Application.Services
[HttpPost] [HttpPost]
public async Task<bool> SetSkipReadingCache(SetSkipReadingCacheInDto inDto ) public async Task<bool> SetSkipReadingCache(SetSkipReadingCacheInDto inDto )
{ {
var clearSkipReadingCacheKey = _userInfo.Id.ToString() + "SkipReadingCache"; var clearSkipReadingCache = await _fusionCache.GetOrDefaultAsync<string>(CacheKeys.SkipReadingCacheKey(_userInfo.Id));
var clearSkipReadingCache = _provider.Get<string>(clearSkipReadingCacheKey).Value;
if (clearSkipReadingCache == null|| clearSkipReadingCache==string.Empty) if (clearSkipReadingCache == null|| clearSkipReadingCache==string.Empty)
{ {
List<Guid> cacheIds = new List<Guid>(); List<Guid> cacheIds = new List<Guid>();
cacheIds.Add(inDto.VisitTaskId); cacheIds.Add(inDto.VisitTaskId);
await _fusionCache.SetAsync<string>(CacheKeys.SkipReadingCacheKey(_userInfo.Id), JsonConvert.SerializeObject(cacheIds), TimeSpan.FromHours(24));
_provider.Set(clearSkipReadingCacheKey, JsonConvert.SerializeObject(cacheIds), TimeSpan.FromHours(24));
} }
else else
{ {
List<Guid>? cacheIds=JsonConvert.DeserializeObject<List<Guid>>(clearSkipReadingCache); List<Guid>? cacheIds=JsonConvert.DeserializeObject<List<Guid>>(clearSkipReadingCache);
cacheIds.Add(inDto.VisitTaskId); cacheIds.Add(inDto.VisitTaskId);
_provider.Set(clearSkipReadingCacheKey, JsonConvert.SerializeObject(cacheIds), TimeSpan.FromHours(24)); await _fusionCache.SetAsync<string>(CacheKeys.SkipReadingCacheKey(_userInfo.Id), JsonConvert.SerializeObject(cacheIds), TimeSpan.FromHours(24));
} }
return true; return true;
} }
@ -2714,8 +2713,9 @@ namespace IRaCIS.Application.Services
#region 跳过阅片 #region 跳过阅片
var clearSkipReadingCacheKey = _userInfo.Id.ToString() + "SkipReadingCache"; var clearSkipReadingCache = await _fusionCache.GetOrDefaultAsync<string>(CacheKeys.SkipReadingCacheKey(_userInfo.Id));
var clearSkipReadingCache = _provider.Get<string>(clearSkipReadingCacheKey).Value;
List<Guid> cacheSkipIds = new List<Guid>(); List<Guid> cacheSkipIds = new List<Guid>();
if (clearSkipReadingCache != null && clearSkipReadingCache != string.Empty) if (clearSkipReadingCache != null && clearSkipReadingCache != string.Empty)
{ {
@ -3014,11 +3014,12 @@ namespace IRaCIS.Application.Services
int readingMinute = _verifyConfig.CurrentValue.ContinuousReadingTimeMin; // 为60整数 int readingMinute = _verifyConfig.CurrentValue.ContinuousReadingTimeMin; // 为60整数
int restMinute = _verifyConfig.CurrentValue.ReadingRestTimeMin; // int restMinute = _verifyConfig.CurrentValue.ReadingRestTimeMin; //
var startReadingTime = _provider.Get<string>(startReadingTimeKey).Value; var startReadingTime = await _fusionCache.GetOrDefaultAsync<string>(CacheKeys.StartReadingTimeKey(_userInfo.Id));
var startRestTime = _provider.Get<string>(startRestTimeKey).Value; var startRestTime = await _fusionCache.GetOrDefaultAsync<string>(CacheKeys.StartRestTime(_userInfo.Id));
if (startReadingTime == null && startRestTime == null) if (startReadingTime == null && startRestTime == null)
{ {
_provider.Set(startReadingTimeKey, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), TimeSpan.FromHours(48)); await _fusionCache.SetAsync<string>(CacheKeys.StartReadingTimeKey(_userInfo.Id), DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), TimeSpan.FromHours(48));
} }
else if (startRestTime != null) else if (startRestTime != null)
{ {
@ -3031,8 +3032,8 @@ namespace IRaCIS.Application.Services
else else
{ {
// 休息时间>10分钟 删除休息时间的缓存 记录开始阅片时间 // 休息时间>10分钟 删除休息时间的缓存 记录开始阅片时间
_provider.Remove(startRestTimeKey); await _fusionCache.RemoveAsync(CacheKeys.StartRestTime(_userInfo.Id));
_provider.Set(startReadingTimeKey, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), TimeSpan.FromHours(48)); await _fusionCache.SetAsync<string>(CacheKeys.StartReadingTimeKey(_userInfo.Id), DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), TimeSpan.FromHours(48));
} }
} }
@ -3044,9 +3045,9 @@ namespace IRaCIS.Application.Services
int timespanMin = (DateTime.Now - cacheDate).Minutes; int timespanMin = (DateTime.Now - cacheDate).Minutes;
if (timespanMin > readingMinute) if (timespanMin > readingMinute)
{ {
await _fusionCache.RemoveAsync(CacheKeys.StartReadingTimeKey(_userInfo.Id));
await _fusionCache.SetAsync<string>(CacheKeys.StartRestTime(_userInfo.Id), DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), TimeSpan.FromHours(48));
_provider.Remove(startReadingTimeKey);
_provider.Set(startRestTimeKey, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), TimeSpan.FromHours(48));
throw new BusinessValidationFailedException(_localizer["ReadingImage_NeedRest", readingMinute / 60m, restMinute]); throw new BusinessValidationFailedException(_localizer["ReadingImage_NeedRest", readingMinute / 60m, restMinute]);
} }
@ -3068,25 +3069,23 @@ namespace IRaCIS.Application.Services
userID = _userInfo.Id; userID = _userInfo.Id;
} }
var startReadingTimeKey = userID.ToString() + "StartReadingTime";
var startRestTimeKey = userID.ToString() + "StartRestTime";
//int readingMinute = 120; // 为60整数 //int readingMinute = 120; // 为60整数
int restMinute = 10; // int restMinute = 10; //
var startReadingTime = _provider.Get<string>(startReadingTimeKey).Value; var startReadingTime = await _fusionCache.GetOrDefaultAsync<string>(CacheKeys.StartReadingTimeKey(_userInfo.Id));
var startRestTime = _provider.Get<string>(startRestTimeKey).Value; var startRestTime = await _fusionCache.GetOrDefaultAsync<string>(CacheKeys.StartRestTime(_userInfo.Id));
if (startRestTime != null) if (startRestTime != null)
{ {
var cacheStartRestTime = DateTime.Parse(startRestTime!.ToString()); var cacheStartRestTime = DateTime.Parse(startRestTime!.ToString());
int timespanMin = (DateTime.Now - cacheStartRestTime).Minutes; int timespanMin = (DateTime.Now - cacheStartRestTime).Minutes;
if (timespanMin > restMinute) if (timespanMin > restMinute)
{ {
_provider.Remove(startRestTimeKey); await _fusionCache.RemoveAsync(CacheKeys.StartRestTime(_userInfo.Id));
} }
} }
else if (startReadingTime != null) else if (startReadingTime != null)
{ {
_provider.Set(startReadingTimeKey, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), TimeSpan.FromHours(48)); await _fusionCache.SetAsync<string>(CacheKeys.StartReadingTimeKey(_userInfo.Id), DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), TimeSpan.FromHours(48));
} }
return true; return true;
} }