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" ],
 | 
					  "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);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,40 +1,26 @@
 | 
				
			|||||||
namespace Ocelot.Requester
 | 
					namespace Ocelot.Requester
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    using System;
 | 
					    using System;
 | 
				
			||||||
    using System.Collections.Concurrent;
 | 
					    using System.Collections.Concurrent;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    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);
 | 
					
 | 
				
			||||||
            }
 | 
					        public IHttpClient Get(string key)
 | 
				
			||||||
            else
 | 
					        {
 | 
				
			||||||
            {
 | 
					            //todo handle error?
 | 
				
			||||||
                connectionQueue = new ConcurrentQueue<IHttpClient>();
 | 
					            return _httpClientsCache.TryGetValue(key, out var client) ? client : null;
 | 
				
			||||||
                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;
 | 
					 | 
				
			||||||
        }      
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user