Skip to content

Commit 9bd143f

Browse files
remove redundant dispose code (#521)
Co-authored-by: Jody Donetti <[email protected]>
1 parent 606ed4d commit 9bd143f

File tree

8 files changed

+75
-147
lines changed

8 files changed

+75
-147
lines changed

src/ZiggyCreatures.FusionCache.Locking.AsyncKeyed/AsyncKeyedMemoryLocker.cs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,17 @@ public void ReleaseLock(string cacheName, string cacheInstanceId, string operati
5252

5353
// IDISPOSABLE
5454
private bool disposedValue;
55-
private void Dispose(bool disposing)
56-
{
57-
if (!disposedValue)
58-
{
59-
if (disposing)
60-
{
61-
_locker?.Dispose();
62-
}
63-
64-
disposedValue = true;
65-
}
66-
}
6755

6856
/// <inheritdoc/>
6957
public void Dispose()
7058
{
71-
Dispose(disposing: true);
72-
GC.SuppressFinalize(this);
59+
if (disposedValue)
60+
{
61+
return;
62+
}
63+
64+
_locker?.Dispose();
65+
66+
disposedValue = true;
7367
}
7468
}

src/ZiggyCreatures.FusionCache.Locking.AsyncKeyed/StripedAsyncKeyedMemoryLocker.cs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,8 @@ public void ReleaseLock(string cacheName, string cacheInstanceId, string operati
4949
}
5050
}
5151

52-
// IDISPOSABLE
53-
private bool _disposedValue;
54-
private void Dispose(bool disposing)
55-
{
56-
if (!_disposedValue)
57-
{
58-
if (disposing)
59-
{
60-
// EMPTY
61-
}
62-
63-
_disposedValue = true;
64-
}
65-
}
66-
6752
/// <inheritdoc/>
6853
public void Dispose()
6954
{
70-
Dispose(disposing: true);
71-
GC.SuppressFinalize(this);
7255
}
7356
}

src/ZiggyCreatures.FusionCache/FusionCache.cs

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,42 +1154,30 @@ internal TValue GetValueFromMemoryEntry<TValue>(string operationId, string key,
11541154
/// <summary>
11551155
/// Release all resources managed by FusionCache.
11561156
/// </summary>
1157-
/// <param name="disposing">Indicates if the disposing is happening.</param>
1158-
private void Dispose(bool disposing)
1159-
//protected virtual void Dispose(bool disposing)
1157+
public void Dispose()
11601158
{
1161-
if (!_disposedValue)
1159+
if (_disposedValue)
11621160
{
1163-
if (disposing)
1164-
{
1165-
RemoveAllPlugins();
1166-
RemoveBackplane();
1167-
RemoveDistributedCache();
1161+
return;
1162+
}
1163+
1164+
RemoveAllPlugins();
1165+
RemoveBackplane();
1166+
RemoveDistributedCache();
11681167

1169-
_autoRecovery?.Dispose();
1170-
_autoRecovery = null;
1168+
_autoRecovery?.Dispose();
1169+
_autoRecovery = null;
11711170

11721171
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
1173-
_memoryLocker.Dispose();
1174-
_memoryLocker = null;
1172+
_memoryLocker.Dispose();
1173+
_memoryLocker = null;
11751174

1176-
_mca.Dispose();
1177-
_mca = null;
1175+
_mca.Dispose();
1176+
_mca = null;
11781177

1179-
_events = null;
1178+
_events = null;
11801179
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
1181-
}
11821180

1183-
_disposedValue = true;
1184-
}
1185-
}
1186-
1187-
/// <summary>
1188-
/// Release all resources managed by FusionCache.
1189-
/// </summary>
1190-
public void Dispose()
1191-
{
1192-
Dispose(true);
1193-
GC.SuppressFinalize(this);
1181+
_disposedValue = true;
11941182
}
11951183
}

src/ZiggyCreatures.FusionCache/Internals/ArrayPoolBufferWriter.cs

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -100,28 +100,15 @@ private static void ThrowInvalidOperationException()
100100
throw new InvalidOperationException("Cannot advance past the end of the buffer.");
101101
}
102102

103-
/// <summary>
104-
/// Returns the buffer to the pool.
105-
/// </summary>
106-
/// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
107-
private void Dispose(bool disposing)
103+
/// <inheritdoc/>
104+
public void Dispose()
108105
{
109-
if (!disposedValue)
106+
if (disposedValue)
110107
{
111-
if (disposing)
112-
{
113-
_arrayPool.Return(_buffer);
114-
}
115-
116-
disposedValue = true;
108+
return;
117109
}
118-
}
119110

120-
/// <inheritdoc/>
121-
public void Dispose()
122-
{
123-
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
124-
Dispose(disposing: true);
125-
GC.SuppressFinalize(this);
111+
_arrayPool.Return(_buffer);
112+
disposedValue = true;
126113
}
127114
}

src/ZiggyCreatures.FusionCache/Internals/AutoRecovery/AutoRecoveryService.cs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -652,25 +652,19 @@ internal async Task BackgroundJobAsync()
652652

653653
// IDISPOSABLE
654654
private bool _disposedValue;
655-
private void Dispose(bool disposing)
655+
656+
public void Dispose()
656657
{
657-
if (!_disposedValue)
658+
if (_disposedValue)
658659
{
659-
if (disposing)
660-
{
661-
_queue.Clear();
662-
_cts?.Cancel();
663-
_cts?.Dispose();
664-
_cts = null;
665-
}
666-
667-
_disposedValue = true;
660+
return;
668661
}
669-
}
670662

671-
public void Dispose()
672-
{
673-
Dispose(disposing: true);
674-
GC.SuppressFinalize(this);
663+
_queue.Clear();
664+
_cts?.Cancel();
665+
_cts?.Dispose();
666+
_cts = null;
667+
668+
_disposedValue = true;
675669
}
676670
}

src/ZiggyCreatures.FusionCache/Locking/ExperimentalMemoryLocker.cs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -116,26 +116,20 @@ public void ReleaseLock(string cacheName, string cacheInstanceId, string operati
116116

117117
// IDISPOSABLE
118118
private bool _disposedValue;
119-
private void Dispose(bool disposing)
119+
120+
/// <inheritdoc/>
121+
public void Dispose()
120122
{
121-
if (!_disposedValue)
123+
if (_disposedValue)
122124
{
123-
if (disposing)
124-
{
125-
_tcsCache.Clear();
126-
}
125+
return;
126+
}
127+
128+
_tcsCache.Clear();
127129

128130
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
129-
_tcsCache = null;
131+
_tcsCache = null;
130132
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
131-
_disposedValue = true;
132-
}
133-
}
134-
135-
/// <inheritdoc/>
136-
public void Dispose()
137-
{
138-
Dispose(disposing: true);
139-
GC.SuppressFinalize(this);
133+
_disposedValue = true;
140134
}
141135
}

src/ZiggyCreatures.FusionCache/Locking/ProbabilisticMemoryLocker.cs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -75,32 +75,26 @@ public void ReleaseLock(string cacheName, string cacheInstanceId, string operati
7575

7676
// IDISPOSABLE
7777
private bool _disposedValue;
78-
private void Dispose(bool disposing)
78+
79+
/// <inheritdoc/>
80+
public void Dispose()
7981
{
80-
if (!_disposedValue)
82+
if (_disposedValue)
83+
{
84+
return;
85+
}
86+
87+
if (_pool is not null)
8188
{
82-
if (disposing)
89+
foreach (var semaphore in _pool)
8390
{
84-
if (_pool is not null)
85-
{
86-
foreach (var semaphore in _pool)
87-
{
88-
semaphore.Dispose();
89-
}
90-
}
91+
semaphore.Dispose();
9192
}
93+
}
9294

9395
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
94-
_pool = null;
96+
_pool = null;
9597
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
96-
_disposedValue = true;
97-
}
98-
}
99-
100-
/// <inheritdoc/>
101-
public void Dispose()
102-
{
103-
Dispose(disposing: true);
104-
GC.SuppressFinalize(this);
98+
_disposedValue = true;
10599
}
106100
}

src/ZiggyCreatures.FusionCache/Locking/StandardMemoryLocker.cs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -120,27 +120,21 @@ public void ReleaseLock(string cacheName, string cacheInstanceId, string operati
120120

121121
// IDISPOSABLE
122122
private bool _disposedValue;
123-
private void Dispose(bool disposing)
123+
124+
/// <inheritdoc/>
125+
public void Dispose()
124126
{
125-
if (!_disposedValue)
127+
if (_disposedValue)
126128
{
127-
if (disposing)
128-
{
129-
_lockCache.Compact(1.0);
130-
_lockCache.Dispose();
131-
}
129+
return;
130+
}
131+
132+
_lockCache.Compact(1.0);
133+
_lockCache.Dispose();
132134

133135
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
134-
_lockCache = null;
136+
_lockCache = null;
135137
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
136-
_disposedValue = true;
137-
}
138-
}
139-
140-
/// <inheritdoc/>
141-
public void Dispose()
142-
{
143-
Dispose(disposing: true);
144-
GC.SuppressFinalize(this);
138+
_disposedValue = true;
145139
}
146140
}

0 commit comments

Comments
 (0)