forked from perrich/Hangfire.MemoryStorage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpirationManager.cs
79 lines (65 loc) · 2.33 KB
/
ExpirationManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System;
using System.Linq;
using System.Threading;
using Hangfire.MemoryStorage.Database;
using Hangfire.MemoryStorage.Dto;
using Hangfire.Server;
using System.Collections.Generic;
namespace Hangfire.MemoryStorage
{
public class ExpirationManager : IServerComponent
{
private const int NumberOfRecordsInSinglePass = 1000;
private static readonly TimeSpan DelayBetweenPasses = TimeSpan.FromSeconds(1);
private static readonly Type[] Types =
{
typeof (AggregatedCounterDto),
typeof (JobDto),
typeof (ListDto),
typeof (SetDto),
typeof (HashDto)
};
private readonly TimeSpan _checkInterval;
public ExpirationManager(TimeSpan checkInterval)
{
_checkInterval = checkInterval;
}
public void Execute(CancellationToken cancellationToken)
{
var now = DateTime.UtcNow;
foreach (var t in Types)
{
if (!typeof(IExpirable).IsAssignableFrom(t))
continue;
int removedCount;
do
{
var table = Data.GetEnumeration(t);
var data = (from d in table
where ((IExpirable)d).ExpireAt < now
select d).Take(NumberOfRecordsInSinglePass).ToList();
removedCount = data.Count;
if (removedCount == 0)
{
continue;
}
if (typeof(IIdentifiedData<int>).IsAssignableFrom(t))
{
Data.Delete(data.Cast<IIdentifiedData<int>>());
}
else if (typeof(IIdentifiedData<string>).IsAssignableFrom(t))
{
Data.Delete(data.Cast<IIdentifiedData<string>>());
}
cancellationToken.WaitHandle.WaitOne(DelayBetweenPasses);
cancellationToken.ThrowIfCancellationRequested();
} while (removedCount != 0);
}
cancellationToken.WaitHandle.WaitOne(_checkInterval);
}
public override string ToString()
{
return "Composite C1 Records Expiration Manager";
}
}
}