using Hotline.CallCenter.BlackLists; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; namespace Hotline.CacheManager { public class BlacklistManager : BackgroundService { //private readonly ICacheManager _cacheManager; private readonly IServiceScopeFactory _serviceScopeFactory; public BlacklistManager(IServiceScopeFactory serviceScopeFactory) { _serviceScopeFactory = serviceScopeFactory; //cacheManager.OnAdd += (sender, args) => { Console.WriteLine(args.ToString()); }; //cacheManager.OnRemove += (sender, args) => { Console.WriteLine(args.ToString()); }; //cacheManager.OnClear += (sender, args) => { Console.WriteLine(args.ToString()); }; //cacheManager.OnRemoveByHandle += (sender, args) => { Console.WriteLine(args.ToString()); }; //cacheManager.OnPut += (sender, args) => { Console.WriteLine(args.ToString()); }; //cacheManager.OnUpdate += (sender, args) => { Console.WriteLine(args.ToString()); }; } /// /// This method is called when the starts. The implementation should return a task that represents /// the lifetime of the long running operation(s) being performed. /// /// Triggered when is called. /// A that represents the long running operations. protected override async Task ExecuteAsync(CancellationToken stoppingToken) { var time = TimeSpan.FromMinutes(5); await Task.Delay(time, stoppingToken); while (!stoppingToken.IsCancellationRequested) { using var scope = _serviceScopeFactory.CreateScope(); var blacklistRepository = scope.ServiceProvider.GetService(); var expiredBlackListItems = await blacklistRepository!.QueryAsync(d => d.Expired <= DateTime.Now); foreach (var blacklistItem in expiredBlackListItems) { await blacklistRepository.RemoveAsync(blacklistItem, true, stoppingToken); } await Task.Delay(time, stoppingToken); } } } }