mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-22 06:22:50 +08:00
This commit is contained in:
parent
d604badd49
commit
a419ed68dc
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"projects": [ "src", "test" ],
|
"projects": [ "src", "test" ],
|
||||||
"sdk": {
|
"sdk": {
|
||||||
"version": "2.1.4"
|
"version": "2.1.300"
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
public interface IHttpClientCache
|
public interface IHttpClientCache
|
||||||
{
|
{
|
||||||
IHttpClient Get(string id);
|
IHttpClient Get(string key);
|
||||||
void Set(string id, IHttpClient handler, TimeSpan expirationTime);
|
void Set(string key, IHttpClient handler, TimeSpan expirationTime);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,36 +5,22 @@
|
|||||||
|
|
||||||
public class MemoryHttpClientCache : IHttpClientCache
|
public class MemoryHttpClientCache : IHttpClientCache
|
||||||
{
|
{
|
||||||
private readonly ConcurrentDictionary<string, ConcurrentQueue<IHttpClient>> _httpClientsCache;
|
private readonly ConcurrentDictionary<string, IHttpClient> _httpClientsCache;
|
||||||
|
|
||||||
public MemoryHttpClientCache()
|
public MemoryHttpClientCache()
|
||||||
{
|
{
|
||||||
_httpClientsCache = new ConcurrentDictionary<string, ConcurrentQueue<IHttpClient>>();
|
_httpClientsCache = new ConcurrentDictionary<string, IHttpClient>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Set(string id, IHttpClient client, TimeSpan expirationTime)
|
public void Set(string key, IHttpClient client, TimeSpan expirationTime)
|
||||||
{
|
{
|
||||||
if (_httpClientsCache.TryGetValue(id, out var connectionQueue))
|
_httpClientsCache.AddOrUpdate(key, client, (k, oldValue) => client);
|
||||||
{
|
|
||||||
connectionQueue.Enqueue(client);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
connectionQueue = new ConcurrentQueue<IHttpClient>();
|
|
||||||
connectionQueue.Enqueue(client);
|
|
||||||
_httpClientsCache.TryAdd(id, connectionQueue);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public IHttpClient Get(string id)
|
public IHttpClient Get(string key)
|
||||||
{
|
{
|
||||||
IHttpClient client= null;
|
//todo handle error?
|
||||||
if (_httpClientsCache.TryGetValue(id, out var connectionQueue))
|
return _httpClientsCache.TryGetValue(key, out var client) ? client : null;
|
||||||
{
|
|
||||||
connectionQueue.TryDequeue(out client);
|
|
||||||
}
|
|
||||||
|
|
||||||
return client;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user