Skip to content

Commit c25115e

Browse files
committed
Dispose IDisposables
1 parent 32e1d23 commit c25115e

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

src/EFCoreSecondLevelCacheInterceptor.FusionCache/EFFusionCacheProvider.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using System;
2-
using System.Collections.Generic;
31
using Microsoft.Extensions.Options;
42
using ZiggyCreatures.Caching.Fusion;
53

@@ -8,11 +6,13 @@ namespace EFCoreSecondLevelCacheInterceptor;
86
/// <remarks>
97
/// Using FusionCache as a cache service.
108
/// </remarks>
11-
public class EFFusionCacheProvider : IEFCacheServiceProvider
9+
public sealed class EFFusionCacheProvider : IEFCacheServiceProvider, IDisposable
1210
{
1311
private readonly IFusionCache _fusionCache;
1412
private readonly IEFDebugLogger _logger;
1513

14+
private bool _disposed;
15+
1616
/// <remarks>
1717
/// Using FusionCache as a cache service.
1818
/// </remarks>
@@ -32,6 +32,18 @@ public EFFusionCacheProvider(IFusionCacheProvider fusionCacheProvider,
3232
_logger = logger;
3333
}
3434

35+
/// <inheritdoc />
36+
public void Dispose()
37+
{
38+
if (_disposed)
39+
{
40+
return;
41+
}
42+
43+
_fusionCache.Dispose();
44+
_disposed = true;
45+
}
46+
3547
/// <summary>
3648
/// Adds a new item to the cache.
3749
/// </summary>

src/EFCoreSecondLevelCacheInterceptor.MemoryCache/EFMemoryCacheChangeTokenProvider.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System;
21
using System.Collections.Concurrent;
3-
using System.Threading;
42
using Microsoft.Extensions.Primitives;
53

64
namespace EFCoreSecondLevelCacheInterceptor;
@@ -24,10 +22,13 @@ public EFMemoryCacheChangeTokenProvider()
2422
public IChangeToken GetChangeToken(string key)
2523
=> _changeTokens.GetOrAdd(key, _ =>
2624
{
25+
// We will dispose the CancellationTokenSource when you are done with it in the RemoveChangeToken method.
26+
#pragma warning disable IDISP001
2727
var cancellationTokenSource = new CancellationTokenSource();
2828
var changeToken = new CancellationChangeToken(cancellationTokenSource.Token);
2929

3030
return new ChangeTokenInfo(changeToken, cancellationTokenSource);
31+
#pragma warning restore IDISP001
3132
})
3233
.ChangeToken;
3334

@@ -39,6 +40,11 @@ public void RemoveChangeToken(string key)
3940
if (_changeTokens.TryRemove(key, out var changeTokenInfo))
4041
{
4142
changeTokenInfo.TokenSource.Cancel();
43+
44+
// Our current code (with disposal in RemoveChangeToken) is correct because we create the CancellationTokenSource with new inside our class.
45+
#pragma warning disable IDISP007
46+
changeTokenInfo.TokenSource.Dispose();
47+
#pragma warning restore IDISP007
4248
}
4349
}
4450

0 commit comments

Comments
 (0)