-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_diff.ts
39 lines (34 loc) · 1.04 KB
/
json_diff.ts
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
public class CacheHostedService : IHostedService, IDisposable
{
private readonly ICacheService _cacheService;
private readonly MongoDbService _mongoDbService;
private Timer _timer;
public CacheHostedService(ICacheService cacheService, MongoDbService mongoDbService)
{
_cacheService = cacheService;
_mongoDbService = mongoDbService;
}
public Task StartAsync(CancellationToken cancellationToken)
{
// Update cache every 5 minutes
_timer = new Timer(UpdateCache, null, TimeSpan.Zero, TimeSpan.FromMinutes(5));
return Task.CompletedTask;
}
private async void UpdateCache(object state)
{
var items = await _mongoDbService.GetAllItemsAsync();
foreach (var item in items)
{
_cacheService.SetItem(item.Id, item);
}
}
public Task StopAsync(CancellationToken cancellationToken)
{
_timer?.Change(Timeout.Infinite, 0);
return Task.CompletedTask;
}
public void Dispose()
{
_timer?.Dispose();
}
}