Skip to content

Commit e95fd5e

Browse files
Merge branch 'master' into merge/release/5.0-to-master\n\nCommit migrated from dotnet/extensions@c202ad3
2 parents 1d4c6bf + 52ae66e commit e95fd5e

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

src/Caching/StackExchangeRedis/src/RedisCache.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class RedisCache : IDistributedCache, IDisposable
3232

3333
private volatile ConnectionMultiplexer _connection;
3434
private IDatabase _cache;
35+
private bool _disposed;
3536

3637
private readonly RedisCacheOptions _options;
3738
private readonly string _instance;
@@ -165,6 +166,7 @@ public void Refresh(string key)
165166

166167
private void Connect()
167168
{
169+
CheckDisposed();
168170
if (_cache != null)
169171
{
170172
return;
@@ -181,7 +183,7 @@ private void Connect()
181183
}
182184
else
183185
{
184-
_connection = ConnectionMultiplexer.Connect(_options.Configuration);
186+
_connection = ConnectionMultiplexer.Connect(_options.Configuration);
185187
}
186188
_cache = _connection.GetDatabase();
187189
}
@@ -194,6 +196,7 @@ private void Connect()
194196

195197
private async Task ConnectAsync(CancellationToken token = default(CancellationToken))
196198
{
199+
CheckDisposed();
197200
token.ThrowIfCancellationRequested();
198201

199202
if (_cache != null)
@@ -212,9 +215,9 @@ private void Connect()
212215
}
213216
else
214217
{
215-
_connection = await ConnectionMultiplexer.ConnectAsync(_options.Configuration).ConfigureAwait(false);
218+
_connection = await ConnectionMultiplexer.ConnectAsync(_options.Configuration).ConfigureAwait(false);
216219
}
217-
220+
218221
_cache = _connection.GetDatabase();
219222
}
220223
}
@@ -431,9 +434,20 @@ private void Refresh(string key, DateTimeOffset? absExpr, TimeSpan? sldExpr)
431434

432435
public void Dispose()
433436
{
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)
435449
{
436-
_connection.Close();
450+
throw new ObjectDisposedException(this.GetType().FullName);
437451
}
438452
}
439453
}

0 commit comments

Comments
 (0)