From c568f352eb4ff006777a22b805dd68594ca89775 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Mon, 6 May 2013 15:31:57 -0400 Subject: replaced http client cache with longer lived cache --- .../HttpClientManager/HttpClientManager.cs | 96 +++++++++++++--------- 1 file changed, 57 insertions(+), 39 deletions(-) (limited to 'MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs') diff --git a/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs b/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs index 87922f60a..ca19e8844 100644 --- a/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs +++ b/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs @@ -36,7 +36,7 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager private readonly IApplicationPaths _appPaths; private readonly IJsonSerializer _jsonSerializer; - //private readonly FileSystemRepository _cacheRepository; + private readonly FileSystemRepository _cacheRepository; /// /// Initializes a new instance of the class. @@ -64,7 +64,7 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager _jsonSerializer = jsonSerializer; _appPaths = appPaths; - //_cacheRepository = new FileSystemRepository(Path.Combine(_appPaths.CachePath, "http")); + _cacheRepository = new FileSystemRepository(Path.Combine(_appPaths.CachePath, "downloads")); } /// @@ -115,41 +115,57 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager { ValidateParams(options.Url, options.CancellationToken); - //var urlHash = url.GetMD5().ToString(); - //var infoPath = _cacheRepository.GetResourcePath(urlHash + ".js"); - //var responsePath = _cacheRepository.GetResourcePath(urlHash + ".dat"); + HttpResponseInfo cachedInfo = null; - //HttpResponseInfo cachedInfo = null; + var urlHash = options.Url.GetMD5().ToString(); + var cachedInfoPath = _cacheRepository.GetResourcePath(urlHash + ".js"); + var cachedReponsePath = _cacheRepository.GetResourcePath(urlHash + ".dat"); - //try - //{ - // cachedInfo = _jsonSerializer.DeserializeFromFile(infoPath); - //} - //catch (FileNotFoundException) - //{ + if (options.EnableResponseCache) + { + try + { + cachedInfo = _jsonSerializer.DeserializeFromFile(cachedInfoPath); + } + catch (FileNotFoundException) + { - //} + } - //if (cachedInfo != null && !cachedInfo.MustRevalidate && cachedInfo.Expires.HasValue && cachedInfo.Expires.Value > DateTime.UtcNow) - //{ - // return GetCachedResponse(responsePath); - //} + if (cachedInfo != null) + { + var isCacheValid = (!cachedInfo.MustRevalidate && !string.IsNullOrEmpty(cachedInfo.Etag)) + || (cachedInfo.Expires.HasValue && cachedInfo.Expires.Value > DateTime.UtcNow); + + if (isCacheValid) + { + try + { + return GetCachedResponse(cachedReponsePath); + } + catch (FileNotFoundException) + { + + } + } + } + } options.CancellationToken.ThrowIfCancellationRequested(); var message = GetHttpRequestMessage(options); - //if (cachedInfo != null) - //{ - // if (!string.IsNullOrEmpty(cachedInfo.Etag)) - // { - // message.Headers.Add("If-None-Match", cachedInfo.Etag); - // } - // else if (cachedInfo.LastModified.HasValue) - // { - // message.Headers.IfModifiedSince = new DateTimeOffset(cachedInfo.LastModified.Value); - // } - //} + if (options.EnableResponseCache && cachedInfo != null) + { + if (!string.IsNullOrEmpty(cachedInfo.Etag)) + { + message.Headers.Add("If-None-Match", cachedInfo.Etag); + } + else if (cachedInfo.LastModified.HasValue) + { + message.Headers.IfModifiedSince = new DateTimeOffset(cachedInfo.LastModified.Value); + } + } if (options.ResourcePool != null) { @@ -168,19 +184,22 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager options.CancellationToken.ThrowIfCancellationRequested(); - //cachedInfo = UpdateInfoCache(cachedInfo, url, infoPath, response); + if (options.EnableResponseCache) + { + cachedInfo = UpdateInfoCache(cachedInfo, options.Url, cachedInfoPath, response); - //if (response.StatusCode == HttpStatusCode.NotModified) - //{ - // return GetCachedResponse(responsePath); - //} + if (response.StatusCode == HttpStatusCode.NotModified) + { + return GetCachedResponse(cachedReponsePath); + } - //if (!string.IsNullOrEmpty(cachedInfo.Etag) || cachedInfo.LastModified.HasValue || (cachedInfo.Expires.HasValue && cachedInfo.Expires.Value > DateTime.UtcNow)) - //{ - // await UpdateResponseCache(response, responsePath).ConfigureAwait(false); + if (!string.IsNullOrEmpty(cachedInfo.Etag) || cachedInfo.LastModified.HasValue || (cachedInfo.Expires.HasValue && cachedInfo.Expires.Value > DateTime.UtcNow)) + { + await UpdateResponseCache(response, cachedReponsePath).ConfigureAwait(false); - // return GetCachedResponse(responsePath); - //} + return GetCachedResponse(cachedReponsePath); + } + } return await response.Content.ReadAsStreamAsync().ConfigureAwait(false); } @@ -237,7 +256,6 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager return Get(url, null, cancellationToken); } - /// /// Gets the cached response. /// -- cgit v1.2.3