@@ -32,6 +32,7 @@ public class RedisCache : IDistributedCache, IDisposable
32
32
33
33
private volatile ConnectionMultiplexer _connection ;
34
34
private IDatabase _cache ;
35
+ private bool _disposed ;
35
36
36
37
private readonly RedisCacheOptions _options ;
37
38
private readonly string _instance ;
@@ -165,6 +166,7 @@ public void Refresh(string key)
165
166
166
167
private void Connect ( )
167
168
{
169
+ CheckDisposed ( ) ;
168
170
if ( _cache != null )
169
171
{
170
172
return ;
@@ -181,7 +183,7 @@ private void Connect()
181
183
}
182
184
else
183
185
{
184
- _connection = ConnectionMultiplexer . Connect ( _options . Configuration ) ;
186
+ _connection = ConnectionMultiplexer . Connect ( _options . Configuration ) ;
185
187
}
186
188
_cache = _connection . GetDatabase ( ) ;
187
189
}
@@ -194,6 +196,7 @@ private void Connect()
194
196
195
197
private async Task ConnectAsync ( CancellationToken token = default ( CancellationToken ) )
196
198
{
199
+ CheckDisposed ( ) ;
197
200
token . ThrowIfCancellationRequested ( ) ;
198
201
199
202
if ( _cache != null )
@@ -212,9 +215,9 @@ private void Connect()
212
215
}
213
216
else
214
217
{
215
- _connection = await ConnectionMultiplexer . ConnectAsync ( _options . Configuration ) . ConfigureAwait ( false ) ;
218
+ _connection = await ConnectionMultiplexer . ConnectAsync ( _options . Configuration ) . ConfigureAwait ( false ) ;
216
219
}
217
-
220
+
218
221
_cache = _connection . GetDatabase ( ) ;
219
222
}
220
223
}
@@ -431,9 +434,20 @@ private void Refresh(string key, DateTimeOffset? absExpr, TimeSpan? sldExpr)
431
434
432
435
public void Dispose ( )
433
436
{
434
- if ( _connection != null )
437
+ if ( _disposed )
438
+ {
439
+ return ;
440
+ }
441
+
442
+ _disposed = true ;
443
+ _connection ? . Close ( ) ;
444
+ }
445
+
446
+ private void CheckDisposed ( )
447
+ {
448
+ if ( _disposed )
435
449
{
436
- _connection . Close ( ) ;
450
+ throw new ObjectDisposedException ( this . GetType ( ) . FullName ) ;
437
451
}
438
452
}
439
453
}
0 commit comments