Skip to content

Commit 68b3f80

Browse files
committed
misc improvements
1 parent 1d4e48a commit 68b3f80

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

src/Abstract/IHttpClientCache.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ public interface IHttpClientCache : IAsyncDisposable, IDisposable
5959
/// Should be used if the component using <see cref="IHttpClientCache"/> is disposed (unless the entire app is being disposed). Includes disposal of the <see cref="HttpClient"/>.
6060
/// </summary>
6161
/// <param name="id">The cache key</param>
62-
ValueTask Remove(string id);
62+
/// <param name="cancellationToken"></param>
63+
ValueTask Remove(string id, CancellationToken cancellationToken = default);
6364

64-
/// <inheritdoc cref="Remove(string)"/>
65-
void RemoveSync(string id);
65+
/// <inheritdoc cref="Remove(string,CancellationToken)"/>
66+
void RemoveSync(string id, CancellationToken cancellationToken = default);
6667
}

src/HandlerKey.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
namespace Soenneker.Utils.HttpClientCache
22
{
33
internal readonly record struct HandlerKey(double LifetimeSeconds, int MaxConnections, bool UseCookies);
4-
}
4+
}

src/HttpClientCache.cs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
namespace Soenneker.Utils.HttpClientCache;
1717

1818
///<inheritdoc cref="IHttpClientCache"/>
19-
public class HttpClientCache : IHttpClientCache
19+
public sealed class HttpClientCache : IHttpClientCache
2020
{
2121
private readonly SingletonDictionary<HttpClient> _httpClients;
2222

@@ -145,39 +145,38 @@ private static void AddDefaultRequestHeaders(HttpClient httpClient, Dictionary<s
145145
}
146146
}
147147

148-
public ValueTask Remove(string id)
148+
public ValueTask Remove(string id, CancellationToken cancellationToken = default)
149149
{
150-
return _httpClients.Remove(id);
150+
return _httpClients.Remove(id, cancellationToken);
151151
}
152152

153-
public void RemoveSync(string id)
153+
public void RemoveSync(string id, CancellationToken cancellationToken = default)
154154
{
155-
_httpClients.RemoveSync(id);
155+
_httpClients.RemoveSync(id, cancellationToken);
156156
}
157157

158-
public ValueTask DisposeAsync()
158+
private void DisposeHandlers()
159159
{
160-
GC.SuppressFinalize(this);
161-
162160
foreach (KeyValuePair<HandlerKey, SocketsHttpHandler> kvp in _handlers.ToArray())
163161
{
164162
if (_handlers.TryRemove(kvp.Key, out SocketsHttpHandler? handler))
165163
handler.Dispose();
166164
}
165+
}
166+
167+
public ValueTask DisposeAsync()
168+
{
169+
GC.SuppressFinalize(this);
167170

171+
DisposeHandlers();
168172
return _httpClients.DisposeAsync();
169173
}
170174

171175
public void Dispose()
172176
{
173177
GC.SuppressFinalize(this);
174178

175-
foreach (KeyValuePair<HandlerKey, SocketsHttpHandler> kvp in _handlers.ToArray())
176-
{
177-
if (_handlers.TryRemove(kvp.Key, out SocketsHttpHandler? handler))
178-
handler.Dispose();
179-
}
180-
179+
DisposeHandlers();
181180
_httpClients.Dispose();
182181
}
183182
}

0 commit comments

Comments
 (0)