7 changed files with 182 additions and 80 deletions
@ -0,0 +1,106 @@ |
|||||
|
using System; |
||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.AspNetCore.Authorization; |
||||
|
using Microsoft.AspNetCore.Mvc; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Microsoft.Extensions.Options; |
||||
|
using Volo.Abp.Application.Services; |
||||
|
using Volo.Abp.Caching; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
using Volo.Abp.Domain.Repositories; |
||||
|
using Win_in.Sfs.Basedata.Application.Contracts; |
||||
|
using Win_in.Sfs.Basedata.Domain; |
||||
|
using Win_in.Sfs.Basedata.Domain.Caches; |
||||
|
using Win_in.Sfs.Basedata.Domain.Shared; |
||||
|
|
||||
|
namespace Win_in.Sfs.Basedata.Application; |
||||
|
|
||||
|
public class CycleOptions |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Boms缓存生命周期(秒)
|
||||
|
/// </summary>
|
||||
|
public int BomsCycle { get; set; } |
||||
|
|
||||
|
} |
||||
|
/// <summary>
|
||||
|
/// 提供缓存服务的类
|
||||
|
/// </summary>
|
||||
|
public class CacheService:ISingletonDependency |
||||
|
{ |
||||
|
private readonly IServiceProvider _serviceProvider; |
||||
|
private readonly IOptions<CycleOptions> _options; |
||||
|
public CacheService(IServiceProvider serviceProvider, IOptions<CycleOptions> options) |
||||
|
{ |
||||
|
_serviceProvider = serviceProvider; |
||||
|
_options = options; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 生命周期操作
|
||||
|
/// </summary>
|
||||
|
private async Task BomsCycle() |
||||
|
{ |
||||
|
using var serviceScope = _serviceProvider.CreateScope(); |
||||
|
var repository = serviceScope.ServiceProvider.GetRequiredService<IBomRepository>(); |
||||
|
var reassigner = new Reassigner(DateTime.Now, new TimeSpan(0, 0, _options.Value.BomsCycle==0?600: _options.Value.BomsCycle)); |
||||
|
await reassigner.RunAsync(async () => |
||||
|
{ |
||||
|
Cache.Boms.Clear(); |
||||
|
Cache.Boms = await repository.GetListAsync().ConfigureAwait(false); |
||||
|
|
||||
|
}).ConfigureAwait(false); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 异步开始生命周期操作
|
||||
|
/// </summary>
|
||||
|
public async Task StartAsync() |
||||
|
{ |
||||
|
await BomsCycle().ConfigureAwait(false); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 异步重置BOM(Bill of Materials)生命周期操作
|
||||
|
/// </summary>
|
||||
|
public async Task ResetAsync() |
||||
|
{ |
||||
|
await BomsCycle().ConfigureAwait(false); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
public class Reassigner |
||||
|
{ |
||||
|
private readonly TimeSpan _interval; |
||||
|
private DateTime _lasttime; |
||||
|
public Reassigner(DateTime lasttime, TimeSpan interval) |
||||
|
{ |
||||
|
_lasttime = lasttime; |
||||
|
_interval = interval; |
||||
|
} |
||||
|
|
||||
|
public async Task RunAsync(Action p_action) |
||||
|
{ |
||||
|
while (true) |
||||
|
{ |
||||
|
// 获取当前时间
|
||||
|
var currentTime = DateTime.Now; |
||||
|
// 计算上次重新赋值到现在的时间间隔
|
||||
|
var elapsed = currentTime - _lasttime; |
||||
|
// 检查时间间隔是否满足条件
|
||||
|
if (elapsed >= _interval) |
||||
|
{ |
||||
|
p_action(); |
||||
|
// 重新赋值
|
||||
|
//_value = await GetValueAsync();
|
||||
|
// 更新最后更新时间
|
||||
|
_lasttime = currentTime; |
||||
|
} |
||||
|
|
||||
|
// 等待下一刻钟
|
||||
|
await Task.Delay(_interval).ConfigureAwait(false); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
Loading…
Reference in new issue