不得不说,跟大佬们的差距是亿点点啊。
1 private async Task ExpiryScanLoop() 2 { 3 var cancelToken = _scanExpiryCancellationToken!.Token; 4 while (cancelToken.IsCancellationRequested == false) 5 { 6 try 7 { 8 await Task.Delay(_options.ExpiryKeyScanInterval, cancelToken); 9 using var sessionWrap = GetSessionWrap(); 10 using var iter = sessionWrap.Session.Iterate(); 11 while (iter.GetNext(out _) && cancelToken.IsCancellationRequested == false) 12 { 13 var key = iter.GetKey(); 14 var context = new StoreContext<ValueWrapper<TValue>>(); 15 var result = sessionWrap.Session.Read(key, context); 16 if (result.status.IsPending) 17 { 18 sessionWrap.Session.CompletePending(true); 19 context.FinalizeRead(out result.status, out result.output); 20 } 21 22 if (result.status.Found && result.output.HasExpired(_systemClock.NowUnixTimestamp())) 23 { 24 sessionWrap.Session.Delete(key); 25 } 26 } 27 } 28 catch (Exception ex) 29 { 30 _logger?.LogWarning("Exception thrown in expiry scan loop:{Ex}", ex); 31 } 32 } 33 }
这段代码摘抄于时总的FasterKv.Cache.Core.
如果是我,我只会timer然后1s一次的去执行。
补充初始化与结束
...init
private readonly CancellationTokenSource? _scanExpiryCancellationToken;
public TimeSpan ExpiryKeyScanInterval { get; set; } = TimeSpan.FromMinutes(5);
private void Dispose(bool _) { _scanExpiryCancellationToken?.Cancel();...
标签:Session,菜鸟,庆军,iter,var,sessionWrap,result,定期检查,cancelToken From: https://www.cnblogs.com/forhell/p/18084960