mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 09:15:27 +08:00 
			
		
		
		
	This commit is contained in:
		@@ -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;
 | 
			
		||||
        }      
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user