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