@@ -7,29 +7,31 @@ public abstract class RedisCacheClientBase : DistributedCacheClientBase
7
7
{
8
8
protected readonly string ? InstanceId ;
9
9
protected static readonly Guid UniquelyIdentifies = Guid . NewGuid ( ) ;
10
- protected readonly ISubscriber Subscriber ;
10
+ protected ISubscriber Subscriber ;
11
11
12
12
protected IDatabase Db
13
13
{
14
14
get
15
15
{
16
- if ( _connection . IsConnected || _connection . IsConnecting )
17
- return _connection . GetDatabase ( ) ;
16
+ EnsureDbConnection ( ) ;
18
17
19
- throw new NotSupportedException ( "Redis service has been disconnected, please wait for reconnection and try again" ) ;
18
+ return _connection . GetDatabase ( ) ;
20
19
}
21
20
}
22
21
23
- private readonly IConnectionMultiplexer _connection ;
22
+ private IConnectionMultiplexer _connection ;
24
23
protected readonly JsonSerializerOptions GlobalJsonSerializerOptions ;
25
24
private readonly CacheEntryOptions _globalCacheEntryOptions ;
26
25
private readonly CacheOptions _globalCacheOptions ;
27
26
27
+ private readonly RedisConfigurationOptions _redisConfigurationOptions ;
28
+
28
29
protected RedisCacheClientBase (
29
30
RedisConfigurationOptions redisConfigurationOptions ,
30
31
JsonSerializerOptions ? jsonSerializerOptions )
31
32
: this ( redisConfigurationOptions . GlobalCacheOptions , redisConfigurationOptions , jsonSerializerOptions )
32
33
{
34
+ _redisConfigurationOptions = redisConfigurationOptions ;
33
35
var redisConfiguration = redisConfigurationOptions . GetAvailableRedisOptions ( ) ;
34
36
_connection = ConnectionMultiplexer . Connect ( redisConfiguration ) ;
35
37
Subscriber = _connection . GetSubscriber ( ) ;
@@ -51,6 +53,22 @@ private RedisCacheClientBase(
51
53
GlobalJsonSerializerOptions = jsonSerializerOptions ?? new JsonSerializerOptions ( ) . EnableDynamicTypes ( ) ;
52
54
}
53
55
56
+ protected void EnsureDbConnection ( )
57
+ {
58
+ if ( ! _connection . IsConnected && ! _connection . IsConnecting )
59
+ {
60
+ // Attempt to reconnect
61
+ var redisConfiguration = _redisConfigurationOptions . GetAvailableRedisOptions ( ) ;
62
+ _connection = ConnectionMultiplexer . Connect ( redisConfiguration ) ;
63
+ Subscriber = _connection . GetSubscriber ( ) ;
64
+ }
65
+
66
+ if ( ! _connection . IsConnected && ! _connection . IsConnecting )
67
+ {
68
+ throw new NotSupportedException ( "Unable to reconnect to Redis, please check the connection settings and try again." ) ;
69
+ }
70
+ }
71
+
54
72
protected T ? ConvertToValue < T > ( RedisValue value , out bool isExist )
55
73
{
56
74
if ( value is { HasValue : true , IsNullOrEmpty : false } )
0 commit comments