adjust http request caching

This commit is contained in:
Luke Pulverenti 2015-11-12 13:41:08 -05:00
parent 040c0ea21a
commit 788d302e41

View File

@ -164,22 +164,9 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager
} }
} }
//request.ServicePoint.BindIPEndPointDelegate = BindIPEndPointCallback;
return request; return request;
} }
private static IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount)
{
// Prefer local ipv4
if (remoteEndPoint.AddressFamily == AddressFamily.InterNetworkV6)
{
return new IPEndPoint(IPAddress.IPv6Any, 0);
}
return new IPEndPoint(IPAddress.Any, 0);
}
private void AddRequestHeaders(HttpWebRequest request, HttpRequestOptions options) private void AddRequestHeaders(HttpWebRequest request, HttpRequestOptions options)
{ {
foreach (var header in options.RequestHeaders.ToList()) foreach (var header in options.RequestHeaders.ToList())
@ -199,20 +186,6 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager
} }
} }
/// <summary>
/// The _semaphoreLocks
/// </summary>
private readonly ConcurrentDictionary<string, SemaphoreSlim> _semaphoreLocks = new ConcurrentDictionary<string, SemaphoreSlim>(StringComparer.OrdinalIgnoreCase);
/// <summary>
/// Gets the lock.
/// </summary>
/// <param name="url">The filename.</param>
/// <returns>System.Object.</returns>
private SemaphoreSlim GetLock(string url)
{
return _semaphoreLocks.GetOrAdd(url, key => new SemaphoreSlim(1, 1));
}
/// <summary> /// <summary>
/// Gets the response internal. /// Gets the response internal.
/// </summary> /// </summary>
@ -292,18 +265,6 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager
return response; return response;
} }
var semaphore = GetLock(url);
await semaphore.WaitAsync(options.CancellationToken).ConfigureAwait(false);
try
{
response = await GetCachedResponse(responseCachePath, options.CacheLength, url).ConfigureAwait(false);
if (response != null)
{
return response;
}
response = await SendAsyncInternal(options, httpMethod).ConfigureAwait(false); response = await SendAsyncInternal(options, httpMethod).ConfigureAwait(false);
if (response.StatusCode == HttpStatusCode.OK) if (response.StatusCode == HttpStatusCode.OK)
@ -313,11 +274,6 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager
return response; return response;
} }
finally
{
semaphore.Release();
}
}
private async Task<HttpResponseInfo> GetCachedResponse(string responseCachePath, TimeSpan cacheLength, string url) private async Task<HttpResponseInfo> GetCachedResponse(string responseCachePath, TimeSpan cacheLength, string url)
{ {
@ -360,14 +316,13 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager
_fileSystem.CreateDirectory(Path.GetDirectoryName(responseCachePath)); _fileSystem.CreateDirectory(Path.GetDirectoryName(responseCachePath));
using (var responseStream = response.Content) using (var responseStream = response.Content)
{
using (var fileStream = _fileSystem.GetFileStream(responseCachePath, FileMode.Create, FileAccess.Write, FileShare.Read, true))
{ {
var memoryStream = new MemoryStream(); var memoryStream = new MemoryStream();
await responseStream.CopyToAsync(memoryStream).ConfigureAwait(false); await responseStream.CopyToAsync(memoryStream).ConfigureAwait(false);
memoryStream.Position = 0; memoryStream.Position = 0;
using (var fileStream = _fileSystem.GetFileStream(responseCachePath, FileMode.Create, FileAccess.Write, FileShare.None, true))
{
await memoryStream.CopyToAsync(fileStream).ConfigureAwait(false); await memoryStream.CopyToAsync(fileStream).ConfigureAwait(false);
memoryStream.Position = 0; memoryStream.Position = 0;