From a419ed68dc90d76efa5dc7a8ac6b06ac10f641b6 Mon Sep 17 00:00:00 2001 From: Tom Pallister Date: Sun, 8 Jul 2018 15:09:16 +0100 Subject: [PATCH] #398 refactored MemoryHttpClientCache to stop it holding onto references (#448) --- global.json | 2 +- src/Ocelot/Requester/IHttpClientCache.cs | 4 +- src/Ocelot/Requester/MemoryHttpClientCache.cs | 62 +++++++------------ 3 files changed, 27 insertions(+), 41 deletions(-) diff --git a/global.json b/global.json index 7281f931..ceb6f979 100644 --- a/global.json +++ b/global.json @@ -1,6 +1,6 @@ { "projects": [ "src", "test" ], "sdk": { - "version": "2.1.4" + "version": "2.1.300" } } \ No newline at end of file diff --git a/src/Ocelot/Requester/IHttpClientCache.cs b/src/Ocelot/Requester/IHttpClientCache.cs index 2c4571ce..8aaeeaab 100644 --- a/src/Ocelot/Requester/IHttpClientCache.cs +++ b/src/Ocelot/Requester/IHttpClientCache.cs @@ -4,7 +4,7 @@ public interface IHttpClientCache { - IHttpClient Get(string id); - void Set(string id, IHttpClient handler, TimeSpan expirationTime); + IHttpClient Get(string key); + void Set(string key, IHttpClient handler, TimeSpan expirationTime); } } diff --git a/src/Ocelot/Requester/MemoryHttpClientCache.cs b/src/Ocelot/Requester/MemoryHttpClientCache.cs index 9e4059e5..58a6c166 100644 --- a/src/Ocelot/Requester/MemoryHttpClientCache.cs +++ b/src/Ocelot/Requester/MemoryHttpClientCache.cs @@ -1,40 +1,26 @@ -namespace Ocelot.Requester -{ - using System; +namespace Ocelot.Requester +{ + using System; using System.Collections.Concurrent; - - public class MemoryHttpClientCache : IHttpClientCache - { - private readonly ConcurrentDictionary> _httpClientsCache; - - public MemoryHttpClientCache() - { - _httpClientsCache = new ConcurrentDictionary>(); + + public class MemoryHttpClientCache : IHttpClientCache + { + private readonly ConcurrentDictionary _httpClientsCache; + + public MemoryHttpClientCache() + { + _httpClientsCache = new ConcurrentDictionary(); } - - public void Set(string id, IHttpClient client, TimeSpan expirationTime) - { - if (_httpClientsCache.TryGetValue(id, out var connectionQueue)) - { - connectionQueue.Enqueue(client); - } - else - { - connectionQueue = new ConcurrentQueue(); - connectionQueue.Enqueue(client); - _httpClientsCache.TryAdd(id, connectionQueue); - } - } - - public IHttpClient Get(string id) - { - IHttpClient client= null; - if (_httpClientsCache.TryGetValue(id, out var connectionQueue)) - { - connectionQueue.TryDequeue(out client); - } - - return client; - } - } -} + + public void Set(string key, IHttpClient client, TimeSpan expirationTime) + { + _httpClientsCache.AddOrUpdate(key, client, (k, oldValue) => client); + } + + public IHttpClient Get(string key) + { + //todo handle error? + return _httpClientsCache.TryGetValue(key, out var client) ? client : null; + } + } +}