12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- using Hotline.CallCenter.BlackLists;
- using Microsoft.Extensions.DependencyInjection;
- using Microsoft.Extensions.Hosting;
- namespace Hotline.CacheManager
- {
- public class BlacklistManager : BackgroundService
- {
- //private readonly ICacheManager<Blacklist> _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()); };
- }
- /// <summary>
- /// This method is called when the <see cref="T:Microsoft.Extensions.Hosting.IHostedService" /> starts. The implementation should return a task that represents
- /// the lifetime of the long running operation(s) being performed.
- /// </summary>
- /// <param name="stoppingToken">Triggered when <see cref="M:Microsoft.Extensions.Hosting.IHostedService.StopAsync(System.Threading.CancellationToken)" /> is called.</param>
- /// <returns>A <see cref="T:System.Threading.Tasks.Task" /> that represents the long running operations.</returns>
- 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<IBlacklistRepository>();
- 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);
- }
- }
- }
- }
|