项目缓存状态逻辑,全局修改
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
c7fd9a6bcb
commit
3b48dfae31
|
@ -37,23 +37,24 @@ namespace IRaCIS.Application.Services.BackGroundJob
|
||||||
{
|
{
|
||||||
_logger.LogInformation("项目启动 hangfire 任务初始化 执行开始~");
|
_logger.LogInformation("项目启动 hangfire 任务初始化 执行开始~");
|
||||||
|
|
||||||
|
//初始化国际化
|
||||||
//项目状态 立即加载到缓存中
|
|
||||||
await MemoryCacheTrialStatusAsync();
|
|
||||||
|
|
||||||
//await MemoryCacheAnonymizeData();
|
|
||||||
|
|
||||||
|
|
||||||
//创建项目缓存 定时任务
|
|
||||||
HangfireJobHelper.AddOrUpdateInitCronJob<IIRaCISHangfireJob>("RecurringJob_Cache_TrialState", t => t.MemoryCacheTrialStatusAsync(), Cron.Daily());
|
|
||||||
|
|
||||||
//初始化
|
|
||||||
|
|
||||||
await InternationalizationHelper.InitInternationlizationDataAndWatchJsonFileAsync(_internationalizationRepository);
|
await InternationalizationHelper.InitInternationlizationDataAndWatchJsonFileAsync(_internationalizationRepository);
|
||||||
|
|
||||||
//创建邮件定时任务
|
//创建邮件定时任务
|
||||||
await InitSysAndTrialCronJobAsync();
|
await InitSysAndTrialCronJobAsync();
|
||||||
|
|
||||||
|
#region 废弃
|
||||||
|
////项目状态 立即加载到缓存中
|
||||||
|
//await MemoryCacheTrialStatusAsync();
|
||||||
|
|
||||||
|
////await MemoryCacheAnonymizeData();
|
||||||
|
|
||||||
|
|
||||||
|
////创建项目缓存 定时任务
|
||||||
|
//HangfireJobHelper.AddOrUpdateInitCronJob<IIRaCISHangfireJob>("RecurringJob_Cache_TrialState", t => t.MemoryCacheTrialStatusAsync(), Cron.Daily());
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
_logger.LogInformation("项目启动 hangfire 任务初始化 执行结束");
|
_logger.LogInformation("项目启动 hangfire 任务初始化 执行结束");
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@ using Panda.DynamicWebApi.Attributes;
|
||||||
using System.Diagnostics.CodeAnalysis;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using IRaCIS.Core.Domain.Share;
|
using IRaCIS.Core.Domain.Share;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using ZiggyCreatures.Caching.Fusion;
|
||||||
|
|
||||||
namespace IRaCIS.Core.Application
|
namespace IRaCIS.Core.Application
|
||||||
{
|
{
|
||||||
|
@ -32,7 +33,7 @@ namespace IRaCIS.Core.Application
|
||||||
|
|
||||||
public IWebHostEnvironment _hostEnvironment { get; set; }
|
public IWebHostEnvironment _hostEnvironment { get; set; }
|
||||||
|
|
||||||
|
public IFusionCache _fusionCache { get; set; }
|
||||||
|
|
||||||
|
|
||||||
public static IResponseOutput Null404NotFound<TEntity>(TEntity? businessObject) where TEntity : class
|
public static IResponseOutput Null404NotFound<TEntity>(TEntity? businessObject) where TEntity : class
|
||||||
|
@ -60,6 +61,9 @@ namespace IRaCIS.Core.Application
|
||||||
[MemberNotNull(nameof(_hostEnvironment))]
|
[MemberNotNull(nameof(_hostEnvironment))]
|
||||||
public IWebHostEnvironment _hostEnvironment { get; set; }
|
public IWebHostEnvironment _hostEnvironment { get; set; }
|
||||||
|
|
||||||
|
[MemberNotNull(nameof(_fusionCache))]
|
||||||
|
public IFusionCache _fusionCache { get; set; }
|
||||||
|
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
@ -81,7 +85,8 @@ namespace IRaCIS.Core.Application
|
||||||
[MemberNotNull(nameof(_localizer))]
|
[MemberNotNull(nameof(_localizer))]
|
||||||
public IStringLocalizer _localizer { get; set; }
|
public IStringLocalizer _localizer { get; set; }
|
||||||
|
|
||||||
|
[MemberNotNull(nameof(_fusionCache))]
|
||||||
|
public IFusionCache _fusionCache { get; set; }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,6 +102,8 @@ namespace IRaCIS.Core.Application
|
||||||
|
|
||||||
public IStringLocalizer _localizer { get; set; }
|
public IStringLocalizer _localizer { get; set; }
|
||||||
|
|
||||||
|
public IFusionCache _fusionCache { get; set; }
|
||||||
|
|
||||||
public static IResponseOutput Null404NotFound<TEntity>(TEntity? businessObject) where TEntity : class
|
public static IResponseOutput Null404NotFound<TEntity>(TEntity? businessObject) where TEntity : class
|
||||||
{
|
{
|
||||||
return new ResponseOutput<string>()
|
return new ResponseOutput<string>()
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
using EasyCaching.Core;
|
using EasyCaching.Core;
|
||||||
|
using IRaCIS.Core.Application.Helper;
|
||||||
using IRaCIS.Core.Domain.Share;
|
using IRaCIS.Core.Domain.Share;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
@ -19,15 +20,17 @@ namespace IRaCIS.Core.Application.Filter
|
||||||
private readonly IUserInfo _userInfo;
|
private readonly IUserInfo _userInfo;
|
||||||
private readonly IFusionCache _fusionCache;
|
private readonly IFusionCache _fusionCache;
|
||||||
public IStringLocalizer _localizer;
|
public IStringLocalizer _localizer;
|
||||||
|
private readonly IRepository<Trial> _trialRepository;
|
||||||
private readonly List<string> _trialOptList = new List<string>();
|
private readonly List<string> _trialOptList = new List<string>();
|
||||||
|
|
||||||
|
|
||||||
public TrialResourceFilter(IFusionCache fusionCache, IEasyCachingProvider provider, IStringLocalizer localizer , IUserInfo userInfo, string trialOpt = null, string trialOpt2 = null, string trialOpt3 = null)
|
public TrialResourceFilter(IFusionCache fusionCache, IRepository<Trial> trialRepository, IEasyCachingProvider provider, IStringLocalizer localizer, IUserInfo userInfo, string trialOpt = null, string trialOpt2 = null, string trialOpt3 = null)
|
||||||
{
|
{
|
||||||
_fusionCache = fusionCache;
|
_fusionCache = fusionCache;
|
||||||
_provider = provider;
|
_provider = provider;
|
||||||
_userInfo = userInfo;
|
_userInfo = userInfo;
|
||||||
_localizer = localizer;
|
_localizer = localizer;
|
||||||
|
_trialRepository = trialRepository;
|
||||||
|
|
||||||
if (!string.IsNullOrWhiteSpace(trialOpt)) _trialOptList.Add(trialOpt.Trim());
|
if (!string.IsNullOrWhiteSpace(trialOpt)) _trialOptList.Add(trialOpt.Trim());
|
||||||
if (!string.IsNullOrWhiteSpace(trialOpt2)) _trialOptList.Add(trialOpt2.Trim());
|
if (!string.IsNullOrWhiteSpace(trialOpt2)) _trialOptList.Add(trialOpt2.Trim());
|
||||||
|
@ -116,16 +119,16 @@ namespace IRaCIS.Core.Application.Filter
|
||||||
if (matchResult.Success)
|
if (matchResult.Success)
|
||||||
{
|
{
|
||||||
//有可能匹配错误 "trialId":"","documentId":"b8180000-3e2c-0016-9fe0-08da33f96236" 从缓存里面验证下
|
//有可能匹配错误 "trialId":"","documentId":"b8180000-3e2c-0016-9fe0-08da33f96236" 从缓存里面验证下
|
||||||
var cacheResultDic = _provider.GetAll<string>(new[] { matchResult.Value });
|
|
||||||
|
|
||||||
var trialStatusStr = cacheResultDic[matchResult.Value.ToLower()].Value;
|
|
||||||
|
|
||||||
if (!string.IsNullOrWhiteSpace(trialStatusStr))
|
|
||||||
{
|
|
||||||
trialIdStr = matchResult.Value;
|
trialIdStr = matchResult.Value;
|
||||||
|
|
||||||
|
var trialStatusStr = await _fusionCache.GetOrSetAsync(CacheKeys.Trial(trialIdStr), _ => CacheHelper.GetTrialStatusAsync(Guid.Parse(trialIdStr), _trialRepository), TimeSpan.FromDays(7));
|
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(trialStatusStr))
|
||||||
|
{
|
||||||
|
//数据库 检查该项目Id不对
|
||||||
|
context.Result = new JsonResult(ResponseOutput.NotOk(_localizer["TrialResource_ReferTrialIdFailed"]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -143,29 +146,7 @@ namespace IRaCIS.Core.Application.Filter
|
||||||
//通过path 或者body 找到trialId 了
|
//通过path 或者body 找到trialId 了
|
||||||
if (!string.IsNullOrWhiteSpace(trialIdStr))
|
if (!string.IsNullOrWhiteSpace(trialIdStr))
|
||||||
{
|
{
|
||||||
|
var trialStatusStr = await _fusionCache.GetOrSetAsync(CacheKeys.Trial(trialIdStr), _ => CacheHelper.GetTrialStatusAsync(Guid.Parse(trialIdStr), _trialRepository), TimeSpan.FromDays(7));
|
||||||
//如果没缓存数据,可能定时任务没执行或者缓存丢失 在此重新缓存
|
|
||||||
if (_provider.GetCount() == 0)
|
|
||||||
{
|
|
||||||
|
|
||||||
var _trialRepository = context.HttpContext.RequestServices.GetService(typeof(IRepository<Trial>)) as IRepository<Trial>;
|
|
||||||
|
|
||||||
var list = _trialRepository.IfNullThrowException().Select(t => new { TrialId = t.Id, TrialStatusStr = t.TrialStatusStr })
|
|
||||||
.ToList();
|
|
||||||
|
|
||||||
list.ForEach(t => _provider.Set(t.TrialId.ToString(), t.TrialStatusStr, TimeSpan.FromDays(7)));
|
|
||||||
}
|
|
||||||
|
|
||||||
var cacheResultDic = _provider.GetAll<string>(new[] { trialIdStr });
|
|
||||||
|
|
||||||
var trialStatusStr = cacheResultDic[trialIdStr].Value;
|
|
||||||
|
|
||||||
//意外 导致缓存过期,调整服务器时间,测试不想重启程序
|
|
||||||
if (string.IsNullOrWhiteSpace(trialStatusStr))
|
|
||||||
{
|
|
||||||
var trialRepository = context.HttpContext.RequestServices.GetService(typeof(IRepository<Trial>)) as IRepository<Trial>;
|
|
||||||
trialStatusStr = trialRepository?.Where(t => t.Id == Guid.Parse(trialIdStr)).Select(t => t.TrialStatusStr).FirstOrDefault();
|
|
||||||
}
|
|
||||||
|
|
||||||
// 这里是统一拦截 项目有关的操作允许情况(特殊的地方,比如项目配置(有的在多种状态(初始化,ongoing)都可以操作,有的仅仅在Initializing)还有 项目添加和更新,不走这里,特殊处理,不然在这里显得很乱,判断是哪个接口)
|
// 这里是统一拦截 项目有关的操作允许情况(特殊的地方,比如项目配置(有的在多种状态(初始化,ongoing)都可以操作,有的仅仅在Initializing)还有 项目添加和更新,不走这里,特殊处理,不然在这里显得很乱,判断是哪个接口)
|
||||||
if (trialStatusStr == StaticData.TrialState.TrialOngoing || _trialOptList.Any(t => t == TrialOpt.BeforeOngoingCantOpt))
|
if (trialStatusStr == StaticData.TrialState.TrialOngoing || _trialOptList.Any(t => t == TrialOpt.BeforeOngoingCantOpt))
|
||||||
|
|
|
@ -0,0 +1,27 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace IRaCIS.Core.Application.Helper
|
||||||
|
{
|
||||||
|
|
||||||
|
public static class CacheKeys
|
||||||
|
{
|
||||||
|
public static string Trial(string trialIdStr) => $"TrialId:{trialIdStr}";
|
||||||
|
|
||||||
|
|
||||||
|
// 你可以为其他实体和模块定义更多的键
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class CacheHelper
|
||||||
|
{
|
||||||
|
public static async Task<string?> GetTrialStatusAsync(Guid trialId, IRepository<Trial> _trialRepository)
|
||||||
|
{
|
||||||
|
var statusStr = await _trialRepository.Where(t => t.Id == trialId, ignoreQueryFilters: true).Select(t => t.TrialStatusStr).FirstOrDefaultAsync();
|
||||||
|
|
||||||
|
return statusStr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -23,6 +23,8 @@ using IRaCIS.Application.Contracts;
|
||||||
using SixLabors.ImageSharp.Formats.Tiff.Compression.Decompressors;
|
using SixLabors.ImageSharp.Formats.Tiff.Compression.Decompressors;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
|
using IRaCIS.Core.Application.Helper;
|
||||||
|
using ZiggyCreatures.Caching.Fusion;
|
||||||
|
|
||||||
namespace IRaCIS.Core.Application
|
namespace IRaCIS.Core.Application
|
||||||
{
|
{
|
||||||
|
@ -1239,7 +1241,7 @@ namespace IRaCIS.Core.Application
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await _provider.SetAsync(trialId.ToString(), trialStatusStr, TimeSpan.FromDays(7));
|
await _fusionCache.SetAsync(CacheKeys.Trial(trial.Id.ToString()), trialStatusStr, TimeSpan.FromDays(7));
|
||||||
|
|
||||||
await _repository.SaveChangesAsync();
|
await _repository.SaveChangesAsync();
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,8 @@ using Microsoft.AspNetCore.Authorization;
|
||||||
using System.Linq.Expressions;
|
using System.Linq.Expressions;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using IRaCIS.Core.Domain.Models;
|
using IRaCIS.Core.Domain.Models;
|
||||||
|
using IRaCIS.Core.Application.Helper;
|
||||||
|
using ZiggyCreatures.Caching.Fusion;
|
||||||
|
|
||||||
namespace IRaCIS.Application.Services
|
namespace IRaCIS.Application.Services
|
||||||
{
|
{
|
||||||
|
@ -238,8 +240,7 @@ namespace IRaCIS.Application.Services
|
||||||
|
|
||||||
await _repository.AddRangeAsync(needAddBodyPartList, true);
|
await _repository.AddRangeAsync(needAddBodyPartList, true);
|
||||||
|
|
||||||
|
await _fusionCache.SetAsync(CacheKeys.Trial(trial.Id.ToString()), StaticData.TrialState.TrialInitializing, TimeSpan.FromDays(7));
|
||||||
_provider.Set(trial.Id.ToString(), StaticData.TrialState.TrialInitializing, TimeSpan.FromDays(7));
|
|
||||||
|
|
||||||
return ResponseOutput.Ok(trial);
|
return ResponseOutput.Ok(trial);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue