33using EntityDb . Common . Envelopes ;
44using EntityDb . Common . Snapshots ;
55using EntityDb . Common . TypeResolvers ;
6+ using EntityDb . Redis . ConnectionMultiplexers ;
67using EntityDb . Redis . Sessions ;
78using Microsoft . Extensions . DependencyInjection ;
89using Microsoft . Extensions . Options ;
@@ -17,17 +18,16 @@ namespace EntityDb.Redis.Snapshots;
1718internal class RedisSnapshotRepositoryFactory < TSnapshot > : DisposableResourceBaseClass , ISnapshotRepositoryFactory < TSnapshot >
1819{
1920 private readonly IServiceProvider _serviceProvider ;
21+ private readonly ConnectionMultiplexerFactory _connectionMultiplexerFactory ;
2022 private readonly IOptionsFactory < SnapshotSessionOptions > _optionsFactory ;
2123 private readonly IEnvelopeService < JsonElement > _envelopeService ;
2224 private readonly string _connectionString ;
2325 private readonly string _keyNamespace ;
24- private readonly SemaphoreSlim _connectionSemaphore = new ( 1 ) ;
25-
26- private IConnectionMultiplexer ? _connectionMultiplexer ;
2726
2827 public RedisSnapshotRepositoryFactory
2928 (
3029 IServiceProvider serviceProvider ,
30+ ConnectionMultiplexerFactory connectionMultiplexerFactory ,
3131 IOptionsFactory < SnapshotSessionOptions > optionsFactory ,
3232 IEnvelopeService < JsonElement > envelopeService ,
3333 ITypeResolver typeResolver ,
@@ -36,35 +36,17 @@ string keyNamespace
3636 )
3737 {
3838 _serviceProvider = serviceProvider ;
39+ _connectionMultiplexerFactory = connectionMultiplexerFactory ;
3940 _optionsFactory = optionsFactory ;
4041 _envelopeService = envelopeService ;
4142 _connectionString = connectionString ;
4243 _keyNamespace = keyNamespace ;
4344 }
4445
45- private async Task < IConnectionMultiplexer > OpenConnectionMultiplexer ( CancellationToken cancellationToken )
46- {
47- await _connectionSemaphore . WaitAsync ( cancellationToken ) ;
48-
49- if ( _connectionMultiplexer != null )
50- {
51- _connectionSemaphore . Release ( ) ;
52-
53- return _connectionMultiplexer ;
54- }
55-
56- var configurationOptions = ConfigurationOptions . Parse ( _connectionString ) ;
57-
58- _connectionMultiplexer = await ConnectionMultiplexer . ConnectAsync ( configurationOptions ) . WaitAsync ( cancellationToken ) ;
59-
60- _connectionSemaphore . Release ( ) ;
61-
62- return _connectionMultiplexer ;
63- }
6446
6547 private async Task < IRedisSession > CreateSession ( SnapshotSessionOptions snapshotSessionOptions , CancellationToken cancellationToken )
6648 {
67- var connectionMultiplexer = await OpenConnectionMultiplexer ( cancellationToken ) ;
49+ var connectionMultiplexer = await _connectionMultiplexerFactory . CreateConnectionMultiplexer ( _connectionString , cancellationToken ) ;
6850
6951 return RedisSession . Create ( _serviceProvider , connectionMultiplexer . GetDatabase ( ) , snapshotSessionOptions ) ;
7052 }
@@ -95,11 +77,4 @@ public static RedisSnapshotRepositoryFactory<TSnapshot> Create(IServiceProvider
9577 keyNamespace
9678 ) ;
9779 }
98-
99- public override ValueTask DisposeAsync ( )
100- {
101- _connectionMultiplexer ? . Dispose ( ) ;
102-
103- return ValueTask . CompletedTask ;
104- }
10580}
0 commit comments