refactor: purge expired sessions on startup
This commit is contained in:
parent
0837092d5f
commit
e21dfd17ff
@ -9,7 +9,7 @@ using ISession = OliverBooth.Data.Web.ISession;
|
|||||||
|
|
||||||
namespace OliverBooth.Services;
|
namespace OliverBooth.Services;
|
||||||
|
|
||||||
internal sealed class SessionService : ISessionService
|
internal sealed class SessionService : BackgroundService, ISessionService
|
||||||
{
|
{
|
||||||
private readonly ILogger<SessionService> _logger;
|
private readonly ILogger<SessionService> _logger;
|
||||||
private readonly IUserService _userService;
|
private readonly IUserService _userService;
|
||||||
@ -171,4 +171,12 @@ internal sealed class SessionService : ISessionService
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
|
{
|
||||||
|
await using WebContext context = await _webContextFactory.CreateDbContextAsync(stoppingToken);
|
||||||
|
context.Sessions.RemoveRange(context.Sessions.Where(s => s.Expires <= DateTimeOffset.UtcNow));
|
||||||
|
await context.SaveChangesAsync(stoppingToken);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user