Line | |
---|
1 | using System;
|
---|
2 | using System.Threading;
|
---|
3 | using System.Threading.Tasks;
|
---|
4 | using FarmatikoServices.Infrastructure;
|
---|
5 | using Microsoft.Extensions.Hosting;
|
---|
6 |
|
---|
7 | namespace FarmatikoServices.Infrastructure
|
---|
8 | {
|
---|
9 | public class JwtRefreshTokenCache : IHostedService, IDisposable
|
---|
10 | {
|
---|
11 | private Timer _timer;
|
---|
12 | private readonly IJwtAuthManager _jwtAuthManager;
|
---|
13 |
|
---|
14 | public JwtRefreshTokenCache(IJwtAuthManager jwtAuthManager)
|
---|
15 | {
|
---|
16 | _jwtAuthManager = jwtAuthManager;
|
---|
17 | }
|
---|
18 |
|
---|
19 | public Task StartAsync(CancellationToken stoppingToken)
|
---|
20 | {
|
---|
21 | // remove expired refresh tokens from cache every minute
|
---|
22 | _timer = new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromMinutes(1));
|
---|
23 | return Task.CompletedTask;
|
---|
24 | }
|
---|
25 |
|
---|
26 | private void DoWork(object state)
|
---|
27 | {
|
---|
28 | _jwtAuthManager.RemoveExpiredRefreshTokens(DateTime.Now);
|
---|
29 | }
|
---|
30 |
|
---|
31 | public Task StopAsync(CancellationToken stoppingToken)
|
---|
32 | {
|
---|
33 | _timer?.Change(Timeout.Infinite, 0);
|
---|
34 | return Task.CompletedTask;
|
---|
35 | }
|
---|
36 |
|
---|
37 | public void Dispose()
|
---|
38 | {
|
---|
39 | _timer?.Dispose();
|
---|
40 | }
|
---|
41 | }
|
---|
42 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.