@@ -6,20 +6,21 @@ namespace BotSharp.Core.Infrastructures;
6
6
7
7
public class DistributedLocker : IDistributedLocker
8
8
{
9
- private readonly IConnectionMultiplexer _redis ;
9
+ private readonly IServiceProvider _services ;
10
10
private readonly ILogger _logger ;
11
11
12
- public DistributedLocker ( IConnectionMultiplexer redis , ILogger < DistributedLocker > logger )
12
+ public DistributedLocker ( IServiceProvider services , ILogger < DistributedLocker > logger )
13
13
{
14
- _redis = redis ;
14
+ _services = services ;
15
15
_logger = logger ;
16
16
}
17
17
18
18
public async Task < bool > LockAsync ( string resource , Func < Task > action , int timeoutInSeconds = 30 )
19
19
{
20
20
var timeout = TimeSpan . FromSeconds ( timeoutInSeconds ) ;
21
21
22
- var @lock = new RedisDistributedLock ( resource , _redis . GetDatabase ( ) ) ;
22
+ var redis = _services . GetRequiredService < IConnectionMultiplexer > ( ) ;
23
+ var @lock = new RedisDistributedLock ( resource , redis . GetDatabase ( ) ) ;
23
24
await using ( var handle = await @lock . TryAcquireAsync ( timeout ) )
24
25
{
25
26
if ( handle == null )
@@ -37,7 +38,8 @@ public bool Lock(string resource, Action action, int timeoutInSeconds = 30)
37
38
{
38
39
var timeout = TimeSpan . FromSeconds ( timeoutInSeconds ) ;
39
40
40
- var @lock = new RedisDistributedLock ( resource , _redis . GetDatabase ( ) ) ;
41
+ var redis = _services . GetRequiredService < IConnectionMultiplexer > ( ) ;
42
+ var @lock = new RedisDistributedLock ( resource , redis . GetDatabase ( ) ) ;
41
43
using ( var handle = @lock . TryAcquire ( timeout ) )
42
44
{
43
45
if ( handle == null )
0 commit comments