diff --git a/global.json b/global.json index 7281f931a..ceb6f9795 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 2c4571ce6..8aaeeaab7 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 9e4059e57..58a6c166d 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; + } + } +}