Skip to content

Commit

Permalink
#398 refactored MemoryHttpClientCache to stop it holding onto referen…
Browse files Browse the repository at this point in the history
…ces (#448)
  • Loading branch information
TomPallister authored Jul 8, 2018
1 parent d604bad commit a419ed6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 41 deletions.
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"projects": [ "src", "test" ],
"sdk": {
"version": "2.1.4"
"version": "2.1.300"
}
}
4 changes: 2 additions & 2 deletions src/Ocelot/Requester/IHttpClientCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
62 changes: 24 additions & 38 deletions src/Ocelot/Requester/MemoryHttpClientCache.cs
Original file line number Diff line number Diff line change
@@ -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<string, ConcurrentQueue<IHttpClient>> _httpClientsCache;

public MemoryHttpClientCache()
{
_httpClientsCache = new ConcurrentDictionary<string, ConcurrentQueue<IHttpClient>>();

public class MemoryHttpClientCache : IHttpClientCache
{
private readonly ConcurrentDictionary<string, IHttpClient> _httpClientsCache;

public MemoryHttpClientCache()
{
_httpClientsCache = new ConcurrentDictionary<string, IHttpClient>();
}

public void Set(string id, IHttpClient client, TimeSpan expirationTime)
{
if (_httpClientsCache.TryGetValue(id, out var connectionQueue))
{
connectionQueue.Enqueue(client);
}
else
{
connectionQueue = new ConcurrentQueue<IHttpClient>();
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;
}
}
}

0 comments on commit a419ed6

Please sign in to comment.