forked from ThreeMammals/Ocelot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ThreeMammals#398 refactored MemoryHttpClientCache to stop it holding …
…onto references (ThreeMammals#448)
- Loading branch information
1 parent
d604bad
commit a419ed6
Showing
3 changed files
with
27 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |